• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding custom Header information to the HttpServletRequest object

 
Venkata Sirish
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi ranchers,

Iam working on a J2EE struts web application. I want to send application information to the server through the Request Headers.

Is there anyway that i can add a custom header information like " isValidUser : true " to the HttpServletRequest object programatically just like we do it using addHeader method in the HttpServletResponse class.

I don't want to send the information through the Request Params / Query string and i want to send application information to the server in the Request Headers Only.

Any alternate way to achieve this ? Thanks in advance for your time and attention.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Venkata.

Add a response header is not working for you?
 
Venkata Sirish
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hebert,

I want to send the data in Request Header not in the Response Header.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The set of request headers - parsed out of the request by the servlet container - that you get in a servlet doGet etc method can not be changed - if you want to add something to a request you can:

1. use the setAttribute method - subsequent code would have to use getAttribute to get the value.

2. create a custom implementation of HttpServletRequestWrapper with special handling of the getHeader method to return the value you want to plug in.


Given that you have setAttribute, I can think of no valid reason to use method 2.

Bill
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'd need to create a ServletRequestWrapper/HttpServletRequestWrapper implementation.

But it depends on how you're making the request--if it's from the JavaScript side via Ajax, most JS libs provide access to the headers. If you're trying to do it via something like a form post you're likely out of luck.

Man, I need to hit send earlier.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic