tomcat生命周期 :web.xml的加载过程是context-param >> listener >> fileter >> servlet
- 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
- 紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
- 容器将<context-param></context-param>转化为键值对,并交给ServletContext.
- 容器创建<listener></listener>中的类实例,即创建监听.
web.xml标签
<context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext*.xml
</param-value>
</context-param>
作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。param-name 设定上下文的参数名称。必须是唯一名称param-value 设定的参数名称的值
<listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
作用:
- 只初始化一次,随web应用的停止而销毁。主要作用是: 做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等 例子表示初始化Spring上下文
常用监听:
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> :spring自动装配application
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>:Spring 刷新Introspector防止内存泄露
<filter>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>xxxxx.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
作用:针对url配置的过滤器类
<session-config>
<session-config>
<session-timeout>2</session-timeout> //2分钟后session失效
</session-config>
- 优先级:Servlet中API设置 > 程序/web.xml设置 > Tomcat/conf/web.xml设置
<error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type> //没有处理此类异常进入
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
作用:系统的最后屏障,对某些异常和异常状态码的最后检测防止系统进入不可预计的页面
<jsp-config>
作用:配置Jsp页面相关信息
- <taglib>:自定义标签库