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

Problems about Filters in Servlets

Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59




This is the doFilter method to display header and footer to a servlet. iam not able to understand HOW is the chain.doFilter(request,response) method able to stop the footer from being printed right below the header. what is it actually doing ? because a filter can pass control to a servlet by using it but a servlet doesnt come back to filter?
also what is the exact difference between a chain.doFilter() and the do Filter method with a chain arguement?

Thanks very much :-)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4364
    
    8

Aashima Arora wrote:because a filter can pass control to a servlet by using it but a servlet doesnt come back to filter?


Yes it does - that's the key point. The filter gets the request on the way in. When it calls chain.doFilter(), that passes the request on to the next filter in line, and eventually to the servlet. Then, when the servlet has finished processing, the processing passes back through all the filters (in reverse order).
ramakrishna rayudu
Ranch Hand

Joined: Mar 08, 2011
Posts: 57
Hi...

you should not use the Filter for the Printing purpose.Filter must be used for Session checking and scanning the the incoming the HttpRequest
in very high security websites.
coming to your Question
when your Filter is fired with URL pattren Automaticlly Server will call the doFilter() Method with Required Arguments
Request and Response chain.
you need to some sort of validations after completion successful the request should go to the Original Resource. like this chain.doFilter(request,response);
if not we will redirct to another page in the doFilter Method only.
response.sendRedrict("error.jsp"); like this..

i hope i gave you the answer if not please rise the Query..

Thanks,
Ramakrishna Rayudu
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

@ Matthew : Ohhh !! yes..so by that you mean when the chain.doFilter(..) method is called, the control is passed to the next filter or the servlet and when the servlet has generated a response, it comes back to doFilter(..) method and executes the statements beneath it ? is that what iam supposed to get??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, yeah. That's normally how calling a method works. I can't imagine why you decided that servlets would somehow be different. It's just Java code after all.
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

You just burst my bubble...true , i dont know what was i thinking before asking such a question
thanks !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems about Filters in Servlets