Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Headers setting and getting  RSS feed

 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My servlet sets a header like this, and then redirects to another jsp
resp.setHeader("Test","test");
resp.sendRedirect("Result.jsp");
And in result.jsp I am trying to get the header like this
String x = request.getHeader("Test");
out.println(x);
Now, this returns me a null value, Or I am not getting the header.
Am I missing something ? Please suggest
Thanks
 
Chris Reeves
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are putting in the header, but it's probably a bit cleaner to pass the item in the session.
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isnt a header counted as output returned to teh client? You cant send a redirect if you have sent anything to the client, and I think this includes headers. I think that you have been lucky this time and the response is buffered, so it just gets chucked. It would be better for forward the request instead. (and as mentioned store as scoped attribute
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maya,
Headers names and values are specific to the client-server protocol and are not meant for application specific data. But if you do want to send the name-value pair Test=test to the browser and receive it back with subsequent requests then use the Cookies mechanism.
In your example:
The servlet must use response.setHeader("Set-Cookie","Test=test");
Result.jsp page must use request.getHeader("Cookie");
This will return the string "Test=test; JSESSIONID=C7123123213123..." which you can parse and extract the values. Or better use the Cookie class.
-j
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!