Servlet.init() and Filter.init() call sequence

前端 未结 4 450
[愿得一人]
[愿得一人] 2021-01-31 16:48

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 Filt

4条回答
  •  无人共我
    2021-01-31 17:23

    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 entries wherein you can specify their priority. They will then be loaded in the priority order.
    E.g.

    
        myServlet
        mypackage.MyServlet
        1
    
    

    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.

提交回复
热议问题