Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting HttpServletRequest headers

 
Thomas Mcfarrow
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to make a code stub for some SSO software that my company uses. I wan't to mimick its behavior by setting HttpServletRequest headers and forwarding it on. How could I set request headers?

Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are modifying an existing request, the servlet API provides the HttpServletRequestWrapper class to accomplsh this since you can't modify an existing request directly.
You create a custom wrapper that watches for getParameter calls specifying the parameters of interest.
If you are creating a new request, just implement the HttpServletRequest interface in your custom class.
Bill
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am curious about this...
Where we can modify a request using that request wrapper?
In a filter class or servlet class?

Correct me if my understanding is wrong
thanks
daniel
 
Thomas Mcfarrow
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I don't want to modify the query string. I want to modify the HTTP header.

Thanks in advance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically you use a Filter to substitute your custom request wrapper object for the original request.
Your custom wrapper will be what the servlet sees so every request for a header OR for a parameter OR for an input stream will go to your filter first.
Take a look at the javax.servlet.http.HttpServletRequestWrapper class JavaDocs to see how convenient the designers have made this capability.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic