• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting headers on Http Request

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ben:

Thank you for elaborating on your previous response.

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, let me know if when you get it working.
 
reply
    Bookmark Topic Watch Topic
  • New Topic