This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
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

Possible to add new custom header

 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all...

Is it possible to add your own custom header besides the existing headers?
If yes.. how can I do that....??

Thanks...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html#addHeader(java.lang.String,%20java.lang.String)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If, on the other hand, you mean add a header to a HttpServletRequest before forwarding it, you can't do it directly. You have to do it indirectly by creating a custom implementation of HttpServletRequestWrapper.
Bill
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you guide me or explain in more details... on the HttpRequestServletWrapper..
perhaps some sample code..

Thanks..

I'm really out of ideas coz.. I had.. use response.setHeader() or response.addHeader() plus forwarding the request to another page... but it just give me NULL value...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain what it is that you're trying to do with the headers?
It would make it easier to help you.
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I explain to you..
Er...

I have a page which collect inputs from the user (via form). Once I'd submitted, I need to get those parameter values and add new headers (not parameters) containing those values accordingly. From there onwards, I need to forward those newly added headers to another page. Please note that the page (another company which hosted the page) that I wanted to invoke specifically told me to use headers instead of passing the parameter values...

Do you understand my question..??
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're passing this request to another application on another server?
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I'm trying to pass new headers to another server. Is this possible?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but not the with anything built into the servlet spec.

You'll want to read up on UrlConnection
or look a third party library such as jakarta commons httpclient to build your own client within the servlet.
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So.. what you means is.. it wont be possible to add new header using servlet api...

I need to use third party.. library or functions?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that a HttpServletRequest is an object created by your servlet container - it is only valid inside that container. If you want to send a modified request to another server, you have to construct it "by hand". This means creating a URLConnection (really a HttpURLConnection), populating it with headers and connecting to the other server.
OR
Like Ben said, use the HttpClient toolkit - it will save you lots of time, manage cookies, etc. etc.
Bill
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...what you all saying is that...
inside the servlet...
I need to create HttpUrlConnection and then define my own header..
and forward it to another server...

Is it something like this

HttpUrlConnection huc=new HttpUrlConnection( new URL("watever"));
huc.setRequestMethod("POST");
huc.setRequestProperty("test","hi");
huc.connect();

if(huc.getResponseCode() != 200)
out.println(huc.getResponseMessage());
else
out.println("message sent successfully");

if(huc!=null)
huc.disconnect();
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a quick glance, it looks good.
You're on the right track.

Again, you may want to consider Apache's httpclient.
You'll be able to work with a slightly higher level API.
[ August 22, 2005: Message edited by: Ben Souther ]
 
Simon Sew
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... I will try out the HttpClient oso..
Thank you for pointing me to the right direction...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic