File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Setting the HTTP Request Headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Setting the HTTP Request Headers" Watch "Setting the HTTP Request Headers" New topic

Setting the HTTP Request Headers

Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Hey Guys,
This is the scenario... I need to set a value in the HTTP headers which are sent from the browser, i.e the JSP and need to retrieve that value in a servlet using getHeader() or an equivalent. For eg, I need to send a value like the username in the header and then retrieve the same in the servlet. I'm currently using the URLConnection class' addRequestProperty(String key, String value) to set the value and then retrieve it in the servlet using request.getHeader(). But it is returning null all the time.. plzzzz do let me know about the issue. The code is below.

JSP code :

<%URL url = new URL http://localhost:9090/MMPortal/servlet/ShowRequestHeaders");
URLConnection conn = url.openConnection();

Servlet Code :

request.getHeader("UserID"); inside the doGet() method.

Please do advice on this. Thanks in advance.

Abhishek Dwaraki
Dept of Electrical and Comp Engg
University of Massachusetts, Amherst
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090

I also had the same issuse a while back.

May be this thread may help you a bit.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Generally a browser doesn't suppose to return any headers you set in a request. However it will return all cookie values set. Maybe it helps.

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: Setting the HTTP Request Headers
It's not a secret anymore!