File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Adding http headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Adding http headers " Watch "Adding http headers " New topic

Adding http headers

JP Estrada
Ranch Hand

Joined: Mar 21, 2006
Posts: 48
Is there a way to add http headers to a request every time a page is called? I mean aside from the usual http request headers generated, is there a way to add something like "aNewHeader: test1"?

What i need to happen is, every time i call a page from my project using Internet Explorer, there will always be a "aNewHeader" appended to the list of the container generated request headers.

I'm using HttpURLConnection's addRequestProperty() method. This works when i call a java application and call the addRequestProperty method. A new header will be added to the specified URL object..

But i can't find a way to have the "aNewHeader" header added when calling the page from IE.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

JP Estrada wrote:appended to the list of the container generated request headers.

Headers are not added by the container, but by the browser.

And, no, without resorting to Ajax, you have very little control over the requests generated by the browsers.

Why not back up and tell us what you are actually trying to accomplish? There's likely a better way.

[Asking smart questions] [About Bear] [Books by Bear]
JP Estrada
Ranch Hand

Joined: Mar 21, 2006
Posts: 48
yes right. by the browser. What i'm trying to do is simulate what Siteminder does during preauthentication. It adds a header called SM_USER to the browser generated headers. Our project is not setup with a real siteminder yet so we need to manually add SM_USER to the headers so development could continue.

There is a Firefox plugin called Modify Headers that allows you to add headers. Unfortunately i can't find a similar tool for Internet Explorer. And we have to test the pages in IE too. I'm wondering if this can be done by using the URLConnection class?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I think the best tool for inserting a request header before a servlet processes the request would be a servlet filter.
JP Estrada
Ranch Hand

Joined: Mar 21, 2006
Posts: 48
yes a filter is a good idea. I see you can set the values of existing headers by overriding the getHeaders(string) method of the HttpServletRequestWrapper. But what if want to add a new header name with a specific header value?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Also using "getHeader", "getHeaders" and "getHeaderNames". You'd need to override all of them, because you need to add the new header to the ones that are part of the underlying request.
JP Estrada
Ranch Hand

Joined: Mar 21, 2006
Posts: 48
Right. Thanks guys
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 544
Instead of playing with headers you can write filter in such a way that if SM_USER is not available in the Request header use some default value as a user id and process the request.
1) Your code after the filter should not rely on the SM_USER if you want to follow this approach.
2) You can initialize a new variable that represents SM_USER and put it in the request scope and use that as a user identifier.
3) In servlet filter try to read SM_USER and if it is not present then initialize the request attribute with some default value.
This way you will not have to use a browser specific solution.
Hope this helps.

I agree. Here's the link:
subject: Adding http headers
jQuery in Action, 3rd edition