Adding HTTP Headers To Requests in Filters and Servlets

The aim of this tutorial is to demonstrate how you can add Headers to the ServletRequest and HttpServletRequest objects in either servlets or filters. This requires subclassing the HttpServletRequestWrapper class.

First let us have a look at subclassing the HttpServletRequestWrapper class. As you can see we have overridden getHeaderNames and getHeader to include our custom headers. These custom header are added through the addHeader function which is a new function. Our custom headers are stored in a hashmap.

 
package myfilter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;
 
public class MyServletRequestWrapper extends HttpServletRequestWrapper{
 
    private Map headerMap;
 
     public void addHeader(String name, String value){
         headerMap.put(name, new String(value));
     }
 
     public MyServletRequestWrapper(HttpServletRequest request){
         super(request);
         headerMap = new HashMap();
     }
 
     public Enumeration getHeaderNames(){
 
         HttpServletRequest request = (HttpServletRequest)getRequest();
 
         List list = new ArrayList();
 
         for( Enumeration e = request.getHeaderNames() ; e.hasMoreElements() ;)
             list.add(e.nextElement().toString());
 
         for( Iterator i = headerMap.keySet().iterator() ; i.hasNext() ;){
             list.add(i.next());
         }
 
         return Collections.enumeration(list);
     }
 
     public String getHeader(String name){
         Object value;
         if((value = headerMap.get(""+name)) != null)
             return value.toString();
         else
             return ((HttpServletRequest)getRequest()).getHeader(name);
 
     }
 }
 
 

Now let's have a look at a filter that will use this wrapper to add some custom headers. You can observe on line 31 how we use the wrapper to wrap the HttpServletRequest.

 
package myfilter;
 
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
 
 import java.util.logging.*;
 
 
 public class MyFilter implements javax.servlet.Filter {
     private ServletContext servletContext;
     private Logger log;
 
     public MyFilter(){
         super();
     }
 
     public void init(FilterConfig filterConfig) throws ServletException {
         servletContext = filterConfig.getServletContext();
         log = Logger.getLogger(MyFilter.class.getName());
     }
 
     public void doFilter(   ServletRequest req, 
                             ServletResponse res, 
                             FilterChain filterChain)
         throws IOException, ServletException {
 
         MyServletRequestWrapper httpReq    = new MyServletRequestWrapper((HttpServletRequest)req);
         HttpServletResponse    httpRes   = (HttpServletResponse)res;
 
         HttpSession session = httpReq.getSession();
 
         httpReq.addHeader("MY-HEADER-X1", "header 1");
         httpReq.addHeader("MY-HEADER-X2", "header 2");
 
         filterChain.doFilter(httpReq, httpRes);
 
     }
 
     public void destroy(){
     }
 
 }