Servlet3.0之五:servlet3.0下的web.xml

时间:2023-01-26 09:25:26

Servlet3.0随J2EE6一起发布,web.xml配置文件中包含: 默认页配置、session超时配置和错误提示页配置。

上面三篇文章都是在spring boot环境中的,如果不在呢,则用下面的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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" >
     
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
     
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
     
    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>
     
    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>
     
</web-app>

spring项目中的Servlet3.0示例

1.新建项目,勾选3.0

Servlet3.0之五:servlet3.0下的web.xml

工程结构如下:

Servlet3.0之五:servlet3.0下的web.xml

2.编码

web.xml的内容如上面的web.xml。

Filter类TwoFilter.java如下

package com.dxz.demo.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class TwoFilter
 */
@WebFilter("/twoFilter/*")
public class TwoFilter implements Filter {

    /**
     * Default constructor. 
     */
    public TwoFilter() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("TwoFilter doFilter()");
        chain.doFilter(request, response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

部署到tomcat容器后,启动并访问得如下结果:

Servlet3.0之五:servlet3.0下的web.xml