Filter入门学习

(一)Filter介绍

1.概念:filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目 标资源访问前后进行逻辑处理

2.快速入门

步骤:(重要,另外初始化方法,销毁方法和doFilter()方法的执行时间也必须要知道,在代码中有注释笔记

  1. 编写一个过滤器的类实现Filter接口
  2. 实现接口中尚未实现的方法(着重实现doFilter方法)
  3. 在web.xml中进行配置(主要是配置要对哪些资源进行过滤)

(二)示例及笔记

(a)QuickFilterOne.java

package cn.shu.web.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;

public class QuickFilterOne implements Filter{
	/*FIlter对象销毁时执行的,关闭服务器执行该方法*/
	@Override
	public void destroy() {
		System.out.println("destroy执行...");
	}

	@Override
	/*只要访问某资源配置了filter过滤器,每次访问该资源时,都会执行doFilter方法
	 * 
	 * doFilter是Filter的核心过滤方法
	 * request :内部封装的是客户端http请求的内容
	 * response:代表的是响应
	 * FilterChain:过滤器链对象,知道所有filter的引用(所有filter都在FilterChain中有注册)和顺序*/
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("QuickFilterOne running");
		/*放行请求,此处的doFilter方法只有两个参数,区别于三个参数*/
		chain.doFilter(request, response);
		/*此处会放行请求,给下一个Filter,决定filter执行先后顺序的
		 * 因素是web.xml配置文件中filter-mapping的位置,若存在多个
		 * filter-mapping配置,则filter-mapping配置写在前面的filter先执行*/
	}
	
	//Filter创建时执行init方法,
	/*服务器一启动就创建QuickFilterOne过滤器对象,并执行初始化init方法*/
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		/*1.获得web.xml中filter的名称QuickFilterOne*/
		System.out.println(arg0.getFilterName());
		/*2.可以获得当前filter初始化参数
		 * 需在web.xml中进行配置
		 * 
    QuickFilterOne
    cn.shu.web.filter.QuickFilterOne
  
  	hehe
  	HEHE
  
  */
		System.out.println(arg0.getInitParameter("hehe"));//HEHE
		
		/*3.获得servletContext*/
		System.out.println(arg0.getServletContext());
		System.out.println("init执行...");
	}

}
(b)QuickFilterTwo.java

package cn.shu.web.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;

public class QuickFilterTwo implements Filter{

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		System.out.println("QuickFilterTwo running....");
		/*若此处不写放行语句,chain.doFilter(request, response);
		 * 启动项目,在浏览器中访问ServletOne资源,是访问不到的
		 * 控制台会打印:QuickFilterOne running
                      QuickFilterTwo running....
                      因为访问资源过了FIlterOne,却被FilterTwo过滤了
        
        filter可以做系统的权限访问模块
		 * */
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
(c)ServletOne.java

package cn.shu.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletOne
 */
public class ServletOne extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletOne() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletResponse request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ServletOne running..");
		 //response.getWriter().write("ServletOne running..");
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	 //((ServletRequest) request).getRequestDispatcher("/servlet2").forward((ServletRequest) request, request);
	
	request.sendRedirect(((HttpServletRequest) request).getContextPath()+"/servlet2");;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
(d)ServletTwo.java

package cn.shu.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletTwo
 */
public class ServletTwo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletTwo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Servlet2 running...");
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
(e)web.xml



  Filter
  
    QuickFilterOne
    cn.shu.web.filter.QuickFilterOne
    
      hehe
      HEHE
    
  
  
    QuickFilterOne
    /*
    REQUEST
  
 
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    
    ServletOne
    ServletOne
    cn.shu.web.servlet.ServletOne
  
  
    ServletOne
    /servletOne
  
  
    
    ServletTwo
    ServletTwo
    cn.shu.web.servlet.ServletTwo
  
  
    ServletTwo
    /servletTwo
  

说明:

在web.xml配置url-pattern配置时

  1. 完全匹配  /servlet1----直接访问/servlet1资源
  2. 目录匹配  /shu/* ----访问shu包下的
  3. 扩展名匹配  *.abc  *.jsp-----访问后缀为abc和jsp的文件

 

在访问某个资源前,可以对其配置过滤器,若不符合条件不让其访问,可以配置一个过滤器,也可以配置多个过滤器,若其中任何一个 过滤器不放行,该请求访问资源就会失败,过滤器访问的顺序由web.xml配置文件中过滤器的filter-mapping的前后顺序决定,filter-mapping配置在前,则首先执行,chain链对象知道所有的filter执行顺序,因为所有的filter都会在其中注册。

 

你可能感兴趣的:(Filter)