Servlet.init()和Filter.init()调用序列 [英] Servlet.init() and Filter.init() call sequence

查看:100
本文介绍了Servlet.init()和Filter.init()调用序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java Web应用程序中调用的Servlet.init()和Filter.init()方法的顺序是什么?首先叫哪一个?是否所有Servlet.init()方法都被调用,而不是任何Filter.doFilter方法?

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?

推荐答案

过滤器始终在webapp启动期间初始化按照 web.xml 中定义的顺序排列。

The filters are always initialized during webapp's startup in the order as they are defined in the web.xml.

默认情况下,servlet在其url-pattern上的第一个HTTP请求期间初始化。但您也可以使用< load-on-startup> 条目在webapp启动期间对其进行初始化,您可以在其中指定其优先级。然后它们将按优先顺序加载。

例如。

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>

如果有更多具有相同优先级顺序的servlet,那么这些servlet的加载顺序是未指定的,可能是任意的。但是,在初始化过滤器之后,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.

这篇关于Servlet.init()和Filter.init()调用序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆