Analysis of Web.xml in Hello1 project

一、web.xml文件介绍

  • The web.xml file contains several elements that are required for a Facelets application. All of the following are created automatically when you use NetBeans IDE to create an application.
  • web.xml文件的作用

web.xml主要用来配置Filter、Listener、Servlet等。但是要说明的是web.xml并不是必须的,一个web工程可以没有web.xml文件。

  • WEB容器的加载过程

WEB容器的加载顺序是:

ServletContext -> context-param -> listener -> filter -> servlet。在web.xml文件中最好按照这种顺序配置这些元素,以兼容较低版本的Tomcat。

  • WEB容器的启动过程

 WEB容器启动时,加载过程顺序如下:

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

二、web.xml of hello1 analysis

• xml文档第一行的声明和它的文档元素描述信息。

<?xml version="1.0" encoding="UTF-8"?>
  • 表示文档符合xml1.0规范,文档字符编码默认为“UTF-8” 

• Servlet 3.1 deployment descriptor:

<web-app version="3.1" 
     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_3_1.xsd">
... </web-app>
  • Java EE 7 XML schema, namespace is http://xmlns.jcp.org/xml/ns/javaee/
  • web-app是web.xml文档的根元素
  • xmlns是XML NameSpace的缩写
  • xmls(:xxx)="yyy"这是xml引入名称空间的语法格式,式中,“xxx”表示引入名臣空间的前缀名,可以指定(如“xsi”),也可不指定(使用默认),“yyy”表示该名称空间的名称,形式上为一个URL。
  • xsi名称空间下有很多较为重要的属性,其中一个就是xsi:schemaLocation,它的作用是引入XML Schema文档,对xml文档的元素进行内容约束。它包含了两个URL,这两个URL之间用空白符或者换行符进行分割。第一个URL是名称空间的名称,第二个URL是文档的位置。那么,这句的作用是引入一个名称空间为http://xmlns.jcp.org/xml/ns/javaee、文档位置为http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd的XML Schema文档。也可参阅Eclipse XML文件模板中给出的XML文件引入Schema文档的语法格式:
    xsi:schemaLocation="{namespace} {location}

• A context parameter specifying the project stage:

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
  •  A context parameter provides configuration information needed by a web application. An application can define its own context parameters. In addition, JavaServer Faces technology and Java Servlet technology define context parameters that an application can use.
  • 声明应用范围内的初始化参数。它用于向 ServletContext提供键值对,即应用程序上下文信息。我们的listener, filter等在初始化时会用到这些上下文中的信息。
  • 在servlet里面可以通过getServletContext().getInitParameter("context/param")得到。

 • A servelt element and its servlet-mapping element specifying the FacesServlet. All files with the .xhtml suffix will be matched:

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
  • <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

• 会话超时配置:

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

• A welcome-file-list element specifying the location of the landing page:

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
原文地址:https://www.cnblogs.com/wdyaoyao/p/10636248.html