zoukankan      html  css  js  c++  java
  • web.xml详解 及 web.xml模板

    web.xml

    web项目运行时,通常会首先加载web.xml文件

    一、web.xml是什么?

    web.xml是web项目的配置文件,一般的web工程都会用到web.xml来配置,主要用来配置Listener,Filter,Servlet等。

    但需要注意的是:web.xml并不是必须的,一个web工程可以没有web.xml文件

    二、web项目加载web.xml过程

    Tomcat启动web项目时,web容器就会首先加载web.xml文件,

    加载过程如下:

    1.web项目加载web.xml,读取context-param和listener这两个结点

    2.创建一个ServletContext(Servlet上下文),整个项目会共享这个ServletContext

    3.容器将<context-param>转换为键值对,并交给ServletContext

    4.容器创建<listener>中的类实例,创建监听器

    三、web.xml 元素详解

    1.首先是表明xml的使用版本。

    <?xml version="1.0" encoding="UTF-8"?>
    

    2.web-app是web.xml的根元素,包含着web.xml所有子元素。

    xmlns以及xmlns:xsi后面引进的连接是表明web.xml引进了模式文件,便能拥有该模式的相关功能。

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    		http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
    
    </web-app>
    

    3.指明项目名称

    <display-name>web-SSMTest</display-name>
    

    4.web项目加载web.xml首先读取这两个结点,加载spring容器及创建spring监听器

    ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    

    5.这个过滤器就是针对于每次浏览器请求进行过滤的【编码过滤器】

     <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>
    

    6.配置DispatcherServlet 前端控制器,加载springMVC容器

     <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置springMVC需要加载的配置文件-->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
      </servlet>
      <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    7.展示首页页面

      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    

    8.session配置

      <session-config>
        <session-timeout>15</session-timeout>
      </session-config>
    

    四、web.xml 示例模板

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <!--配置欢迎界面-->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    
        <!--log4j配置文件地址 -->
        <context-param>
            <param-name>log4jConfiguration</param-name>
            <param-value>classpath:log4j2.xml</param-value>
        </context-param>
    
        <!--Spring配置文件-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!-- Log4j的监听器要放在spring监听器前面 -->
        <listener>
            <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
        </listener>
        <filter>
            <filter-name>log4jServletFilter</filter-name>
            <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>log4jServletFilter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>INCLUDE</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>
    
    
        <!--配置Spring的监听器,加载applicationContext.xml配置文件-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!--设置静态资源不过滤-->
        <!-- 激活Tomcat的defaultServlet来处理静态文件 -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.js</url-pattern>
            <url-pattern>*.css</url-pattern>
            <url-pattern>/css/*"</url-pattern>
            <url-pattern>/scss/*"</url-pattern>
            <url-pattern>/font/*"</url-pattern>
            <url-pattern>/images/*</url-pattern>
            <url-pattern>/js/*</url-pattern>
            <url-pattern>/lay/*</url-pattern>
            <url-pattern>/static/*</url-pattern>
        </servlet-mapping>
    
    
        <!--配置前端控制器 SpringMVC-->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--加载springmvc.xml配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!--启动服务器,创建该servlet-->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    
        <!--解决中文乱码的过滤器-->
        <filter>
            <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
         <!-- session 时间 -->
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
    
    </web-app>
    
  • 相关阅读:
    API Hook完全手册
    ASP.NET页面刷新的实现方法
    ASP.NET验证码
    ASP.NET优化性能的方法
    Asp.net中Server.Transfer,Server.Execute和Response.Redirect的区别
    FireFox新标签页打开搜索和书签
    win10里如何在中文输入法里添加美式键盘
    Sublime Text 3中文乱码问题解决
    Win2008 r2 IIS7.5出现“FastCGI进程最近常常失败。请过一会再尝试此请求”的解决方法
    Sublime Text 3中设置不记住上次打开的文件
  • 原文地址:https://www.cnblogs.com/mytJava/p/13143449.html
Copyright © 2011-2022 走看看