【web.xml】项目从web.xml开始

时间:2024-01-08 08:09:20

前言

依自己目前的所闻所见,每个项目的WEB-INF下都会带有一个web.xml配置文件。当启动项目时,web容器(tomcat)会首先去读取web.xml中的内容,读取加载完成后才算完成了项目的启动。

详解

总体结构如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name>
<!--在这里配置-->
</web-app>

头部         :    schema 引用

<web-app>:具体配置配置在此标签内,如下常见配配置项

1.  <context-param>:配置上下文的初始参数

 <!--容器在启动时就加载spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <context-param>
<param-name>name</param-name>
<param-value>小明</param-value>
</context-param>

参数会加载到  context——>parameters中

此时可以在controller中利用以下方法获取

    @RequestMapping("/home")
public String home(HttpServletRequest request){
String name = request.getServletContext().getInitParameter("name");
System.out.println(name+"_______________________");
return "/index2";
}

2.<listener>:配置监听器

<!--spring 启动IOC容器的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.<servlet>:配置servlet

    <!--spring mvc 的DispatcherServlet-->
<servlet>
<servlet-name>enterprise-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--spring mvc 的 配置文件,如果配置文件名和servlet-name相同则不需要此配置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:enterprise-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--对文件上传的限制-->
<multipart-config>
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>enterprise-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

也可单独使用,使用默认servlet加载静态资源

<!--tomcat默认servlet为default-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/*.css</url-pattern>
</servlet-mapping>

4.<filter>:配置过滤器

 <!--配置字符编码过滤器-->
<!--字符编码过滤器必须要配置在所有过滤器前面-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

5.<session-config>:配置session过期时间

 <!--session的过期时间,单位为分钟,优先级  servlet api(setMaxInactiveInterval(秒))—> 项目/web.xml—>tomcat/web.xml-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>

6.<error-page>:配置错误跳转页面

<!--当页面访问不存在的请求时就跳转到404.jsp页面-->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/pages/error/404.jsp</location>
</error-page>

7.<resource-ref>:配置JNDI数据源

 <resource-ref>
<description>DHCP数据库连接池</description>
<res-ref-name>jdbc/microDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

此处可参看:这里  查看如何实现利用JNDI实现数据库连接。

加载顺序:会先读取<context-param>和<listener>两个节点

——>创建servletContext,将<context-param>的键值对交给servletContext

——>创建listener的类实例

——>filter

——>servlet(load-on-startup:标记容器是否在启动的时候就加载这个servlet,越小越先执行(0-正整数),负数或不配置则等到调用该servlet时才加载)