In which order are Servlet.init() and Filter.init() methods called in java web application? Which one is called first? Are all Servlet.init() methods called before than any Filter.doFilter method?
4 Answers
The filters are always initialized during webapp's startup in the order as they are defined in the web.xml
.
The servlets are by default initialized during the first HTTP request on their url-pattern only. But you can configure them as well to initialize during webapp's startup using the <load-on-startup>
entries wherein you can specify their priority. They will then be loaded in the priority order.
E.g.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>mypackage.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
If there are more servlets with the same priority order, then the loading order for those servlets is unspecified and may be arbitrary. Servlets are however in any way initialized after the initialization of filters, but before invocation of the filters.

- 3,677
- 3
- 17
- 40

- 1,082,665
- 372
- 3,610
- 3,555
-
1If I have set load-on-startup in web.xml, can I be certain that this servlet's init() is called before any filter's doFilter? – martsraits May 25 '10 at 16:30
-
Yes, I edited that in. This is however regardless of `load-on-startup`. – BalusC May 25 '10 at 16:31
-
1@kukudas: You're welcome. This related answer may be helpful as well: http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909 – BalusC Apr 12 '11 at 14:45
-
@BalusC: The order of filter initiliazation doesn't depend on the order defined in the web.xml as Jan Gutvirth pointed out - tested on Apache Tomcat 6.0.37 – Kroky Aug 25 '14 at 23:02
-
Same comment as @Kroky -- see the important answer below: https://stackoverflow.com/a/16237835/385667 – merryreaper Oct 06 '17 at 22:08
- For all filters:
Filter.init()
- For all servlets with
<load-on-startup>
inweb.xml
:Servlet.init()
- For all applicable filters for request:
Filter.doFilter()
- If applicable servlet not already initialised:
Servlet.init()
- For applicable servlet:
Servlet.service()

- 4,418
- 3
- 25
- 45

- 25,562
- 6
- 51
- 57
-
1This contradicts BalusC's answer, which would have your (4) happen before (3). – Robert Tupelo-Schneck Sep 06 '12 at 20:20
-
(2) is for servlets that have load-on-startup in the web.xml. (4) is for servlets that are initialized on first request to the servlet. – downeyt Jul 14 '15 at 23:50
Just a side note - I experienced on tomcat (7.0.30) that the Filter.init() methods are run in random order (iteration over HashMap).

- 1,158
- 1
- 9
- 3
-
-
Yes I know, but I can't comment as I don't have enough reputation. But I thought it worth mentioning that the accepted answer seems to be wrong in this aspect (that is the order of calling Filter.init() method is generally undefined) – Jan Gutvirth Apr 29 '13 at 13:45
Beware. I've been witnessing concurrent invocation of Filter.init() and Filter.doFilter() on the same instance. I'm still shocked and can't recover. Its' name is Jetty.

- 203
- 2
- 5