Setting custom HTTP headers


General J2EE: Setting custom HTTP headers

  1. Setting custom HTTP headers (1 messages)

    Hi, An application I'm working with is going to be receiving information from a security and authentication system by way of a custom HTTP header. My application will read the data in that header and use it to retrieve data In order to test my code, I need to create a mockup that will mimic the security system's actions, i.e. I need to set an HTTP header that will be read by my application. I tried using the HttpServletResponse.setHeader(...) method in conjunction with calls to sendRedirect, forward and include, but nothing works. Is there a way that I can set a custom HTTP header within a servlet/JSP that can be read by another servlet/JSP? Thanks in advance!
  2. To test your Servlet you could use HTTPUnit or Cactus. Or if you just want something really quick, you could do something like this: Create a Filter that wraps the HttpServletRequest in your own subclass of HttpServletRequestWrapper. Override the getHeader() method to return your custom header, or return the regular header: public class CustomRequest extends HttpServletRequestWrapper { @Override public String getHeader(String name) { if ("customHeader".equalsIgnoreCase(name)) { return "customValue"; } return super.getHeader(name); } }