Web.xml of Java Web Application

This article mainly discusses the web and xml in the JavaWeb project, why they exist, the function and meaning of the existence of the file, what elements it contains, and the function of the corresponding elements.

 

When tomcat starts, it will look for the web.xml file in the application, and if found, it will read the configuration items in web.xml. For a project with only static pages, there is no need to configure web and xml files in the project, such as a static html file, which can be accessed in tomcat.

It can be seen that the web.xml file does not have to exist in the web project. However, the projects usually developed must contain the web.xml file, and some element tags of the web.xml file are used in the project.

 

The web.xml file is used to configure: welcome page, servlet, filter and other functions. How many kinds of label elements are defined in the schema file of web.xml, the label elements defined by its schema file can appear in web.xml, and it can have the functions defined. The schema file of web.xml is defined by Sun. The root element <web-app> of each web.xml file must indicate which schema file the web.xml uses. Such as:

<?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://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

< /web-app>

 

And the version of the schema file of web.xml can be modified, the version that can be used by web.xml can be searched on this website http://java.sun.com/xml/ns/javaee.

 

Taking the springMVC project as an example, it will be explained from top to bottom. The order here is just a matter of habit, not a fixed order. Generally, the following tags will be configured:

1: Filter filter: There are mainly two kinds of character filters, the ones that set character encoding and XSS anti-attack filters, which can be packaged with the StringEscapeUtils class in commons-lang3

 

<filter>
	<filter-name>encoding</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>encoding</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- XSS attack wrapper -->
<filter>
	<filter-name>XssEscape</filter-name>
	<filter-class>filter.XssFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>XssEscape</filter-name>
	<url-pattern>/*</url-pattern>
	<dispatcher>REQUEST</dispatcher>
</filter-mapping>
 

 

2: Configure the Servlet processor of springMvc

 

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:cfg-spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- / means that all requests go through this serlvet -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 3: Configure the spring listener, context-param is used to declare the initialization parameters of the context in the entire application, and param-name sets the parameter name of the context. It must be a unique name. param-value sets the value of the parameter name.   Two listeners are configured here: one is org.springframework.web.context.ContextLoaderListener, which implements the ServletContextListener interface. When the Tomcat container is started, the The function of the class is to automatically load the configuration information of the ApplicationContext. If the initial parameter of the contextConfigLocation is not set, the application.xml file in the default parameter WEB-INF path will be used. If you need to read multiple configuration files or modify the default path, you can configure it in context-param. Another function of this class is to use the container function of spring in the servlet category defined by itself , obtain the WebApplicationContext , and then obtain the instance Bean through the WebApplicationContext . org.springframework.web.context.request.RequestContextListener is a spring bean to support session scope.
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:cfg-spring/spring-application.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>
			org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
 4. The configuration of the default servlet mapping is as follows
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.eot</url-pattern>
	</servlet-mapping>
 

 This is directly processed using the defaultServlet of web.xml in tomcat:

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

 5: Application Welcome Page

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

 

 When tomcat starts, the loading sequence is as follows

context-param -> listener -> filter -> servlet

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326602054&siteId=291194637