Home Backend Development XML/RSS Tutorial Detailed analysis of web.xml file content

Detailed analysis of web.xml file content

Apr 24, 2017 am 10:49 AM

Detailed explanation of web.xml file

Preface: web.xml is used in general web projects. .xml is mainly used for configuration and can facilitate the development of web projects. web.xml is mainly used to configure Filter, Listener, Servlet, etc. But it should be noted that web.xml is not necessary. A web project does not need a web.xml file.

1. WEB project loading web.xml process

After personal testing, WEB The project loading order has nothing to do with the configuration order of element nodes in the file. That is, filter will not be loaded first because filter is written before listener. The loading sequence of the WEB container is: ServletContext -> context-param -> listener -> filter -> servlet. And these elements can be configured anywhere in the file.

The loading process sequence is as follows:

  1. When starting a WEB project, the WEB container will read its The configuration file web.xml reads the two nodes and .

  2. #Emergency, create a ServletContext (servlet context), all parts of this web project will share this context.

  3. The container converts into a key-value pair and hands it to servletContext.

  4. The container creates a class instance in and creates a listener.

##2. Detailed explanation of web.xml file elements

1. schema

The schema file of web.xml is defined by Sun. The root element of each web.xml file is , must indicate which schema file this web.xml uses. All other elements are placed in .

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></web-app>
Copy after login

 2. Web application icon

## Indicates that IDE and GUI tools are used to represent Web applications of large and small icons.

<icon>
    <small-icon>/images/app_small.gif</small-icon>
    <large-icon>/images/app_large.gif</large-icon></icon>
Copy after login

 

3. Web application name## Provide GUI tools that may be used to mark A name for this specific Web application

<display-name>Tomcat Example</display-name>
Copy after login
 4. Descriptive text related to this

<disciption>Tomcat Example servlets and JSP pages.</disciption>
Copy after login
 5, Context parameter

Declaration application Initialization parameters within the range. It is used to provide key-value pairs, that is, application context information, to the ServletContext. Our listeners, filters, etc. will use the information in these contexts during initialization. In the servlet, it can be obtained through 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>
Copy after login
 6.Filter

##  Combine a name with an implementation of javaxs.servlet.Filter The interface is associated with the class.

<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>
Copy after login
 7.Listener

<listener> 
    <listerner-class>com.listener.SessionListener</listener-class> </listener>
Copy after login
 8.

##  is used to declare the data of a servlet. It mainly has the following sub-elements:

Specify the name of the servlet

# Specifies the class name of the servlet

  • ## Specifies the complete content of a JSP web page in the web site Path

  • is used to define parameters. There can be multiple init-params. Access initialization parameters through the getInitParamenter(String name) method in the servlet class

  • 指定当Web应用启动时,装载Servlet的次序。当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet。当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它。

  • 用来定义servlet所对应的URL,包含两个子元素

  • 指定servlet的名称

  • 指定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><!-- 高级配置 --><servlet>
    <servlet-name>snoop</servlet-name>
    <servlet-class>SnoopServlet</servlet-class>
    <init-param>
        <param-name>foo</param-name>
        <param-value>bar</param-value>
    </init-param>
    <run-as>
        <description>Security role for anonymous access</description>
        <role-name>tomcat</role-name>
    </run-as></servlet><servlet-mapping>
    <servlet-name>snoop</servlet-name>
    <url-pattern>/snoop</url-pattern></servlet-mapping>
Copy after login

  9、会话超时配置

  单位为分钟。

<session-config>
    <session-timeout>120</session-timeout></session-config>
Copy after login

  10、

<mime-mapping>
    <extension>htm</extension>
    <mime-type>text/html</mime-type></mime-mapping>
Copy after login

  11、欢迎文件页

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file></welcome-file-list>
Copy after login

  12、错误页面

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

  13、设置jsp

   包括  和  两个子元素。其中 元素在JSP 1.2 时就已经存在;而 是JSP 2.0 新增的元素。

   元素主要有八个子元素,它们分别为:

  • :设定的说明 

  • :设定名称 

  • :设定值所影响的范围,如: /CH2 或 /*.jsp

  • :若为 true,表示不支持 EL 语法 

  • :若为 true,表示不支持 <% scripting %>语法 

  • :设定 JSP 网页的编码 

  • :设置 JSP 网页的抬头,扩展名为 .jspf

  • :设置 JSP 网页的结尾,扩展名为 .jspf

<jsp-config>
    <taglib>
        <taglib-uri>Taglib</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group></jsp-config>
Copy after login

  对于Web 应用程式来说,Scriptlet 是个不乐意被见到的东西,因为它会使得HTML 与Java 程式码交相混杂,对于程式的维护来说相当的麻烦,必要的时候,可以在web.xml 中加上 标签,设定所有的JSP 网页都不可以使用Scriptlet。

3、Mapping规则

  当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉,剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下的servlet了。

  其匹配规则和顺序如下:

  1. 精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先 进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。

  2. Longest path matching. Example: The url-pattern of servletA is /test/*, and the url-pattern of servletB is /test/a/*. When accessing http://localhost/test/a, the container will select the servlet with the longest path. Match, which is servletB here.

  3. #Extension matching, if the last segment of the URL contains an extension, the container will select the appropriate servlet based on the extension. Example: servletA's url-pattern: *.action

Starting with "/" and ending with "/*" are used for path mapping. Things starting with the prefix "*." are used for extended mapping. So, why is it wrong to define a seemingly normal match like "/*.action"? Because this match belongs to both path mapping and extended mapping. The container cannot determine

.

The above is the detailed content of Detailed analysis of web.xml file content. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles