Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting the HTTP Request Headers

 
Abhishek Dwaraki
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I also had the same issuse a while back.

May be this thread may help you a bit.
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic