my dog learned polymorphism*
The moose likes Servlets and the fly likes How to set http headers in servlet request ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to set http headers in servlet request ?" Watch "How to set http headers in servlet request ?" New topic
Author

How to set http headers in servlet request ?

Ramakrishna Raju
Greenhorn

Joined: Jul 15, 2004
Posts: 7
Hi all,
I have a peculiar problem. I need to set headers in the servlet request.

We can get the headers from the request by using
getIntHeader(java.lang.String name) method in HttpServletRequest interface.

But, the question is how to set the headers. For example, I want to set a header of a key-value pair (eg: myname , raju) in request.
Would any one can help me how to do that.

Thanks ,
Raju
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
You'll want to use the addHeader() method on the HttpServletResponse. You can add anything you'd like.
[ September 23, 2004: Message edited by: Scott Dunbar ]

<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Ramakrishna Raju
Greenhorn

Joined: Jul 15, 2004
Posts: 7
Yeah..but I need to add it to Request, not to Response.

The thing is that... some class in third party jar is reading the headers from the request. So, I need to manipulate somehow to set the values in the request header before I send it to the third party jar.
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
Oh, I apologize, I didn't read your post close enough.

This is an ugly problem. The only thing I can come up with is to create your own request class from HttpServletRequestWrapper and pass through all getHeader() requests to your super but check to see if it is the parameter you want to add. You're kinda faking it as the request is not truly in the HTTP request but the caller doesn't know that.
Ramakrishna Raju
Greenhorn

Joined: Jul 15, 2004
Posts: 7
Sorry I could not understand fully. Would you please eloborate if you dont mind.

Thanks,
Raju
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
If I assume that you have a javax.servlet.Filter then it is pretty straight forward. You will extend javax.servlet.http.HttpServletRequestWrapper. In your filter you would create a new request of your wrapper type (there is a copy constructor to make it simple). The only method you'd overload in your wrapper class would be getHeader(). The getHeader() method in your wrapper class would look something like:



In your filter class your doFilter() method would look something like:




This does assume a 2.3 servlet container as Filters were introduced in the 2.3 servlet spec.
Ramakrishna Raju
Greenhorn

Joined: Jul 15, 2004
Posts: 7
Hello Scott,
Thanks so much for taking paing to explain me. But unfortunately, we are using servlets 2.2 and hence filters are of no use :-(

I have a feeling that I should use URLConnector but not quite sure how to make use of it.

Thanks anyway..

Raju
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Raju, I think you can still do what Scott suggested. Instead of using a Filter, which isn't an available option for you, perhaps you could just forward the request from one Servlet to another.In this case, resource would be a String that described the location of the Servlet to forward the request to.

Were you able to get things working?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
kumari Jain
Ranch Hand

Joined: Jul 28, 2004
Posts: 44
Hi all,
i tried Scott's example,but there's a catch.

if this is my wrapper class-RequestWrapTest.java

import javax.servlet.*;
import javax.servlet.http.*;

class RequestWrapTest extends HttpServletRequestWrapper
{

public RequestWrapTest(HttpServletRequest request)
{
super(request);
}
/*Overloading the getHeader method*/
public String getHeader(String name)
{
String value=null;
if("Application".equals(name))
{
value = new String("Shopping Cart");
}
else
{
value = super.getHeader(name);
}
return value;
}
}

and this is the filter that instantiates the request FilterWrapperTest.java

import javax.servlet.*;
import javax.servlet.http.*;

class FilterWrapperTest implements Filter
{
FilterConfig config = null;
public void init(FilterConfig config)
{
this.config = config;
}
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
{
RequestWrapTest request = new RequestWrapTest(req);
chain.doFilter(request,res);
}
public void destroy()
{
System.out.println("In destroy method");
}
}
on compiling the filter ,i get the following error

---------- javac ----------
FilterWrapperTest.java:14: cannot resolve symbol
symbol : constructor RequestWrapTest (javax.servlet.ServletRequest)
location: class RequestWrapTest
RequestWrapTest request = new RequestWrapTest(req);
^
1 error
Output completed (5 sec consumed) - Normal Termination

Now RequestWrapTest can't have another constructor taking ServletRequest object,
since the super class has only one constructor that takes HttpServletRequest.

So what should i do to have the FilterWrapperTest file successfully compiled

Regards,
kumari.
Ramakrishna Raju
Greenhorn

Joined: Jul 15, 2004
Posts: 7
Hi Dirk,
In Servlet 2.2, we dont have wrapper classes also.
So, you want me right my own wrapper class?

Thanks,
Raju
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set http headers in servlet request ?
 
Similar Threads
Problems with AJAX+multipart/formdata+Commons FileUpload
difference between request and response headers
Access Headers
why service() accepts HttpServletResponse
qn on Trace Method