File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet - Servlet Communication - Setting the headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet - Servlet Communication - Setting the headers" Watch "Servlet - Servlet Communication - Setting the headers" New topic

Servlet - Servlet Communication - Setting the headers

Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Hey people,
This is the scenario.. I need to send a custom value in the header of my request alongwith the normal request headers and then retrive it in the destination servlet. For eg, I want to send the user name in the header of the request, whether it is from a servlet or from a JSP, and then retrieve the value in the servlet that processes the information. If it is a servlet to servlet communication, can the response headers be used to set and retrieve the value....? Someone plzzzzz advice in this regard and it is a bit urgent. Thanks in advance for the help. You can mail me at if you come up with anything plz.

Abhishek Dwaraki
Dept of Electrical and Comp Engg
University of Massachusetts, Amherst
Manuel Palacio
Ranch Hand

Joined: Oct 16, 2000
Posts: 45
Can't you just append the value as a request parameter?

like: myapp/whatever.html?userName=ziggy

[ April 07, 2006: Message edited by: Manuel Palacio ]

<a href="" target="_blank" rel="nofollow"></a>
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

What request is this that you want to attach headers to? Were you hoping that by attaching a header to your response, the client (browser) would kindly copy it over to the next request it sends to your host? That isn't going to work. You say
I want to send the user name in the header of the request, whether it is from a servlet or from a JSP
but that doesn't make much sense, because servlets and JSPs don't send requests. They send responses to requests.
Ganesh Chinni

Joined: Dec 30, 2005
Posts: 24
Can you explain why do you want to put user name in the headder and send it, when there are other ways to send the parameter.

Putting username in headder is not a good idea.
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80

In which senarios we put data in response header?

Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
If it is a servlet to servlet communication

=> are you talking:
1) of a servlet forwarding a request to another servlet or JSP before reponding to the client?
2) of a servlet processing a request, saving some info about it, sending a response to the browser and expecting to get the info back when getting another request from the save client?

In the first case, you would rather put your info in an attribute of the ServletRequest and not in a a parameter. This info disapears as soon as the response is sent to the client. Have a look at ServletRequest.setAttribute(String, Object).

If you want to keep some info during several requests of the client, you wan to put the attribute in the HttpSession. Have a look at ServletRequest.getSession() as well as at this encoreURL thing.


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Originally posted by siva prakash:

In which senarios we put data in response header?


As application developers, we shouldn't really have to.
The headers are for manipulating aspects of the HTTP protocol.

Some cases where an application developer might find him/herself writing headers would be servlet/applet communication. You would, of course, need to understand HTTP headers if you were to write your own web client.

I built a component for an application that served up some server side dynamic CSS. By working with the If-Modified-Since and Date headers I was able to get the browser to use it's cached version of the CSS sheet when there were no changes. This cut down on a lot of bandwith usage.

I would be very hesitant to start adding my own headers just to pass information back and forth between server side components. That's what querystring and post parameters are for.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Servlet - Servlet Communication - Setting the headers
It's not a secret anymore!