title: CORS Request Error Tomcat 7 & Java Servlet
body: <p>I somehow can't get this to work. I have a Java Servlet running on a Tomcat 7 Server (running on localhost:8080) and I want to make a PUT-Request from a different Webservice (running on localhost:80). I have added the CORS-Accept-Header in the Servlet-Code as well as in the Tomcat Configuration, but somehow I always get this error when I try to make the PUT-Request:</p>
  
  <pre><code>[Error] Failed to load resource: Origin http://localhost is not allowed by Access-Control-Allow-Origin.
  </code></pre>
  
  <p>In my Java Servlet I have the following headers defined:</p>
  
  <pre><code>response.addHeader("Access-Control-Allow-Origin", "*");
  response.addHeader("Access-Control-Allow-Headers", "Content-Type");
  response.addHeader("Access-Control-Max-Age", "86400");
  </code></pre>
  
  <p>Before using response.addHeader I used response.setHeader, but this made no difference in my case.
  In the tomcat web.xml config file I have added the following lines:</p>
  
  <pre><code> &lt;filter&gt;
      &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.catalina.filters.CorsFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;cors.allowed.methods&lt;/param-name&gt;
          &lt;param-value&gt;GET,POST,HEAD,OPTIONS,PUT,DELETE&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;cors.allowed.origins&lt;/param-name&gt;
          &lt;param-value&gt;*&lt;/param-value&gt;
      &lt;/init-param&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
      &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>As described in the tomcat-7-docs: <a href="https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter" rel="nofollow">https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter</a></p>
  
  <p>I really can't see what I'm missing</p>