• web.xml文件配置说明


    web.xml作用:

    web.xml主要用来配置Filter、Listener、Servlet等,当我们去启动一个WEB项目时,容器(jetty、tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。

    web.xml配置元素的加载顺序:

    <context-param> -> <listener> -> <filter> -> <servlet>。其中,如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载。

    web容器启动过程:

    1. 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。 
    2. 紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。 
    3. 容器将<context-param>转换为键值对,并交给servletContext。 
    4. 容器创建<listener>中的类实例,创建监听器。

     

    web.xml配置元素:

    1.<web-app>根元素

        web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素<web-app>中,都必须标明这个 web.xml使用的是哪个模式文件。其它的元素都放在<web-app></web-app>之中,<web-app>是根节点。
    <web-app version="3.0" 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_3_0.xsd">
    </web-app>

    2.<icon>Web应用图标

        指出IDE和GUI工具用来表示Web应用的大图标和小图标。
    <icon>
      <small-icon>/images/app_small.gif</small-icon>
      <large-icon>/images/app_large.gif</large-icon>
    </icon>

    3.<display-name>Web应用名称

        提供GUI工具可能会用来标记这个特定的Web应用的一个名称。
    <display-name>Tomcat Example</display-name>

    4.<disciption>Web应用描述

        给出于此相关的说明性文本。
    <disciption>Tomcat Example servlets and JSP pages.</disciption>

    5.<context-param>上下文参数

        声明应用范围内的初始化参数。它用于向 ServletContext提供键值对,即应用程序上下文信息。我们的listener, filter等在初始化时会用到这些上下文中的信息。在servlet里面可以通过getServletContext().getInitParameter("context/param")得到。
    <context-param>
      <param-name>ContextParameter</para-name>
      <param-value>test</param-value>
      <description>It is a test parameter.</description>
    </context-param>

    6.<filter>过滤器

        将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。
    <filter>
      <filter-name>setCharacterEncoding</filter-name>
      <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>setCharacterEncoding</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    7.<listener>监听器

    <listener>
      <listener-class>com.listener.SessionListener</listener-class>
    </listener>

    8.<servlet>

    • <servlet> 用来声明一个servlet的数据,主要有以下子元素:
    • <servlet-name> 指定servlet的名称
    • <servlet-class> 指定servlet的类名称
    • <jsp-file> 指定web站台中的某个JSP网页的完整路径
    • <init-param> 用来定义参数,可有多个init-param。
    • <load-on-startup> 当值为正数或零时,从小到大加载。否则第一次访问时加载。
    • <servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素
    • <servlet-name> 指定servlet的名称
    • <url-pattern> 指定servlet所对应的URL 
    <servlet>
        <servlet-name>snoop</servlet-name>
        <servlet-class>SnoopServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>snoop</servlet-name>
        <url-pattern>/snoop</url-pattern>
    </servlet-mapping>

     9.<session-config>会话超时配置

    <session-config>
      <session-timeout>120</session-timeout><!-- 单位为分钟 -->
    </session-config>

    10.<mime-mapping>

    <mime-mapping>
      <extension>htm</extension>
      <mime-type>text/html</mime-type>
    </mime-mapping>

    11.<welcome-file-list>欢迎文件页

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

    12.<error-page>错误页面

    <!-- 1、通过错误码来配置error-page。当系统发生×××错误时,跳转到错误处理页面。 -->
    <error-page>
      <error-code>404</error-code>
      <location>/NotFound.jsp</location>
    </error-page>
    <!-- 2、通过异常的类型配置error-page。(即空指针异常)时,跳转到错误处理页面。 -->
    <error-page>
      <exception-type>java.lang.NullException</exception-type>
      <location>/error.jsp</location>
    </error-page>

    spring在web.xml中的配置:

       <!-- Spring -->
        <!-- 配置Spring配置文件路径 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
          classpath:spring/applicationContext.xml
        </param-value>
        </context-param>
        <!-- 配置Spring上下文监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- Spring -->

    其中监听器ContextLoaderListener是必须的,作用是加载spring的配置文件,如果不配置contextConfigLocation,则默认的路径是/WEB-INF/applicationContext.xml。

    application的相关配置介绍:applicationContext.xml配置简介

    spring MVC在web.xml中的配置:

    <!-- Spring MVC配置 -->
    <servlet> 
      <servlet-name>spring</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认 
      </init-param> 
      <load-on-startup>1</load-on-startup> 
    </servlet> 
      
    <servlet-mapping> 
      <servlet-name>spring</servlet-name> 
      <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 

    springMVC配置的相关介绍:Spring MVC学习笔记

    WebAppRootListener在web.xml中的配置:

    <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>webapp.root</param-value>
    </context-param>
    
    <listener>
      <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
    </listener>

    WebAppRootListener用于获得项目根目录,在代码中使用System.getProperty(webapp.root)获得。

    WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否则ApplicationContext的bean注入根目录值时会发生无法注入异常。

    但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener 这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能。

    参考:

    web.xml文件详解

    web项目配置webAppRootKey 取得根目录

  • 相关阅读:
    (转)js的左右滑动触屏事件
    (转)Document对象内容集合
    AppCan相关网站
    (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解
    (转)深入浅出 iOS 之生命周期
    (转)iphone数据存储之-- Core Data的使用
    (转)xcode5.0.2下国际化图文解说
    (转)IOS之Info.plist文件简介
    Note_Master-Detail Application(iOS template)_06_ YJYDetailViewController.h
    Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m
  • 原文地址:https://www.cnblogs.com/Jason-Xiang/p/6611553.html
Copyright © 2020-2023  润新知