什么是过滤器?

过滤器:从字面上看,可以理解为将具有杂质的水过滤,留下干净的水。那么从IT的角度上理解。过滤器:是处在源数据(数据库之类的)目标数据(显示页面)的中间组件。对于Web应用来说,过滤器是驻留在服务器上的Web组件,它可以截取客户端和资源之间的请求和响应信息,并对这些信息进行过滤。

当Web容器(服务器)接收到一个对资源数据的请求时,它会判断过滤器和这个请求是不是有关联,如果有,它将这个请求交给过滤器处理,然后在过滤器中,你可以改变请求的内容,然后再将请求给目标资源。

[请求—>过滤器—>Web容器—>目标资源]

当目标资源对请求作出响应时,Web容器同样会向转发给过滤器,在过滤器你可以对响应的内容进行改变,然后再发送给显示页面。

[目标资源—>Web容器—>过滤器—>显示页面]

过滤器的生命周期

  过滤器的生命周期与web容器相同,当web容器启动时,就会读取应用的web.xml配置文件,如果这里配置了过滤器,容器就会执行实例化,并调用过滤器的init方法。

  之后用户的每一次请求都会执行过滤器的doFilter方法。

  当web容器销毁时,就会执行destroy方法,释放资源。

Javaweb——过滤器映射

 

过滤器的执行过程

Javaweb——过滤器映射

 

用户在发送请求后,如果该请求满足过滤器的过滤规则,web容器就会执行过滤器中的doFilter方法进行特定的操作;然后通过调用FilterChain.doFilter转交给web容器。web容器执行完成后把资源返回给过滤器,再展现给用户。

简单的过滤器实例

  下面通过一个简单的代码,看看过滤器的编写。

  首先,需要创建一个过滤器,过滤器集成javax.servlet.Filter接口,其中必须实现三个方法:init() doFilter() destroy()

package com.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.http.HttpServletRequest;

//定义类继承Filter,定义过滤器
public class myFilter implements Filter {

    public myFilter() {
      
    }
    //销毁
    public void destroy() {
        
        System.out.println("myFilter destroy");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        
        System.out.println("myFilter start..dofilter");
        //请求放行        
        HttpServletRequest req = (HttpServletRequest)request;
        req.getRequestDispatcher("index.jsp").forward(request, response);
        System.out.println("myFilter end..fofilter");
    }
  //初始化
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("myFilter init");
    }

}

 init()方法是在web容器实例化过滤器时调用的。

 doFilter()方法是每次有请求,且满足过滤规则时调用。

 destroy()方法是web容器关闭时,调用。

 

然后,在web.xml中配置相应的选项。如果是servlet3.0,那么支持注解的方式配置过滤器。

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>com.filter.myFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>

其中几个必备的项:

  在<filter>中配置了过滤器,filter-name是过滤器的名字,filter-class是过滤器的类;

  在<filter-mapping>中配置了过滤器的映射规则,filter-name是过滤器的名字,url-pattern是过滤的路径,dispatcher是过滤器的分类(主要包括四种)

  这里先说下过滤器的规则,如果想要全部的请求都过滤,那么可以写/*

  如果想要过滤index.jsp  index.html 可以写/index*

  如果只想过滤index.jsp,可以写成/index.jsp

其次,配置好后,创建index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    This is Filter JSP!
    <%
        System.out.println("index jsp");
    %>
</body>
</html>
View Code

相关文章:

  • 2022-02-08
  • 2021-12-23
  • 2021-12-23
  • 2021-09-08
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-12-23
  • 2021-12-23
  • 2021-05-21
  • 2022-02-26
相关资源
相似解决方案