aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A question about Response-side filter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A question about Response-side filter" Watch "A question about Response-side filter" New topic
Author

A question about Response-side filter

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
I know filter can't grab the response form servlet aftr it complete its work.
But after reading HFSJ P685 to P691. I can't understand why it should pass a custom response to chain.doFilter method and why should it make a custom outputStream?
The real code in book is complex so that I can't understand how it work.

Can someone explain it for me?
Thx a lot!


My Way,My Pace
Esam Ahmed
Ranch Hand

Joined: Aug 10, 2005
Posts: 101

I know filter can't grab the response form servlet aftr it complete its work.
But after reading HFSJ P685 to P691. I can't understand why it should pass a custom response to chain.doFilter method and why should it make a custom outputStream?
The real code in book is complex so that I can't understand how it work.

Can someone explain it for me?



You can look at page 672:
At the bottom it said:

Response filters can:

Compress the response stream
Append or alter the response stream
Create a different response altogether



So, if you need to do these things, then you will need to pass a custome response since you know that you can't grab the response....

Does this short description makes it clear?

Esam
[ September 18, 2005: Message edited by: Esam Ahmed ]

Esam<br />SCJP 1.4, SCWCD 1.4
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
If I send original response object to servlet,it return to client directly.But why I send custom response object to servlet,it can't return to client directly?
Why I can grab custom response objcet after servlet complete its works?
B.Sathish
Ranch Hand

Joined: Aug 18, 2005
Posts: 372
There is some extra understanding needed for this topic.

When the original response is passed by the filter to the servlet, the servlet would write to the servlet's output stream. The moment the service() method completes, the container would flush the stream. (The container would call out.flush()). This would send the response to the client. This flushing always happens after the servlet's service() method is done. The container will not wait for any filter. Now, when the control goes back to the filter, the filter WILL have a reference to the actual response (It was having it even before). But if it tries to call any method on the response, an IllegalStateException will be thrown as output stream has already been flushed by the container. This explains why you need to send a custom response.

Coming to you second question - why you need a custom response stream. If you prepare a custom response (by extending ServletResponseWrapper) and pass it to the servlet, without overriding the getOutputStream method to return a custom output stream, when the servlet calls getOutputStream on the custom response object, the custom response object would just delegate the call to the original response object that it wraps (which is how ServletResposeWrapper behaves). So think what will happen - the servlet would actually get the original response stream from the custom response object and write to the original response stream. So you have the same problem again, the container would just flush the stream and the response would be sent to the client. So you would need to override the getOutputStream method and return a custom compression response stream from that. When the servlet calls getOutputStream, it would now get the custom compression respnnse stream and write to it. After the servlet writes to it ,you (not the container) would flush it (from the filter by calling finish). This would send the compressed response to the actual response stream (which the custom response stream wraps), which the container would flush and send to the client
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about Response-side filter