Question : problem adding response headers in servlet

I am adding values to a response header in a servlet:
   response.addHeader("detail 1:", "something");
   response.addHeader("detail 2:", "something else");

I then redirect to another servlet to see if the response headers were actually added:
   response.sendRedirect("servlet2");

in servlet2 I enumerate and print the headers:

   Enumeration e = request.getHeaderNames();
   while (e.hasMoreElements()) {
      String name = (String)e.nextElement();
      String value = request.getHeader(name);
      System.out.println("   HTTP HEADER: " + name + " = " + value);
   }

The header values I added are NOT there.  Can anyone tell me what I'm doing wrong?  I also tried the response.setHeader("deatail1", "something") method, but that didn't work either.

Answer : problem adding response headers in servlet

HttpServletResponse#sendRedirect() [1] will only tell the web client to redirect to another page.  The redirect instruction itself will contain your headers, and the following request will have a fresh set of headers.  You might want to check out the RequestDispatcher#forward() [2] method, which will pass on the same request and response.  However, this will only work if it the place you want to redirect to is in the same container.

[1] http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String)
[2] http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
Random Solutions  
 
programming4us programming4us