zoukankan      html  css  js  c++  java
  • JavaWeb学习篇--Filter过滤器

    Filter过滤器简介

      ServletAPI中提供了一个Filter接口,开发web应用时,如果编写的 java 类实现了这个接口,则把这个java类称之为过滤器Filter。

      WEB服务器每次在调用web资源的service方法之前(服务器内部对资源的访问机制决定的),都会先调用一下filter的doFilter方法。

      通过Filter技术,开发人员可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截。简单说,就是可以实现web容器对某资源的访问前截获进行相关的处理,还可以在某资源向web容器返回响应前进行截获进行处理

    Filter的生命周期

      和Servlet一样,Filter的生命周期也是由Web服务器来负责的。

      实例化-->初始化-->服务-->销毁

      和Servlet生命周期的区别:

    1. 启动服务器时加载实例,Filter直接初始化,Servlet是第一次访问时初始化。
    2. Servlet从第一次到以后的多次访问,都是只调用doGet()或doPost()方法; Filter只调用方法doFilter()进行处理

    实现Filter接口以后,会有三个方法:

    • public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)     //执行过滤操作
    • public void init(FilterConfig fConfig)                                                                                     //通过FilterConfig 获取Config 配置对象者其它域对象
    • public void destroy()                                                                                                      //销毁时执行

     简单用法:

    首先新建类 MyFilter 实现  Filter接口  

    //首先实现  Filter
    public class MyFilter implements Filter {
     
        public MyFilters() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * 销毁时执行
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * 在Serveice方法钱执行过滤操作
         */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
             
            chain.doFilter(request, response);
        }
    
        /**
         * 获取配置信息
         */
        public void init(FilterConfig fConfig) throws ServletException {
             
        }
    View Code

    然后在Web.xml配置文件中添加配置信息   

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>com.wwj.Filter</display-name>
      
      <filter>
        <filter-name>MyFilter</filter-name>
        <!-- 类的限定名 -->
        <filter-class>com.wwj.Filte.MyFilter</filter-class>
        <init-param>
          <param-name>ip</param-name>
          <param-value>127.0.0.1</param-value>
        </init-param>
      </filter>
      <filter-mapping>
          <!-- 名称和上面的名称保持一致 -->
        <filter-name>MyFilter</filter-name>
        <!-- 过滤的URL地址,此种写法是过滤所有 -->
        <url-pattern>/*</url-pattern>
        <!-- 设置需要过滤的请求,默认是Request -->
        <dispatcher>REQUEST</dispatcher>
      </filter-mapping>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    View Code

     这样一个简单的过滤器就配置好了!

    下面演示一个过滤IP的小Demo:

    首先在Web.xml配置需要过滤的IP地址

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>com.wwj.Filter</display-name>
      
      <filter>
        <filter-name>MyFilter</filter-name>
        <!-- 类的限定名 -->
        <filter-class>com.wwj.Filte.MyFilter</filter-class>
        <init-param>
          <!-- 参数名,相当于Key -->
          <param-name>ip</param-name>
          <!-- 参数值,相当于Value -->
          <param-value>127.0.0.1</param-value>
        </init-param>
      </filter>
      <filter-mapping>
          <!-- 名称和上面的名称保持一致 -->
        <filter-name>MyFilter</filter-name>
        <!-- 过滤的URL地址,此种写法是过滤所有 -->
        <url-pattern>/*</url-pattern>
        <!-- 设置需要过滤的请求,默认是Request -->
        <dispatcher>REQUEST</dispatcher>
      </filter-mapping>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    View Code

    然后在程序里读取配置的需要过滤的IP地址,然后在doFilter进行过滤: 

    (服务器和客户端为一台机器时,地址不能写http:localhost:8008/xxx ,应该写  http:127.0.0.1:8008/xxx 这种方式)

    //首先实现  Filter
    public class MyFilters implements Filter {
    
        //定义变量
        private FilterConfig config;
    
        public MyFilters() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * 销毁时执行
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * 在Serveice方法钱执行过滤操作
         */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            //读取we.xml配置的IP地址
            String FilterIp = this.config.getInitParameter("ip");
            //获取当前客户端的地址
            String ip = request.getRemoteAddr();
            System.out.println(ip);
            if (ip.equals(FilterIp)) {
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("<b>该IP被设置禁止访问!!!</b>");
            } else {
                System.out.println("IP不在过滤的名单之中");
                chain.doFilter(request, response);
            }
        }
    
        /**
         * 获取配置信息
         */
        public void init(FilterConfig fConfig) throws ServletException {
            this.config = fConfig;     //赋值
        }
    }
    View Code
  • 相关阅读:
    微软发布3款SQL Injection(SQL 注入)攻击检测工具
    TortoiseSVN 使用介绍
    windows下的Bug跟踪管理软件Bugfree的安装
    国内PHP开源建站程序一览
    55个经典开源Windows工具
    JavaScript代码格式化工具(JS代码分析必备)
    Javascript工具 使用packer来压缩JS文件
    开源UML设计工具StarUML
    看似简单的问题其实不简单
    Javascript工具 使用JSDoc建立JavaScript代码的文档
  • 原文地址:https://www.cnblogs.com/wwj1992/p/6382709.html
Copyright © 2011-2022 走看看