Hello; I am going through the Sun Core J2EE design pattern book second edition and noticed that custom servlet filters cannot wrap the request/response object (Page 153) but standard servlet filters can.
could someone please explain why???
I would think any filter should be able to wrap the requst/response.
I am fairly certain that it has something to do with the the front controller calling the filters(fig 6.4)
[ December 27, 2006: Message edited by: peter cooke ] [ December 27, 2006: Message edited by: peter cooke ]
When answering, I assumed (without even thinking about it!) that what Peter said was : 1. Using filters in web.xml, where the doFilter receives both request and response 2. Using wrappers like HttpServletXXXXXWrapper which can only be used for either response or request.
I understand Bear's puzzlement, as number 2 is not a filter.
Joined: Mar 16, 2004
Originally posted by Bear Bibeault: Filter manager? You are speaking of some proprietary system?
from the core j2ee patterns book it the filter manager is part of the filter pattern. It any one implementing servlet filters appaently must provide some class that implements the functionality of the filter manager.
Since the declarative servlet filters can be selected and sequenced by URL patterns. Something must decide what filters are going to be run for a particular request and what order they will be run. Therefore some class must must be provided by the appserver that fullfills the role of filter manager.
I can say in filter mapping for URL ...../servlet1 run filter1 folled by filter2. I can also say for URL ..../servlet2 run filter2 follwed by filter3 then filter1
I may be wrong, sevlet filter pattern appears to be a suped up combination of the Decorator and Chain of Responsibility patterns. The filter itself is the decorator, and one of the concerete handlers. The Filter manager is something that arranges the the sequence of concerete handlers.
So this is a system whipped up by the author just for the book? You could have said so in the first place.
On that case, you'll need to see if the author posts anywhere online to ask him why he designed it that way.
Joined: Mar 16, 2004
Ahhhhh to get back on topic.
If someone can point me to where I can post my origional question below
Given that Standard Servlet Filters (delarative)can wrap http requsets and reposones for any J2EE appserver. Why can't custom servlet filters (programatic choice of filter) wrap Http Servlet Requests and reposnses?
Originally posted by peter cooke: Why can't custom servlet filters (programatic choice of filter) wrap Http Servlet Requests and reposnses?
Again, these "custom servlet filters" of which you speak are a figment of an author's imagination. No one but the author will be able to tell you why he designed his "filter manager" that way.
By the way, is this an older book written before true servlet filters were added to the Specification? That's the only reason I can think of to concoct a "custom" filter sub-system. [ December 28, 2006: Message edited by: Bear Bibeault ]
Joined: Mar 16, 2004
The book (ISBN 0-13-142246-4 )is copywrited 2003. But it accurately shows how the declarative servlet filers work for modern app servers.
I have the second edition of that book and looked up this pattern.
Implement the Filter with your own custom strategy. This is less flexible and less powerful than the preferred Standard Filter strategy that is available in containers supporting the 2.3 servlet specification and later ...
From the quote, it looks like this pattern was a workaround for containers that didn't yet support declarative filters; a "Poor Man's Filter".
Without reading too far into it, it doesn't look like the book suggests any benefits to using this pattern in a modern container where declarative filters are available.
To try answer your question.. Without the injection points along the request/response chain that are exposed to declarative filters, it doesn't seem possible to wrap the request and response objects; at least not at such a deep level. Declarative filters have access to these objects before they get passed to any servlets and after they have been forwarded to the last servlet in the app. I don't see how you could do that with a home rolled solution.
Originally posted by peter cooke: Hello; I am going through the Sun Core J2EE design pattern book second edition and noticed that custom servlet filters cannot wrap the request/response object
I think some of the confusion in this thread goes back to the terminology used here. There is no mention in the servlet spec of "custom filters" or "standard filters". This book appears to be showing the readers one solution for solving a problem with containers that don't support filters. It then goes on to let the readers know that a better alternative is available in newer containers. In my copy, they were even careful to print the term Standard Filter in italics.