Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using Servlet Filter to modify headers

843841Apr 4 2005 — edited Apr 6 2005
I seem to be unable to modify the headers using a Servlet filter, but I'm not sure why.

The code looks like this:
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
    try {

      //System.out.println("Intercepted by filter 2");



      try {
        HttpServletResponse http = (HttpServletResponse)response;

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+10);

        http.setDateHeader("Date", cal.getTime().getTime());
        http.setDateHeader("Expires", cal.getTime().getTime());

      } catch (ClassCastException ignored) {
        System.out.println("ClassCastException");
      }

      filterChain.doFilter(request, response);
    }
    catch(ServletException sx) {
      filterConfig.getServletContext().log(sx.getMessage());
    }
    catch(IOException iox) {
      filterConfig.getServletContext().log(iox.getMessage());
    }
  }
It does not print any errors, and I had a System out which confirmed that this filter was being used. It is supposed to set the Expires header to 10 years from now, but all it does is clear out the Expires header so when I look at the image downloaded on the client side and check the header information it shows 'NONE'. I also threw in the Date header for create date and instead of changing the date it too made the header show 'NONE'.

Any idea what's going on?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 4 2005
Added on Apr 4 2005
4 comments
155 views