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?
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).
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..
@ 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??