This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Logging HTTP Response Headers  RSS feed

 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to read HTTP response headers with Servlets? HttpServletResponse does not seem to have anything that serves this purpose.

Also I'm using a JBoss server, so if there is no way to do this with the Servlet spec, is there some JBoss specific way of reading this information before it gets served?
 
Gaurav Sainii
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there any way to read HTTP response headers with Servlets?

Yes you can get these values.

HttpServletResponse does not seem to have anything that serves this purpose.

Please see the specification of getHeader(java.lang.String name) method of this class HttpServletResponse.java it provides you with the value of the response header with the given name.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the specification of getHeader(java.lang.String name) method of this class HttpServletResponse

HttpServletResponse has no such method; it's possible to set headers, not get them.
 
Gaurav Sainii
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer the API specification.

For method getHeader here is the direct link:-

getHeader(java.lang.String name)

Please correct me if I am missing anything. :-)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That assumes a Servlet 3.0 container, which are not widely used yet.
 
vijin das
Ranch Hand
Posts: 129
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getHeader

java.lang.String getHeader(java.lang.String name)
Gets the value of the response header with the given name.
If a response header with the given name exists and contains multiple values, the value that was added first will be returned.

This method considers only response headers set or added via setHeader(java.lang.String, java.lang.String), addHeader(java.lang.String, java.lang.String), setDateHeader(java.lang.String, long), addDateHeader(java.lang.String, long), setIntHeader(java.lang.String, int), or addIntHeader(java.lang.String, int), respectively.

Parameters:
name - the name of the response header whose value to return
Returns:
the value of the response header with the given name, or null if no header with the given name has been set on this response
Since:
Servlet 3.0
 
Ra Carter
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That assumes a Servlet 3.0 container, which are not widely used yet.


Yes, I'm using the 2.5 Java EE API where HttpServletResponse.getHeaders is not yet implemented.

It looks like 3.0 is still a draft specification? I don't think I am ready to adopt it yet.

Is there any other less obvious way to read the response headers?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!