File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Thinking about filters, again. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thinking about filters, again." Watch "Thinking about filters, again." New topic
Author

Thinking about filters, again.

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
Shamefully, there is something I don't understand about Servlets filters...
My basic idea of filters :
If the request satifies your requirements, allow it to hit the resource (you know, chain.doFilter), if not, return from the method.
But recently, I found this snippet from Hibernate CaveatEmptor:

How the hell they put another statement after doFilter( ) call ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

doFilter() is a Java method like any other. When it completes, it returns and execution continues from the point of call.

Why should this surprise you?
[ April 04, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

John,
There are three basic patterns for filters.

1) Execute filter before rest of request. Good for restricting access.
// do filter stuff
chain.doFilter(request, response);

2) Execute filter after rest of request. Good for post processing of response.
chain.doFilter(request, response);
// do filter stuff

3) Execute request in between filter. Good for more complex filters like the open source compression filter.
// do filter stuff
chain.doFilter(request, response);
// do more filter stuff

I'm assuming that Hibernate is an example of case 3 where they make sure every request is in a transaction. (What I'm assuming is that they begin the transaction earlier in the filter.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

When it completes

completes here means, after (lets say) JSP had been rendered.
right ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Correct. Remember this is all done in Java code, so all the usual Java rules apply.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Ok, lets say we have two filters :
Filter one :


Filter two :


So, the workflow is as the following :
- print 1
- filter1 chain.doFilter( );
- print 3
- filter2 chain.doFilter( );
- hit the JSP
- print 4
- print 2
Right ?
[ April 04, 2007: Message edited by: John Todd ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thinking about filters, again.