title: Java filter failing to set response headers
body: <p>I am trying to create a Java "Filter" which detects a custom HTTP Request Header, and inserts response headers so that the file will download automatically.  The response header that is most important for this is the "Content-Type = Attachment" response header.  I have created an HTTP request object that inserts the custom Header:</p>
  
  <pre><code>function myHttpObject(filePath){
  function makeHttpObject() {
      return new XMLHttpRequest();
  }
  
  var request = makeHttpObject();
  
  request.open("GET", filePath, false);
  request.setRequestHeader("X-Wria-Download", "PDFdownload");
  request.send(null);
  window.open(filePath);
  console.log(request.getAllResponseHeaders());
  }
  </code></pre>
  
  <p>This will insert the X-Wria-Download header into the request.
  Then I have a Java Filter which looks for that request header and should set the response header to "Content-Type=attachment"</p>
  
  <pre><code>import javax.servlet.*;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  public class Contenttypefilter implements Filter  {
  
  protected FilterConfig filterConfig;
  
  public void init(FilterConfig filterConfig) throws ServletException {
      this.filterConfig = filterConfig;
  }
  
  public void destroy() {
      //noop
  }
  
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  
      HttpServletRequest req = (HttpServletRequest) request;
      HttpServletResponse res = (HttpServletResponse) response;
  
      //get the headers we placed in the request
      //based on those request headers, set some response headers
  
      if(req.getHeader("X-Wria-Download") != null){
          res.setHeader("Content-Type", "application/pdf");
          res.setHeader("Content-Disposition", "attachment; filename=success.pdf");
      }
  
      chain.doFilter(req,res);
  }
  
  
  }
  </code></pre>
  
  <p>And then of course the web.xml has the code to include the Filter on all jsp files.  </p>
  
  <p>The thing that is baffling me, is that the header is being set on the response file, but it is not downloading as it should.  If I put the res.setHeader("Content-Disposition", "attachment; filename=success.pdf"); line outside the "if" statement, then it will work, but it will apply the download behavior to all JSP's which I don't want.  </p>
  
  <p>Why is it applying the content-disposition but not working when I have the res.setHeader in the if statement; and then working when it is outside the if statement?  Any ideas for how I can get the desired behavior (only applying content disposition to jsp's that I have applied a custom request header to)?</p>