aspose file tools*
The moose likes Servlets and the fly likes Response Headers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Response Headers" Watch "Response Headers" New topic
Author

Response Headers

J.J. Jackson
Greenhorn

Joined: Jun 05, 2007
Posts: 9
HttpServletResponse, and its parent, ServletResponse, define several methods for setting response meta-data like content-length, header values, etc.

Surprisingly, at least to me, there are no methods for reading these values. (Servlet API 2.4 introduced getContentType(), but that's about it.)

I suppose many people won't need to do this, but if you wanted to write a Servlet filter that compressed all content bigger than 2K, you'd miss not having a getContentLength() method. Or, if you wanted to log all redirects and their messages, it would useful to be able to know if the SC_FOUND error code was set and to be able to extract the message.

I've tried using the response.toString() output, but it's implementation is non-standard and not always consistent between requests.

I feel like I must be missing something obvious. How can a Servlet Filter writer access the response meta-data?

Thanks.

JJ
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

That's where response wrappers can come in handy.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J.J. Jackson
Greenhorn

Joined: Jun 05, 2007
Posts: 9
Would you please elaborate on the use of the (Http)ServletResponse wrapper a bit to get at the response headers?

I understand the response wrapper would be useful if I wanted to override the ServletResponse getOutputStream to do compression, but there are no extra accessor methods to get at the ServeltResponse meta-data; i.e. no methods for getting a response header value.

Here's a concrete example of what I want to do:

Suppose a servlet invokes HttpServletResponse's setStatus() method, passing in HttpServletResponse.SC_ACCEPTED. This results in the HTTP STATUS header being set to 202.

Now, in the filter, I want to query the response as to its header STATUS code. If it's a 202, I want some action to occur. How can I query the HttpServletResponse object to get the HTTP response STATUS header value?

Thanks for your response!

JJ
[ July 05, 2008: Message edited by: J.J. Jackson ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you wrap the response and override setHeader with your own method that stores the header in a map, you can then write your own getHeader method that reads that header.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
J.J. Jackson
Greenhorn

Joined: Jun 05, 2007
Posts: 9
That will work.

Thanks!

JJ
pallu adhav
Greenhorn

Joined: Sep 21, 2008
Posts: 1
Can you please explain how you did it?
Please reply.It is very urgent.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi pallu adhav welcome to Javaranch
Please reply.It is very urgent.

If its really very urgent, then read this .

Also take some time to read the ask good questions link below.


SCJP, SCWCD.
|Asking Good Questions|
Erik Westland
Greenhorn

Joined: Dec 04, 2008
Posts: 1
As described above, I use the following in a servlet filter before invoking the rest of the chain (e.g. chain.doFilter(req, myWrappedResp)) to retrieve values in the response.



Note: This doesn't always appear to operate as I expect. For example, I don't believe I've had setHeader() or setContentLength() called in my simple tests. It will probably make sense in the morning, but if there are any obvious flaws, I would be appreciate the pointers.

Cheers,
Erik
Peter Jin
Greenhorn

Joined: Jun 11, 2009
Posts: 1
seems a topic long ago. However, I just tried the wrapper and got the following results. Hope somebody knows the cause.
The the setHeader and setContentLength do get called. I got results correctly. But the following two headers are missing. Were they set before I create the wrapper? How to catch those then?

Server: Apache-Coyote/1.1
Date: Thu, 11 Jun 2009 07:52:33 GMT

In addition, the following two were not caught either

Content-Type: text/html
Content-Length: 636
Soonthorn Ati
Greenhorn

Joined: Nov 17, 2009
Posts: 2
Hi Peter and et.,

I'm having the same issue. Are you able to figure it out how to intercept "Server" and "Date"'s JBOSS HTTP response headers? My requirement is to remove "Server" header and reformat "Date" header.

Any help would be really appreciated.

Thank you,
Soonthorn A.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
Soonthorn, welcome to JavaRanch.

Have you tried the wrapper approach discussed above? Do the overridden methods get called? Be careful about reformatting the Date header - it has a standardized format described by the HTTP specification, and clients (such as browsers) will expect the format to be exactly like that.

What do you hope to achieve by changing the date format?


Ping & DNS - my free Android networking tools app
Soonthorn Ati
Greenhorn

Joined: Nov 17, 2009
Posts: 2
Ulf Dittmer wrote:Soonthorn, welcome to JavaRanch.

Have you tried the wrapper approach discussed above? Do the overridden methods get called? Be careful about reformatting the Date header - it has a standardized format described by the HTTP specification, and clients (such as browsers) will expect the format to be exactly like that.

What do you hope to achieve by changing the date format?



Thanks Ulf,

First, I should mention that I run this on JBOSS 5.1

Yes, I have tried to implement javax.servlet.Filter by passing my own HttpServletResponseWrapper to filterChange.doFilter(). My HttpServletResponseWrapper override setHeader( ), setDateHeader( ) and other methods. However, I don't see any code call them to set those headers ("Date" and etc). I'm afraid that JBOSS may set the Date header later outside Filter.

The reason I need to change date format is that our client (customer ) expects different date format. Basically, our customer just uses HTTP to carry their on message/data, but their uses some headers that may not follow what exactly described in HTTP specification (e.g. Date). And we want to develop our application in JBoss environment instead of writing our own custom HTTP server codes.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
JBoss is a spec-compliant servlet container - if the filter is configured and written correctly, then it should be called. Do you have evidence that the filter class is used at all? For example, does the init method get called?
 
jQuery in Action, 2nd edition
 
subject: Response Headers