zoukankan      html  css  js  c++  java
  • java web过滤器

                                                      java过滤器(imooc学习)
    定义:过滤器是一个服务器端的组件,它可以截取用户端的请求与响应信息,并对这些信息过滤。

    工作原理

    1、过滤器中web容器启动时就进行加载
    2、过滤器存在于用户请求和web资源之间
    3、用户请求和web资源响应的【收发】都经过滤器按【过滤规则】进行性过滤

    生命周期

    实例化--》初始化--》过滤--》销毁
    web.xml init() doFilter() destroy()


     web.xml配置

     第一个过滤器实例

    1.配置web.xml

    <filter>
            <filter-name>myfilter</filter-name>
            <filter-class>firstfilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>myfilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    

    2.写一个类基础filter

    public class firstfilter implements Filter{
    
    	public void destroy() {
    		System.out.println("destroy...");
    	}
    
    	public void doFilter(ServletRequest request, ServletResponse response,
    			FilterChain chain) throws IOException, ServletException {
    		System.out.println("start filter....");
    		chain.doFilter(request, response);
    		System.out.println("end filter");
    		
    		
    	}
    
    	public void init(FilterConfig filterConfig) throws ServletException {
    		System.out.println("init....");
    		//tomcat服务器加载时运行,能读取web.xml
    	}
    
    }
    

    当tomcat启动时候,输出init,浏览器中输入index.jsp后输出start filter,end filter,关闭tomcat后执行destroy

      

      

     

  • 相关阅读:
    poi 导出文件
    获取哪一年 周一的所有日期
    线程池配置
    mybatis基于唯一索引插入或更新
    mongoTemplate关联查询
    cas认证机制
    SpringBoot服务
    HashMap的底层实现
    maven仓库提示“Downloading: http://repo.maven.apache.org/maven2/”
    Tomcat安装SSL证书
  • 原文地址:https://www.cnblogs.com/linhong/p/4298351.html
Copyright © 2011-2022 走看看