This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting the HTTP Request Headers  RSS feed

 
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.
 
That feels good. Thanks. Here's a tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!