• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Adding http headers

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
JP Estrada
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. Thanks guys
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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 brought this back from the farm where they grow the tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic