wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question about servlet filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question about servlet filters" Watch "Question about servlet filters" New topic
Author

Question about servlet filters

Holger Brade
Greenhorn

Joined: Apr 14, 2008
Posts: 8
Hello Friends,

I am confused with one question in the Head First Book (Servlets and JSP) about Filters (chapter 13 page 728).
"A filter can act on only the request or response object, not both." This question is not true.

Technically the answer is correct, it will compile and "work", but I can not imagine any case where the request and the
response filter will be the same class or can you give me an example?

The main different between request and response filter are the method call chain.doFilter(request, response).
The request filter calls the chain at the beginning and the response at the end within the doFilter method. Furthermore
the response filter uses a wrapped response object, otherwise we will not get the response back. So it depends
on the kind of filter how I will implement the filter class.

Thanks in advance

Holger


SCJP, Preparing for SCWCD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

There is no distinction between request and response filter actually. Where you call the filter.doChain method is totally up to you. You are not forced to call doChain at the beginning or end of doFilter method. You can implement a filter which does some processing before and after the call to doChain method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Holger Brade
Greenhorn

Joined: Apr 14, 2008
Posts: 8
Thanks Ankit.

so far I understand it does not matter where I put the method.
But can you give me an example where a filter can be used as request and response filter?

For me the question in the book suggest that I can write a filter and it can be used as request and
response filter.

I made as an example a filter which outputs the http request and the response header (I know it is not
such a good example, because you have to differ between Request and Response Object, but I can only
imagine examples where I get always two different filter classes) . To output
both headers, I need two classes one for the request and one for the response header.

The request filter:

and the response filter:

Is it possible for this example (or a similar example) to have only one filter? If not I would answer for the question "A filter can act on only the request or response object, not both." yes and this would be wrong .

Thanks in advance

Holger


Holger Brade
Greenhorn

Joined: Apr 14, 2008
Posts: 8
ok I understand it. Code concerning the request has to be before chain.doFilter() and code for the response after the method.
The response must be wrapped in this case. The example would be the following:


Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Lets take a very simple example filter. I want to log the actual time taken by the service method to respond to a request. So for that what I'll have to do is this



[Edit: Late with the reply ]
Holger Brade
Greenhorn

Joined: Apr 14, 2008
Posts: 8
Thank you very much Ankit.

In the book they always separate response and request filter. At the end the solution was quite simple.
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
Thanks for the excellent exchange! I'm not yet to the filters chapter, but feel well-prepared after reading this.


SCJP 5, SCWCD 5
 
Consider Paul's rocket mass heater.
 
subject: Question about servlet filters
 
Similar Threads
Doubt on Filters
Confusion regarding filters (HFSJ)
Confused about doFilter() - Filters and Wrappers Mock in HFSJ
Filters
Filter Question