aspose file tools*
The moose likes Servlets and the fly likes Possible to add new custom header Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Possible to add new custom header" Watch "Possible to add new custom header" New topic
Author

Possible to add new custom header

Simon Sew
Greenhorn

Joined: Aug 14, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html#addHeader(java.lang.String,%20java.lang.String)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
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

Joined: Aug 14, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 14, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

So you're passing this request to another application on another server?
Simon Sew
Greenhorn

Joined: Aug 14, 2005
Posts: 9
Yup, I'm trying to pass new headers to another server. Is this possible?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 14, 2005
Posts: 9
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

Joined: Mar 22, 2000
Posts: 12821
    
    5
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

Joined: Aug 14, 2005
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Aug 14, 2005
Posts: 9
Thanks... I will try out the HttpClient oso..
Thank you for pointing me to the right direction...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible to add new custom header