title: Tomcat "Access-Control-Allow-Origin" and JavaScript
body: <p>i have a huge problem with CORS on my server. My server is running on Tomcat on localhost. The server i implemented has a couple of RestFul resources. 
  For example:</p>
  
  <pre><code>@Path("user")
  public class UserService {
  @GET
  @Path("/{username}/{password}")
  @Produces("text/plain")
  public String checkLoginParameter(@PathParam("username") String username, @PathParam("password") String password) {
          String tempUserName = "";
          String tempPassword = "";
          String bool = "";
  
          EntityManager em = createEntityManager();
          try{
              User user = (User)em.createQuery("SELECT u FROM User u WHERE u.username='"+username+"' AND u.password='"+password+"'").getSingleResult();
              tempUserName = user.getUsername();
              tempPassword = user.getPassword();
          }
          catch(Exception e){}
  
          if(username.equals(tempUserName) &amp;&amp; !username.isEmpty() &amp;&amp; password.equals(tempPassword) &amp;&amp; !password.isEmpty()){
              bool = "true";
          }
          else{
              bool = "false";
          }
          return bool;
     }      
  }
  </code></pre>
  
  <p>The Ressource is working. 
  Now i want to access to the Ressource via JavaScript from a different Website. When i try to do this, i get a Error message:</p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:8080/RSAppStore/user/poc/poc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
  </code></pre>
  
  <p>I know i have add to my Server the Permission for CORS.
  I do it this way:</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;filter&gt;
  &lt;filter-name&gt;CORS&lt;/filter-name&gt;
  &lt;filter-class&gt;com.thetransactioncompany.cors.CORSFilter&lt;/filter-class&gt;
  &lt;init-param&gt;
   &lt;param-name&gt;cors.allowOrigin&lt;/param-name&gt;
      &lt;param-value&gt;*&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;init-param&gt;
   &lt;param-name&gt;cors.supportedMethods&lt;/param-name&gt;
      &lt;param-value&gt;GET, POST, HEAD, PUT, DELETE&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;init-param&gt;
  &lt;param-name&gt;cors.supportedHeaders&lt;/param-name&gt;
  &lt;param-value&gt;*&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;init-param&gt;
      &lt;param-name&gt;cors.exposedHeaders&lt;/param-name&gt;
      &lt;param-value&gt;Set-Cookie&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;init-param&gt;
      &lt;param-name&gt;cors.supportsCredentials&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
  &lt;filter-name&gt;CORS&lt;/filter-name&gt;
  &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>To access with JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(function(){
    $.ajax({
      type: "GET",
      crossDomain: true,
      url: "http://robosmart-appstore:robosmart@localhost:8080/RSAppStore/user/poc/poc",
  
      contentType: "text/plain",
      success: function( string ) {
  
        console.log(string);  
  
  
      },
      error:function () {
        console.log("err");
      }
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>In addition i have added these 2 jars.</p>
  
  <pre><code>cors-filter-1.9.2.jar
  java-property-utils-1.9.jar
  </code></pre>
  
  <p>I read so many examples and explanations on the Internet, but nothing seems to work and i don't know what i am doing wrong.</p>