Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting an HTTP header from within a jsp  RSS feed

 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement to send a new header parameter(like "mynewheader") to the client from a jsp page. How can this be achieved? The request object does not have the setHeader method and if I set it through the response object I am not able to get the value at the server.

Some more back ground to my problem:
The form submit invokes a rest service that tries to get the value using an @HeaderParam annotation.
This value is populated correctly if I attempt to post my request using an HttpURLConnection and set the new header value that I need to populate using the
setRequestProperty method.
I want to achieve the same result using a jsp

Thanks!




 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. You want to send the header "to the client from a jsp page" - in other words from the server to the client. But you are "not able to get the value at the server" - which would make sense if the header is sent from the server TO the client: the client would be the place to retrieve the header.

The second paragraph sounds like the header should go from the client to a REST server, though. So, ..., what kind of client are you trying to make this work with when you say "I want to achieve the same result using a jsp"? From where to where do the requests and responses go, and at which point should the header be set?
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. The requirement is to send a header from a jsp to the server. The client is the jsp.

I have a java class that does a post to the server by creating an HttpURLConnection. Here I am able to
setRequestProperty("mynewheader","something") and retrieve the same using the @HeaderParam annotation.

I want the same behaviour when I use a jsp but I dont have a clue how to set mynewheader from within the jsp.

Thanks!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other than GET or POST requests what kind of requests are you sending from a JSP?!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "how to set mynewheader from within the jsp" - do you want to send this request while the JSP is being executed on the server (in other words, the request would go from the servlet container to a different server), or should the HTML page that is being generated by the JSP somehow send this REST request? If the former, then you can use the Java class you mentioned earlier to send the request from within the JSP. If the latter, then this can be accomplished using the JavaScript XMLHttpRequest object and its setRequestHeader method, but not using HTML forms.
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that worked! Here is a sample that helped me.

http://www.weberdev.com/get_example-4413.html
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent. If you'll need to use XMLHttpRequest more regularly, you'll want to look into using an AJAX library that takes some of the pain out of it, like Prototype.
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!
JavaRanch rocks
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!