• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the purpose of chain.doFilter()

 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Try to read the specifications and some articles on Filters. However, I still cannot really fully understand what is the purpose of "chain.doFilter()" except that it can examines/customizes the request/response etc.
For instance, I review the Filter examples included in Tomcat 4 and realise that the statement "chain.doFilter()" can appear at the top, middle or bottom of method and I have no idea why is this so.
 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, to make things a bit clearer, let's consider the following example
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
throws IOException, ServletException {
/**** One ***/
// Store ourselves as a request attribute (if requested)
if (attribute != null)
request.setAttribute(attribute, this);

/**** Two ***/
// Time and log the subsequent processing
long startTime = System.currentTimeMillis();
/**** Three ***/
long stopTime = System.currentTimeMillis();
filterConfig.getServletContext().log
(this.toString() + ": " + (stopTime - startTime) +
" milliseconds");
/*** Four ***/
}
What's the difference if we place "chain.doFilter(request, response);" at position One, Two, Three or Four.
I tried to run the example in Tomcat 4 but it was unsuccessfully as I suspect there are some config problems in my Tomcat 4 installation. Previously I can run the jsp/servlets examples but not now.
 
Gaurav Chugh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there ,
Is it in the objective of SCWCD , i think filter are not in the objective , if yes i did not touch this topic.Was there any question on Filter.plz let me know.
Thanks in advance

------------------
with warm regards
..gaurav
 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of whether filter is included in certification exams or not, I would want to clear my doubts. For me, it's more important to understand than to pass the exams.
After much reading and thinking again, am I right to say that the position to put chain.doFilter() depends on when you would like to pass the control to the next entity ?
Thanks in advance.
 
Rohit Poddar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kodo,
You are right that chain.doFilter() calls the next entity (next filter or the resource requested originally) and then the control returns to the statement next to chain.doFilter() and the execution continues.
Therefore, where you call doFilter() (one, two or three) will make a difference as to what time you see in your loggin.
Hope that helps.
Rohit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic