web.xml配置文件详解
2016-12-23 10:55
429 查看
启动一个WEB项目时,WEB容器首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。
例子:
关于schema
web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素中,都必须标明这个 web.xml使用的是哪个模式文件。其它的元素都放在之中。
Listener的配置
为web应用程序定义监听器,监听器用来监听各种事件,比如:application和session事件,所有的监听器按照相同的方式定义,功能取决去它们各自实现的接口,常用的Web事件接口有如下几个:
ServletContextListener:用于监听Web应用的启动和关闭;
ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变;
ServletRequestListener:用于监听用户的请求;
ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变;
HttpSessionListener:用于监听用户session的开始和结束;
HttpSessionAttributeListener:用于监听HttpSession范围(session)内属性的改变。
主要用于监听Web应用事件,其中有两个比较重要的WEB应用事件:应用的启动和停止(starting up or shutting down)和Session的创建和失效(created or destroyed)。应用启动事件发生在应用第一次被Servlet容器装载和启动的时候;停止事件发生在Web应用停止的时候。Session创建事件发生在每次一个新的session创建的时候,类似地Session失效事件发生在每次一个Session失效的时候。为了使用这些Web应用事件做些有用的事情,我们必须创建和使用一些特殊的“监听类”。它们是实现了以下两个接口中任何一个接口的简单java类:javax.servlet.ServletContextListener或javax.servlet.http.HttpSessionListener,如果想让你的类监听应用的启动和停止事件,你就得实现ServletContextListener接口;想让你的类去监听Session的创建和失效事件,那你就得实现HttpSessionListener接口。
例子:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- session-config 配置,单位为分钟,会话超时配置项--> <session-config> <session-timeout>300</session-timeout> </session-config> <!-- error-page 错误页面配置项。当系统发生错误时,跳转到错误处理页面。 --> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <!-- 还可以通过异常的类型配置error-page。当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面。 --> <error-page> <exception-type>java.lang.NullException</exception-type> <location>/error.jsp</location> </error-page> <!-- log4j_satrt --> <!-- 其中log4jRefreshInterval是能够动态修改log4j.properties的关键,WEB容器会每60秒扫描log4j的配置文件。--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- 有一点需要注意,就是如果用RollingFileAppender或者是FileAppender时,可以通过${webapp.root}来定位到服务器的发布的该项目下,这是spring把web目录的路径压入到了webap.root的系统变量。然后,在log4j.properties里就可以这样定义logfile位置log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log 如果有多个web应用,怕webapp.root变量重复,可以在context-param里定义webAppRootKey --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- log4j_end --> <!-- 编码过滤器--> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 登录检查过滤器_satrt--> <filter> <filter-name>loginFilter</filter-name> <filter-class>com.keymen.web.filter.OperatorLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- 登录检查过滤器_end--> <!-- Spring 配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*Context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener> <listener-class>com.keymen.listener.ApplicationInitListener</listener-class> </listener> <listener> <listener-class>com.keymen.listener.SessionCreateListener</listener-class> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Filter可认为是Servle的一种“加强版”,主要用于对用户请求request进行预处理,也可以对Response进行后处理,是个典型的处理链。使用Filter的完整流程是:Filter对用户请求进行预处理,接着将请求HttpServletRequest交给Servlet进行处理并生成响应,最后Filter再对服务器响应HttpServletResponse进行后处理。Filter与Servlet具有完全相同的生命周期,且Filter也可以通过<init-param>来配置初始化参数,获取Filter的初始化参数则使用FilterConfig的getInitParameter()。换种说法,Servlet里有request和response两个对象,Filter能够在一个request到达Servlet之前预处理request,也可以在离开Servlet时处理response,Filter其实是一个Servlet链。 --> <!-- 通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts2框架而言,需要加载FilterDispatcher,只要Web应用负责加载FilterDispatcher,FilterDispatcher将会加载Struts2框架。因为Struts2将核心控制器设计成Filter,而不是一个普通Servlet。故为了让Web应用加载FilterDispatcher,只需要在web.xml文件中配置FilterDispatcher即可。 --> <!-- struts2拦截器 --> <filter> <!-- 配置Struts2核心Filter的名字 --> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <!-- 配置Filter拦截的URL --> <filter-mapping> <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 验证码 --> <servlet> <servlet-name>verifycode</servlet-name> <servlet-class>com.keymen.util.image.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>verifycode</servlet-name> <url-pattern>/verifycode.gif</url-pattern> </servlet-mapping> <!-- 首页欢迎页配置 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
关于schema
web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素中,都必须标明这个 web.xml使用的是哪个模式文件。其它的元素都放在之中。
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
Listener的配置
为web应用程序定义监听器,监听器用来监听各种事件,比如:application和session事件,所有的监听器按照相同的方式定义,功能取决去它们各自实现的接口,常用的Web事件接口有如下几个:
ServletContextListener:用于监听Web应用的启动和关闭;
ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变;
ServletRequestListener:用于监听用户的请求;
ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变;
HttpSessionListener:用于监听用户session的开始和结束;
HttpSessionAttributeListener:用于监听HttpSession范围(session)内属性的改变。
主要用于监听Web应用事件,其中有两个比较重要的WEB应用事件:应用的启动和停止(starting up or shutting down)和Session的创建和失效(created or destroyed)。应用启动事件发生在应用第一次被Servlet容器装载和启动的时候;停止事件发生在Web应用停止的时候。Session创建事件发生在每次一个新的session创建的时候,类似地Session失效事件发生在每次一个Session失效的时候。为了使用这些Web应用事件做些有用的事情,我们必须创建和使用一些特殊的“监听类”。它们是实现了以下两个接口中任何一个接口的简单java类:javax.servlet.ServletContextListener或javax.servlet.http.HttpSessionListener,如果想让你的类监听应用的启动和停止事件,你就得实现ServletContextListener接口;想让你的类去监听Session的创建和失效事件,那你就得实现HttpSessionListener接口。
相关文章推荐
- web.xml配置文件详解(转)
- Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标签的作用。
- JSP中Web.xml配置文件详解
- web.xml配置文件详解
- web.xml配置文件详解
- web.xml配置文件详解(转)
- 关于JSP配置文件web.xml加载顺序详解
- web.xml配置文件详解
- web.xml文件配置详解
- j2ee项目开发web.xml文件加载及配置详解
- web.xml配置文件详解
- web.xml 配置文件详解
- JSP中Web.xml配置文件详解
- JSP配置文件web.xml加载顺序详解
- web.xml配置文件详解(转)
- Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标签的作用
- [转]Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标签的作用。
- web.xml配置文件详解(转)
- web.xml配置文件详解
- Web.xml 配置文件详解