Home Backend Development XML/RSS Tutorial Detailed introduction to the role of web.xml file

Detailed introduction to the role of web.xml file

Mar 11, 2017 pm 06:03 PM

There is a web.xml file in every javaEE project, so what is its function? Is it required for every web.xml project?
There can be no web.xml file in a web, that is to say, the web.xml file is not necessary for web projects. The
web.xml file is used to initialize configuration information: such as Welcome page, servlet, servlet-mapping, filter, listener, startup loading Level etc.

When your web project does not use these, you can configure your Application without the web.xml file.


Each xml file has a definition Schema file in which it writes rules, that is to say, how many tags are defined in the xml Schema file corresponding to javaEE's definition web.xml element, the tag element it defines can appear in web.xml, and it also has specific functions. The schema file of web.xml is defined by Sun. The root element of each web.xml file is , which schema file is used by this web.xml must be indicated. For example:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
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://www.php.cn/"> 
</web-app>
Copy after login

The tags defined in the web.xml pattern file are not fixed, and the pattern file can also be changed. Generally speaking, as the version of the web.mxl pattern file is upgraded, The functions defined in it will become more and more complex, and the types of label elements will definitely increase, but some are not very commonly used. We only need to remember some commonly used ones and know how to configure them.

The following lists some commonly used tag elements and their functions in web.xml:
1. Specify the welcome page, for example:

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

PS: 2 welcome pages are specified Pages are displayed in order from the first one. If the first one exists, the first one will be displayed, and the following ones will have no effect. If the first one doesn't exist, find the second one, and so on.

About the welcome page:

When you visit a website, the first page you see by default is called the welcome page. Generally, the home page serves as the welcome page. Normally, we will specify the welcome page in web.xml. But web.xml is not a necessary file for the Web. Without web.xml, the website can still work normally. However, when the functions of the website become more complicated, web.xml is indeed very useful. Therefore, the dynamic web project created by default has a web.xml file under the WEB-INF folder.

2. Naming and customizing the URL. We can name and customize URLs for Servlet and JSP files. Customized URLs depend on naming, and naming must precede the customized URL. Let’s take serlet as an example:
(1), name the Servlet:

<servlet> 
    <servlet-name>servlet1</servlet-name> 
    <servlet-class>org.whatisjava.TestServlet</servlet-class> 
</servlet>
Copy after login

(2), customize the URL for the Servlet,

<servlet-mapping> 
    <servlet-name>servlet1</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping>
Copy after login



3. Customize initialization parameters: You can customize the initialization parameters of servlet, JSP, and Context, and then obtain these parameter values ​​in servlet, JSP, and Context.

The following uses servlet as an example:

<servlet> 
    <servlet-name>servlet1</servlet-name> 
    <servlet-class>org.whatisjava.TestServlet</servlet-class> 
    <init-param> 
          <param-name>userName</param-name> 
          <param-value>Daniel</param-value> 
    </init-param> 
    <init-param> 
          <param-name>E-mail</param-name> 
          <param-value>125485762@qq.com</param-value> 
    </init-param> 
</servlet>
Copy after login


4. Specify the error handling page, which can be done through "Exception Type" or "Error Code" Specify error handling page.

<error-page> 
    <error-code>404</error-code> 
    <location>/error404.jsp</location> 
</error-page> 
----------------------------- 
<error-page> 
    <exception-type>java.lang.Exception<exception-type> 
    <location>/exception.jsp<location> 
</error-page>
Copy after login


5. Set filter: For example, set an encoding filter to filter all resources

<filter> 
    <filter-name>XXXCharaSetFilter</filter-name> 
    <filter-class>net.test.CharSetFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>XXXCharaSetFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping>
Copy after login


6. Set the listener:

<listener> 
<listener-class>net.test.XXXLisenet</listener-class> 
</listener>
Copy after login


7. Set the session (Session) expiration time, where the time is in minutes. If you set a 60-minute timeout:

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

In addition to these tag elements, you can also add many tag elements to web.xml, which are omitted because they are not commonly used.

The above is the detailed content of Detailed introduction to the role of web.xml file. 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

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

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

What are web standards? What are web standards? Oct 18, 2023 pm 05:24 PM

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

See all articles