web.xml作用,以及web容器执行顺序
每个网站一般都有一个web.xml,被用来初始化配置信息,但是又不是必须的。web.xml是web容器首先读取的文件。web容器执行的顺序是:1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。
2、容器创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。
3、容器将<context-param>转换为键值对,并交给servletContext。
4、容器创建<listener>中的类实例,创建监听器。
web.xml中配置执行顺序ServletContext-> context-param ->listener -> filter -> servlet
web.xml中配置的项目内容
- 定义log4j配置文件的加载
- 定义应用程序的初始化
- 定义Spring MVC 配置
- 定义防止spring内存溢出配置
- 定义请求的字符集过滤器
- 定义整个应用程序访问的默认页
- 定义jstl自定义函数文件配置
- 定义验证码加载配置
- 定义加载dwr配置
- 定义session有效期限配置
- 定义自定义默认404、500页面
- 定义应用程序是否支持分布式部署配置
- 定义mybatis配置
- 定义上下文监听器配置
完整的web.xml配置内容
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/spring-content.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!--log4j配置文件加载-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:setup/log4j.xml</param-value>
</context-param>
<!--启动Log4jConfigListener监听器 每60s监听一次log4j.xml配置文件的变化-->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!--log4j.properites加载Log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log/console.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond -->
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- logback配置 -->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:logback.xml</param-value>
</context-param>
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
<!--启动应用程序初始化监听器 ApplicationInitListener-->
<listener>
<listener-class>com.rrtong.frame.ApplicationInitListener</listener-class>
</listener>
<!-- Spring MVC 相关配置 -->
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:setup/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 定义过滤器 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 定义默认页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 自定义jstl标签函数 -->
<jsp-config>
<taglib>
<taglib-uri>http://tags.rrtong.com/pj</taglib-uri>
<taglib-location>/WEB-INF/jstl/custom.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/rrtong</taglib-uri>
<taglib-location>/WEB-INF/jstl/unescape.tld</taglib-location>
</taglib>
</jsp-config>
<!--定义验证码-->
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
<!-- 加载dwr -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<init-param>
<!-- dwr测试页面,上线时设置为false -->
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- dwr拦截 -->
<init-param>
<param-name>org.directwebremoting.extend.Remoter</param-name>
<param-value>com.pinhuba.web.filter.dwrRemoter.DWRRemoter</param-value>
</init-param>
<init-param>
<!-- 防止其他域提交访问 -->
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<!-- 压缩js -->
<init-param>
<param-name>scriptCompressed</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 定义session过期时间 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- 配置自定义错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.html</location>
</error-page>
<!--tomcat集群配置-->
<display-name>rrtong</display-name>
<distributable/>
如果通过java读取param-value,getServletContext().getInitParameter("my_param")方式可以读取到数据。
<context-param>
<param-name>my_param</param-name>
<param-value>hello</param-value>
</context-param>