Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set http headers in servlet request ?

 
Ramakrishna Raju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ramakrishna Raju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I could not understand fully. Would you please eloborate if you dont mind.

Thanks,
Raju
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
kumari Jain
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dirk,
In Servlet 2.2, we dont have wrapper classes also.
So, you want me right my own wrapper class?

Thanks,
Raju
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic