Talking about web.xml configuration details

I have been writing the project for more than a year, and today I will sort out some configurations in web.xml

First of all, we need to understand what wel.xml contains. For simple projects, it is the configuration of spring and some configurations of springMvc (servlet). Now many projects are developed by ssm, so servlets are generally the springMvc framework, and then some listeners. listener, interceptor filter,

There are some others including welcome page, session time, error-page page, etc.

 

First of all, we need to know some loading orders in web.xml

 

 1. When starting a WEB project, the WEB container will read its configuration file web.xml and read the two nodes of <listener> and <context-param>. 

2. Urgently, create a ServletContext (servlet context), which will be shared by all parts of the web project. 

3. The container converts the <context-param> into a key-value pair and hands it to the servletContext. 

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

 

Load order is independent of their order in the web.xml file. That is, the filter will not be loaded first because the filter is written in front of the listener. The final conclusion is: ServletContext -> listener -> filter -> servlet

 

First of all, let's talk about the label load-on-startup

 

The Load-on-startup element specifies the order in which servlets are loaded when the web application starts, and its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when the servlet is called. If the value is a positive integer or zero, the container loads and initializes the servlet at configuration time. The container must ensure that the smaller value is loaded first. If the values ​​are equal, the container can automatically choose who to load first.

 

<listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <!-- Specifies the directory where the Spring Bean configuration file is located. The default configuration is in the WEB-INF directory -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>

 Looking at the above code, you can see that it has nothing to do with the order of the labels.

Here we put the sping in the listener tag to make it load first, because spring is actually the last loaded after the servlet. If that is the case, some filters need to be called to configure some beans. In this case, those beans The bean entity will become null

 

Next is the log

  <listener>
    <listener-class>
      org.springframework.web.util.Log4jConfigListener
    </listener-class>
  </listener>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>

 

The following filter is a key point

Because garbled characters in normal projects have always been a problem that often exists and causes headaches for many people

The current encoding filter can solve some garbled characters submitted by post

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>eccoding</param-name>
    <param-value>utf-8</param-value>
  </init-param>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
  
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

When springMvc requests, sometimes we will use methods such as RequestMethod=PUT or DELETE, but some browsers only recognize get and post requests, so we configure a filter and it is ok

<filter>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
</filter>  
  
<filter-mapping>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <url-pattern>/</url-pattern>  
</filter-mapping>

 

接下俩是springMvc servlet的配置

 <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:springmvc/springmvc.xml</param-value>
    </init-param>
    <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>&nbsp; 默认
    </init-param>
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>

 

 <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 

最后加载servlet

 

最后就是一些Session 跟一些欢迎,或者跳转的页面

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

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

 错误页面

 <error-page>
2      <error-code>404</error-code>
3      <location>/error404.jsp</location>
4  </error-page>
5  <error-page>
6      <exception-type>java.lang.Exception</exception-type>
7      <location>/exception.jsp</location>
8  </error-page>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326152481&siteId=291194637