Filter过滤器周期、Filter拦截过滤、Filter执行链

Servlet过滤器的概念:
Servlet过滤器本身并不生成请求和响应对象,它只提供过滤作用。

Servlet过滤器能够在Servlet被调用之前检查Request对象,修改Request Header和Request内容。

在Servlet被调用之后检查Response对象,修改Response Header和Response内容。

Servlet过滤器负责过滤的Web组件可以是Servlet、JSP或HTML文件。

Filter过滤器周期、Filter拦截过滤、Filter执行链_第1张图片

来看一个简单小例子
Filter也是需要在web.xml进行配置:


<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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <filter>
        <filter-name>helloFilterfilter-name>
        <filter-class>com.safly.HelloFilterfilter-class>
        <init-param>
            <param-name>nameparam-name>
            <param-value>rootparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>helloFilterfilter-name>
        <url-pattern>/test.jspurl-pattern>
    filter-mapping>
web-app>

HelloFilter

package com.safly;

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 HelloFilter implements Filter{

    public void destroy() {
        System.out.println("destroy...");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("doFilter");
        chain.doFilter(request, response);
    }
    //加载web应用时,即被创建
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init...");
    }

}

test.jsp页面只有一行字输出

浏览器输出:
http://localhost:8080/day01/test.jsp

Filter过滤器周期、Filter拦截过滤、Filter执行链_第2张图片

Web服务器启动,根据web.xml配置,创建注册的Filter实例对象,创建完毕后,调用init方法,该法只执行一次,下次请求时,会调用doFilter,关闭服务器调用destroy


来看另一个简单小例子
Filter过滤器周期、Filter拦截过滤、Filter执行链_第3张图片

我们来看下login.jsp页面吧–里面有用户名、密码

<body>
<font color="red">${requestScope.message }font>
    <form action="hello.jsp" method="post">
        username:<input type="text" name="username" value="${param.username }"/>
        password:<input type="password" name="password"/>
        <input type="submit" value="Submit"/>
    form>
body>

提交后跳转到hello.jsp页面,如果验证成功,就提示hello界面


 Hello:${param.username }

条件是在跳转到hello.jsp页面之前,需要进行拦截验证,UsernameFilter和PasswordFilter进行拦截hello.jsp页面
PasswordFilter

    <context-param>
        <param-name>passwordparam-name>
        <param-value>1234param-value>
    context-param>

    <filter>
        <filter-name>UsernameFilterfilter-name>
        <filter-class>com.safly.UsernameFilterfilter-class>
        <init-param>
            <param-name>usernameparam-name>
            <param-value>Tomparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>UsernameFilterfilter-name>
        <url-pattern>/hello.jspurl-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>PasswordFilterfilter-name>
        <filter-class>com.safly.PasswordFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>PasswordFilterfilter-name>
        <url-pattern>/hello.jspurl-pattern>
    filter-mapping>

UsernameFilter在web.xml中username:Tom,如果在login.jsp页面输入的用户名为Tom,那么就会去继续验证密码是否正确,如果输入的用户名字不是Tom,就转发到login.jsp页面

package com.safly;
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 UsernameFilter implements Filter {

    public void destroy() {

    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        String initUser = filterConfig.getInitParameter("username");
        String username = request.getParameter("username");

        if (!initUser.equals(username)) {
            request.setAttribute("message", "用户名不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
    private FilterConfig filterConfig;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
}

PasswordFilter如果web.xml中的password:1234,如果在login.jsp输入的密码不是1234,就转发到login.jsp,如果密码也输入正确,就进入到hello.jsp页面进行欢迎提示

package com.safly;
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 PasswordFilter implements Filter {

    public void destroy() {
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        String initPassword = filterConfig.getServletContext().getInitParameter("password");
        String password = request.getParameter("password");
        if (!initPassword.equals(password)) {
            request.setAttribute("message", "密码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
    private FilterConfig filterConfig;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
}

以下是4种情况:
这里写图片描述

Filter过滤器周期、Filter拦截过滤、Filter执行链_第4张图片

这里写图片描述

这里写图片描述


Filter执行链流程

首先定义一个hello.jsp界面,里面只有一个链接跳转到second.jsp界面

<body>
<a href="second.jsp">To Test Pagea>
body>

但是在跳转到second.jsp界面做了2个拦截

<filter>
        <filter-name>HelloFilterfilter-name>
        <filter-class>com.safly.HelloFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>HelloFilterfilter-name>
        /second.jsp
    filter-mapping>

    <filter>
        <filter-name>SecondFilterfilter-name>
        <filter-class>com.safly.SecondFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>SecondFilterfilter-name>
        /second.jsp
    filter-mapping>

HelloFilter

package com.safly;
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 HelloFilter implements Filter {
    public void destroy() {

    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("before helloFilter'chain");
        chain.doFilter(request, response);
        System.out.println("after hellofilter'chain");
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}

SecondFilter

package com.safly;
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 SecondFilter implements Filter {

    public void destroy() { 
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("before secondFilter'chain");
        chain.doFilter(request, response);
        System.out.println("after secondFilter'chain");
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }

}

浏览器输入
http://localhost:8080/day01/second.jsp

log日志输出如下:

before helloFilter'chain
before secondFilter'chain
second.jsp
after secondFilter'chain
after hellofilter'chain

通过截图来看下Filter链执行的流程吧
Filter过滤器周期、Filter拦截过滤、Filter执行链_第5张图片

你可能感兴趣的:(Javaee-javaweb)