| Author |
Setting headers on Http Request
|
Venkatesh Kumar
Ranch Hand
Joined: Aug 08, 2003
Posts: 68
|
|
Hi All: HttpServletRequest has getHeader but no methods such as setHeader(). Is there any way i can create header values on a http request for testing purpose ??? Thank you for your responses .
|
 |
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
|
|
|
In a servlet, the Request Header is basically read only. Since it was sent to the server from the client and will not be sent back, changing anything in the request header is basically usless. If you which to send Header information, you need to change it on the Response Object (Note the constraints on changing the header after you've written something to the response)
|
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Try wrapping the request and overriding the getHeaders method in a filter. http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequestWrapper.html
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Venkatesh Kumar
Ranch Hand
Joined: Aug 08, 2003
Posts: 68
|
|
Hi Ben: Thanks for your response .Can you please elaborate on your response. Thank you again .
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
The HttpServletRequestWrapper extends HttpServletRequest which means it is an instance of HttpServletRequest. In a filter, take the ServletRequest passed to you by the container. Pass it to the constructor of an HttpServletRequestWrapper object, (you'll have to down cast it to HttpServletRequest). Pull all of the original headers from the wrapped servletRequest using super.getHeaders and super.getHeader.... etc. Load all of the headers into an hashtable. Override the getHeaders, getHeaderNames,..etc returning the values from your hashtable instead of the values in the original (wrapped) request object. Pass the HttpServletWrapper object to the doChain method instead of the request that was originally passed to you. It sounds like a lot of work but it's not. The HttpServletWrapper class provided by the servlet spec already did most of the grunt work for you. If you want a running example of the wrapper pattern, look at http://simple.souther.us/capture.war. This example is actually wrapping the response object, not the request, but it should give you a basic idea.
|
 |
Venkatesh Kumar
Ranch Hand
Joined: Aug 08, 2003
Posts: 68
|
|
Hey Ben: Thank you for elaborating on your previous response. Thanks
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
|
No problem, let me know if when you get it working.
|
 |
 |
|
|
subject: Setting headers on Http Request
|
|
|