Filter order in servlets.

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, In the previous article we learned about filters in servlets. This part will deal about the order of execution of filters.

As per previous tutorial, Create

  • HelloServlet :- Our resource that will be requested
  • LoggerFilter :- Just a simple filter that will be applied to HelloServlet
  • AuthFilter :- Just another filter that will be applied to HelloServlet.

HelloServlet.

 LoggerFilter

 AuthFilter:

 Web.xml configuration.

Now when you directly access the HelloServlet using url  http://localhost:8080/ServletsJSP/HelloServlet , you will see the following output in eclipse console.

 What is happening here?

We see that both filters are called. But what decides the filter order? Who knows about which filters are to be placed in sequence? What is the significance of FilterChain in filter order?

ServletFilter

 

  • In web.xml , we have declared two filters i.e. LoggerFilter and AuthFilter
  • LoggerFilter comes first in  web.xml
  • AuthFilter is declared after LoggerFilter .
  • When filters are registered, they are registered according to the order specified in web.xml.
  • FilterChain object knows what filter or resource to be called when doFilter method is called from a Filter.
  • So in this case, as AuthFilter comes after LoggerFilter , AuthFilter is called after LoggerFilter .
  • After AuthFilter is called, HelloServlet  is called and when its doGet method completes, again control goes back to AuthFilter to complete the execution.
  • Again after AuthFilter is completed, LoggerFilter is called.

Now what if we want to change the filter order so that AuthFilter is called first and then LoggerFilter , we simply need to change the filter order in web.xml and FilterChain object will do the rest of the work.

So just change the web.xml filter order  as follows:

and hit the servlet url and you will see the filter order has changed with the following output.

Hope this article helps understand the filter order in servlets.

Share Button

12 comments for “Filter order in servlets.

  1. December 19, 2013 at 12:18 pm

    A question here.. Order in which filters are executed depends on the filter declaration order or the order in which they are registered with servltet?
    what if Auth and Logging filer are consecutively declared and then registered in some different order? something like this :

    AuthFilter
    AuthFilter
    com.thejavageek.filters.AuthFilter

    LoggerFilter
    LoggerFilter
    com.thejavageek.filters.LoggerFilter

    LoggerFilter
    HelloServlet

    AuthFilter
    HelloServlet

    or is it that tag must follow tag?

    • December 19, 2013 at 12:20 pm

      correcting the last line:
      is it that the filter-mapping tag must follow filter tag?

      • Prasad Kharkar
        December 21, 2013 at 12:51 am

        HI Supriya, Its not that filter-mapping must follow filter tag. You can notice that the filters are mapped to HelloServlet in the order LoggerServlet and AuthServlet. So LoggerServlet will be called first.
        If you reverse the order of filter mapping for HelloServlet ,


        AuthFilter
        HelloServlet


        LoggerFilter
        HelloServlet

        Then you will get the output as

        auth filter Before visiting servlet
        logger filter Before visiting servlet
        we are in doGet method
        logger filter After visiting servlet
        auth filter After visiting servlet

  2. Pranaya Ranjan
    January 4, 2014 at 1:24 am

    Nice Tutorial !!

  3. September 5, 2014 at 10:46 am

    Hi Prasad Kharkar!
    If you don’t mind here is my blog post that gives a spam filter illustrating a very practical use of Java servlets filters http://fivesnippets.blogspot.com/2014/08/servlet-filter-for-ddos-spam-etc.html

    • September 5, 2014 at 2:22 pm

      Hi abde, your link to this article has helped me think about the servlet filters in a new way 🙂 Its really good use of filters.

      Regards,
      Prasad Kharkar

  4. viral
    March 16, 2015 at 11:48 pm

    Hi Prasad,

    I am waiting of your tutorial about hibernate and spring. I am really appreciate of your work. keep it up.

    Would you tell me date when you would publish article about Spring and Hibernate.

    • March 17, 2015 at 10:38 pm

      Hi Viral, I am glad you like my work. Right now, my work schedule does not allow me to write articles. I will start writing again next month. Meanwhile, let me know what you want and I’ll try to do it.

  5. surya
    February 9, 2016 at 2:32 pm

    plzzz upload spring tutorials, prasad ji u r explanation is simply superb…

  6. zineb
    February 23, 2016 at 8:27 pm

    Hello,

    stupid question
    it’s possible that the servlet can be called before a filter ?

    thanks

    • Rohan
      March 15, 2016 at 11:26 am

      Every filter is called both before and after the servlet.

  7. May 30, 2017 at 6:15 pm

    very useful information and please keep updating us, …..

Leave a Reply

Your email address will not be published. Required fields are marked *