Two Laptop Bag*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Setting the HTTP Request Headers" Watch "Setting the HTTP Request Headers" New topic
Author

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();
conn.setAllowUserInteraction(false);
conn.setDoOutput(true);
conn.setRequestProperty("UserID","Abhishek");
%>


Servlet Code :

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

Please do advice on this. Thanks in advance.


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

Joined: Apr 13, 2003
Posts: 1088
Hi

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="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting the HTTP Request Headers
 
Similar Threads
header and headerValues
Small Tomcat configuration problem
Retriving Init Param of a JSP
setting http Header using urlconnection
Call servlet from java class and redirect to jsp page