aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFJS: Filters + Wrappers Explaination pg 721 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFJS: Filters + Wrappers Explaination pg 721" Watch "HFJS: Filters + Wrappers Explaination pg 721" New topic
Author

HFJS: Filters + Wrappers Explaination pg 721

Michael Vargenstien
Ranch Hand

Joined: Jan 27, 2007
Posts: 61
Dude,

Lost in the sauce...I do not understand what is going on.

1) The ServletResponseWrapper is a decorator pattern in action...makes sense.

2) The Filters are configured in the DD and a filterImpl must implement the Filter interface contract...makes sense.

3) A request that is to be filtered will be matched agianst url-patterns first, in a chaining order (the order they are declared in) followed by the servlet-name chain...makes sense.

4) The servlet at the end of the chain has no idea about the filters...makes sense.

5) So how does using a custom wrapper object sent on the Filter.doChain(req,customResponseObject) notify the servlet that the incoming request is from a filter?

The only thing I can think of (guessing) is that the HttpServletResponse(response) decoration occuring, somehow says, "Hey, I'm custom and I need you servlet to respond at TheAwesomeFilter.do
Atul Samnerkar
Ranch Hand

Joined: Sep 20, 2003
Posts: 44
1. Filters follow stack processing.
2. The control always comes back to the filter thru which it has passes in the reverse sequence.
3. And being you have wrapped the container's response object, you are not writing directly to the response object but your own custom made response object.
4. So once you come back from the servlet in the filter, then you can take action on what needs to go to response.

So the notification from the servlet to filter always happens whether it is wrapped response or not. It is just a matter that you have wrapped the response hence container didn�t have any chance to write to the actual response object.

I hope, this clarifies your doubt.

Thanks,
Atul Samnerkar
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Michael Vargenstien:
5) So how does using a custom wrapper object sent on the Filter.doChain(req,customResponseObject) notify the servlet that the incoming request is from a filter?

The only thing I can think of (guessing) is that the HttpServletResponse(response) decoration occuring, somehow says, "Hey, I'm custom and I need you servlet to respond at TheAwesomeFilter.do

@5) Looks like you wrapped the response in this case, not the request.

I think you are having a hard time understanding because you're expecting a response wrapper to do something it was never meant to do.

It might become slightly clearer if you read up on the Decorator pattern. Basically, you'd wrap the request or response to change the way the request or response behaves and this happens transparently to the servlets - the servlets have no clue if they're communicating with a true naked request/response or a wrapped version of either one.

You have to look at the APIs of request and response to see what methods you might want to alter the behavior of.

I once had an application where we wanted every single text field to be trimmed so that there would never be leading or trailing space characters. A lot of the fields were number and money values and a value like "20 " with a space wouldn't pass number validation. I could have trimmed every single value at 1000 or so different spots in code but that would have been a nightmare to maintain. All I had to do was wrap the request in a wrapper that overrides the methods for getting parameters, and all those methods would do is trim the values before return them from the method. So the problem was solved with a filter and wrapper and I didn't have to change one line of code in Servlets or any other code.


A good workman is known by his tools.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFJS: Filters + Wrappers Explaination pg 721
 
Similar Threads
Am I too picky or is this question wrong..?
HFSJ Page 696 decalring filters in DD
filter chain
Filter questions
request.getAttribute in onSuccessfulAuthentication method