devxlogo

Updating HTTP Headers

Updating HTTP Headers

Question:
In the first servlet we set the value for HTTP header as below:
response.setHeader(“SessionKey”,”SessionValue”);

In the next servlet we tried retrieving the above set header value as:
request.getHeader(“SessionKey”);

We are getting null as output of getHeader. How can we get the HTTP header set in first servlet, in second servlet?

Answer:
I assume from your question that you are doing some kind of request forwarding from one servlet to another. If this is the case, the mistake you are making is to set the header in the response. The response variable is an instance of “HttpServletResponse,” which is used to return a result back to the client accessing the servlet. Any headers you set in the reponse will be sent to the client, not to a chained servlet.

If you want to add headers to a forwarded request,set them in the request variable before passing it on to the next servlet in the chain.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist