wood burning stoves 2.0*
The moose likes Servlets and the fly likes Adding custom Header information to the HttpServletRequest object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Adding custom Header information to the HttpServletRequest object" Watch "Adding custom Header information to the HttpServletRequest object" New topic
Author

Adding custom Header information to the HttpServletRequest object

Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112


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

Joined: Jul 14, 2010
Posts: 754

Hello Venkata.

Add a response header is not working for you?


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Venkata Sirish
Ranch Hand

Joined: Apr 09, 2007
Posts: 112
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

Joined: Mar 22, 2000
Posts: 12788
    
    5
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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding custom Header information to the HttpServletRequest object