zoukankan      html  css  js  c++  java
  • web.xml文件解析

    web.xml文件过程

    参考:web.xml详细配置

    WEB.xml 初始化顺序:ServletContext — context-param(无顺序) — listener(书写顺序) — filter(书写顺序) — servlet(load-on-startup优先级)

    1. 在启动Web项目时,容器(如Tomcat)会读web.xml文件中的listener节点comtext-param节点

    2. 容器会创建一个ServletContext(Servlet上下文),整个Web项目共享使用该上下文

    3. context-param的内容转换为键值对,并交给ServletContext 

      jsp:${initParam.contextConfigLocation} 
      servlet : String paramValue = getServletContext().getInitParameter("name")
    4. 创建listener中类的实例,并创建监听。 — 继承自ServletContextListener接口

      • 写一个properties文件,在文件里写好初始化参数值,在监听器中可以通得到properties文件中的值(写在静态块中)。 
        监听器初始化方法:contextInitialized(ServletContextEvent event) 
        初始化方法中获得上下文环境中的键值对:event.getServletContext().getInitParameter("name"); 
        —> 通过获得的context-name,就可以进行一些操作。 
        销毁方法:contextDestroyed(ServletContextEvent event)

    5. 当容器启动或终止时,均会触发ServletContextEvent 事件,调用listener进行处理

    6. 当容器完成启动(contextInitialized方法完成)后,再对Filter过滤器初始化

    7. 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>设置使用GUI工具时,用于指定web应用的图标</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>

    欢迎疑问、期待评论、感谢指点 -- kiqi,愿同您为友

    -- 星河有灿灿,愿与之辉

  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/kiqi/p/10454483.html
Copyright © 2011-2022 走看看