java开发框架——配置文件web.xml

    xiaoxiao2021-03-25  112

    初始化过程: 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<context-param>。 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。 由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener  >> fileter  >> servlet  如何使用 页面中 ${initParam.contextConfigLocation} Servlet中 String  paramValue =getServletContext().getInitParameter(" contextConfigLocation ")

    以下开始对web.xml文件的解析,具体那部分的解析我都有在前面做相应的博客索引。

    <?xml version="1.0" encoding="UTF-8"?> <web-app> <!-- 编辑器打开显示的名称,与配置无关--> <display-name>Archetype Created Web Application</display-name> <!-- Log4j配置 在同一容器中部署多个应用不能使用默认的webAppRootKey,必须指定唯一KEY,以免冲突 这里如果tomcat存放多个项目的时候,最好写下,防止记录日志的文件写到当前项目的路径中!--> <!--日志配置路径--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> 博客链接:http://blog.csdn.net/happy_wu/article/details/61196291 </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>webName.rootVW</param-value> </context-param> <!-- Spring的log4j监听器 利用spring的监听器! http://www.cnblogs.com/hellojava/archive/2012/12/26/2833840.html--> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 配置监听类,特定监听类监听特定操作,当动作发生时触发一些操作!它可以监听客户端的请求、服务端的操作等。 常用的监听接口有以下几个:  ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。  ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent   sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent   sce)方法。  HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session   Created(HttpSessionEvent   se)方法;当销毁一个Session时,激发sessionDestroyed   (HttpSessionEvent   se)方法。  HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent   se)   方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent   se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent   se)   方法。 listener定义的类可以是自定义的类但必须需要继承ServletContextListener.此处即为自定义监听类。--> <listener> <listener-class> com.app.listener.startListener </listener-class> </listener> <!-- 在web.xml中通过contextConfigLocation配置spring, contextConfigLocation参数定义了要装入的 Spring 配置文件。 如果想装入多个配置文件,可以在 <param-value>标记中用逗号作分隔符。 此处为加载多个以spring-开头的xml文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> 博客链接: http://blog.csdn.net/happy_wu/article/details/61196519 <!--ContextLoaderListener它能捕捉到服务器的启动和停止,在启动和停止触发里面的方法做相应的操作!--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止spring内存溢出监听器,是最重要的一个监听器 http://blog.csdn.net/z69183787/article/details/28402649--> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- 字符集 过滤器 相当于把request和response中的编码都设置成了utf-8 http://blog.csdn.net/gdufzxp/article/details/10077665 http://blog.csdn.net/zdavb/article/details/50886255--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>characterEncoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view分发器 http://www.cnblogs.com/xdp-gacl/p/3760336.html --> <!-- 远程升级配置 必看: http://blog.csdn.net/u013815649/article/details/50435819 http://www.cnblogs.com/hxsyl/p/3435412.html--> <servlet> <servlet-name>Software</servlet-name> <servlet-class>com.app.util.Update</servlet-class> </servlet> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>com.app.filter.EncodingDispatcherServlet </servlet-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Software</servlet-name> <url-pattern>/Software</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> <error-page> <error-code>400</error-code> <location>/400.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> </web-app>
    转载请注明原文地址: https://ju.6miu.com/read-25497.html

    最新回复(0)