wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet filter and request/resposnse wrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet filter and request/resposnse wrapper" Watch "servlet filter and request/resposnse wrapper" New topic
Author

servlet filter and request/resposnse wrapper

peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
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 ]

CIAO Peter M. Cooke
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think it has something to do with "philosophy"
Wrappers are usually used to wrap ONE thing. It's like a Decorator pattern which maintains a reference to the interface it wraps.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

I do not know what you mean by standard vs. custom filters.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
Standard servlet filters are declared in the web.xml

Custom servlet filters are programatically added to filter manager by the servlet being invoked.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Filter manager? You are speaking of some proprietary system?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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.
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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 ]
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
The book (ISBN 0-13-142246-4 )is copywrited 2003.
But it accurately shows how the declarative servlet filers work for modern app servers.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet filter and request/resposnse wrapper
 
Similar Threads
Please answer the following HF question (Page 703) on filters
throwing exceptions
Servlet auto redirect to another page
Servlet Chaining
jsp to xml