Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems about Filters in Servlets

 
Aashima Arora
Ranch Hand
Posts: 59
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Sheriff
Posts: 21140
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just burst my bubble...true , i dont know what was i thinking before asking such a question
thanks !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic