• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Setting the HTTP Request Headers

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I also had the same issuse a while back.

May be this thread may help you a bit.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic