Solved

problem adding response headers in servlet

Posted on 2006-10-19
1
470 Views
Last Modified: 2013-12-10
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.
0
Comment
Question by:dhenderson12
1 Comment
 
LVL 9

Accepted Solution

by:
shinobun earned 300 total points
ID: 17769110
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)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question