web.xml文件过程
参考:web.xml详细配置
WEB.xml 初始化顺序:ServletContext — context-patam(无顺序) — listener(无顺序) — filter(书写顺序) — servlet(load-on-startup优先级)
在启动Web项目时,容器(如Tomcat)会读web.xml文件中的listener节点和comtext-param节点
容器会创建一个ServletContext(Servlet上下文),整个Web项目共享使用该上下文
将context-param的内容转换为键值对,并交给ServletContext
jsp:${initParam.contextConfigLocation} servlet : String paramValue = getServletContext().getInitParameter("name")
创建listener中类的实例,并创建监听。 — 继承自ServletContextListener接口
写一个properties文件,在文件里写好初始化参数值,在监听器中可以通得到properties文件中的值(写在静态块中)。
初始化方法:contextInitialized(ServletContextEvent event)
初始化方法获得上下文环境中的键值对:event.getServletContext().getInitParameter("name");
—> 通过获得的context-name,就可以进行一些操作。
销毁方法:contextDestroyed(ServletContextEvent event)
当容器启动或终止时,均会触发ServletContextEvent 事件,调用listener进行处理
当容器完成启动(contextInitialized方法完成)后,再对Filter过滤器初始化
servlet初始化:load-on-startup为正数的值越小优先级越高,为负数或未指定的,将在servlet被调用时初始化。
Context-param是application范围内的初始化参数,用于向Servlet-context提供键值对,即应用程序上下文
getServletContext().getInitParameter("name")
Init-param 是servlet范围内的参数,只能在servlet类的init()方法中取得 this.getInitParameter(“param1″)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 ?????????xmlns="http://java.sun.com/xml/ns/javaee" 4 ?????????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" ?5 ?????????id="WebApp_ID" version="3.0"> 6 ?7 <icon>设置标题栏的图标</icon> 8 ?9 <welcome-file-list> ?<!-- 欢迎页面文件 -->10 ??<welcome-file>index.jsp</welcome-file> 11 </welcome-file-list> 12 13 14 ??<context-param> ???15 ??????<param-name>ContextParameter</para-name> ???16 ??????<param-value>test</param-value> ???17 ??????<description>It is a test parameter.</description> ???18 ??</context-param>19 20 <!-- 定制URL,所有.do后缀的url都会交给servlet1的class去处理 -->21 <servlet>22 ????<servlet-name>servlet1</servlet-name>23 ????<servlet-class>net.test.TestServlet</servlet-class>24 <!--初始化参数 - 在TestServlet中调用getServletConfig().getInitParameter("userName")可获得参数值25 ????<init-param>26 ??????????<param-name>userName</param-name>27 ??????????<param-value>Tommy</param-value>28 ????</init-param>29 ????<init-param>30 ??????????<param-name>E-mail</param-name>31 ??????????<param-value>Tommy@163.com</param-value>32 ????</init-param> -->33 </servlet>34 35 <servlet-mapping>36 ????<servlet-name>servlet1</servlet-name>37 ????<url-pattern>*.do</url-pattern>38 </servlet-mapping>39 __________________________________________________________________________________________________40 <error-page> <!-- 错误页面404文件 -->41 ????<error-code>404</error-code>42 ????<location>/error404.jsp</location>43 </error-page>44 45 <error-page>46 ????<exception-type>java.lang.Exception<exception-type>47 ????<location>/exception.jsp<location>48 </error-page>49 <error-page> ?50 ??????<exception-type>java.lang.NullException</exception-type> ?51 ??????<location>/error.jsp</location> ?52 </error-page> 53 __________________________________________________________________________________________________54 <!-- 设置过滤器 -->55 <filter> 56 ????<filter-name>XXXCharaSetFilter</filter-name> 57 ????<filter-class>net.test.CharSetFilter</filter-class> 58 </filter> 59 <filter-mapping> 60 ????<filter-name>XXXCharaSetFilter</filter-name> 61 ????<url-pattern>/*</url-pattern> 62 </filter-mapping> 63 64 <!-- 设置监听器 -->65 <listener> 66 <listener-class>net.test.XXXLisenet</listener-class> 67 </listener> 68 69 <!-- 设置会话session的过期时间 默认30分钟 -->70 <session-config> 71 <session-timeout>60</session-timeout> 72 </session-config>
web.xml文件解析
原文地址:https://www.cnblogs.com/kiqi/p/10454483.html