• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting headers on Http Request

 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben:

Thanks for your response .Can you please elaborate on your response.

Thank you again .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ben:

Thank you for elaborating on your previous response.

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, let me know if when you get it working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic