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


    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

    工程结构如下:

    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容器后,启动并访问得如下结果:

  • 相关阅读:
    [UOJ#128][BZOJ4196][Noi2015]软件包管理器
    [UOJ#127][BZOJ4195][NOI2015]程序自动分析
    [BZOJ3653]谈笑风生
    Django 数据库查询优化
    C#中引用(ref关键字)参数
    C#中值参数的使用实例
    静态变量和实例变量
    全局变量和局部变量的理解
    C#方法定义和调用-2
    C#函数的方法定义和方法调用小议
  • 原文地址:https://www.cnblogs.com/duanxz/p/2738628.html
Copyright © 2020-2023  润新知