📜  Servlet – 过滤器链

📅  最后修改于: 2022-05-13 01:55:34.987000             🧑  作者: Mango

Servlet – 过滤器链

过滤器是在请求的预处理和后处理阶段使用的对象。转换、日志记录、压缩、加密和解密、输入验证和其他过滤操作通常使用它来执行。

Servlet 过滤器链

在本课中,我们将学习如何将过滤器链与 Web 资源相关联。 Servlet、JSP 或静态 HTML 页面可以用作 Web 资源。我们可能会使用过滤器函数在 Web 应用程序中进行许多操作,例如会话验证、用户身份验证和验证、阻止对某个在线资源的访问等。通过实现 Filter 接口,我们可以构建多个 Filter 类。当用户在关联多个Filter类后对Web资源发出请求时,关联的Filter类在处理用户请求的Web资源之前和之后运行,从而为用户请求和响应提供过滤器。与 Web 资源关联的多个过滤器按照部署描述符文件的 filter-mapping> 标记 (web.xml) 中提供的顺序执行。

用于过滤器链接口的方法:

要开发一个过滤器类,我们必须实现三个javax.servlet.Filter接口进行过滤。

  1. void doInit(FilterConfig config) –使用此方法初始化过滤器。
  2. void doFilter(ServletRequest request, ServletResponse, response, FilterChain chain) –当客户端请求Web资源时,例如Servlet或JSP页面,Web容器调用该函数。
  3. void destroy() –使用此过程销毁 Filter 对象。

示例项目

我们正在制作一个网页,要求用户输入他的姓名,然后单击提交按钮,这将调用一个 Servlet。但是,在运行此 Servlet 之前,与其相连的两个过滤器将按照部署描述符文件的 标记 (web.xml) 中提供的顺序运行。

项目结构:

索引.html

HTML


 Gfg Filter demo

  

  Enter your name :
  
     
  Name :      
  


Java
import java.io.*;
import javax.servlet.*;
  
public class GfgFilter1 implements Filter {
    public void init(FilterConfig filterConfig) {}
  
    public void destroy() {}
    // This method is called each time a client requests for
    // a web resource
    // which could be a Servlet or a JSP page i.e.
    // preprocessing request
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        out.println(
            " Filter1 is Filtering the request : ");
        out.println("Hello " + request.getParameter("name")
                    + "!");
  
        out.println("
");         out.println("
");         out.println("
");            // Calling  doFilter() calls the next filter in the         // chain will execute or if there is no filter then         // the requested web resource is executed.         chain.doFilter(request, response);            out.println("
");         out.println("
");         out.println("
");            // post-processing the request and after the         // requested web resource is called.         out.println(             " Filter2 is Filtering the response : ");         out.println("Bye " + request.getParameter("name")                     + "!");     } }


Java
import java.io.*;
import javax.servlet.*;
  
public class GfgFilter2 implements Filter {
    public void init(FilterConfig filterConfig) {}
  
    public void destroy() {}
    // This method is called each time a client requests for
    // a web resource
    // which could be a Servlet or a JSP page i.e.
    // preprocessing request
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        out.println(
            " Filter2 is Filtering the request : ");
        out.println("Hello " + request.getParameter("name")
                    + "!");
  
        out.println("
");         out.println("
");         out.println("
");            // Calling  doFilter() calls the next filter in the         // chain will execute or if there is no filter then         // the requested web resource is executed.         chain.doFilter(request, response);            out.println("
");         out.println("
");         out.println("
");            // post-processing the request and after the         // requested web resource is called.         out.println(             " Filter1 is Filtering the response : ");         out.println("Bye " + request.getParameter("name")                     + "!");     } }


Java
import java.io.*;
import javax.servlet.*;
public class GfgServlet extends GenericServlet {
    public void service(ServletRequest request,
                        ServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("GeekssForGeeks servlet is executed.");
    }
}


XML

  GeeksForGeeks FilterChain
    

     Filter1
    GfgFilter1

  

     Filter2
    GfgFilter2

  

    Filter1
    /GfgServlet

  

    Filter2
    /GfgServlet

  

     Servlet
    GfgServlet

  

    Servlet
    /GfgServlet

  


创建过滤器:

之后,我们将实现 Filter 接口及其三个方法:

  • 初始化(过滤器配置)
  • doFilter(ServletRequest, ServletResponse, FilterChain)
  • 破坏()

我们还必须从 doFilter(ServletRequest, ServletResponse, FilterChain) 方法中调用 FilterChain 接口的 doFilter(ServletRequest, ServletResponse, FilterChain)函数。当调用 doFilter(request, response)函数时,将调用链接到 Servlet 的任何附加过滤器。如果没有更多的过滤器与 Servlet 关联,则最终执行请求的 Servlet。 doFilter(request,response) 方法在 servlet 完成执行后返回,允许您在响应对象中处理 Servlet 输出。

GfgFilter1。Java

Java

import java.io.*;
import javax.servlet.*;
  
public class GfgFilter1 implements Filter {
    public void init(FilterConfig filterConfig) {}
  
    public void destroy() {}
    // This method is called each time a client requests for
    // a web resource
    // which could be a Servlet or a JSP page i.e.
    // preprocessing request
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        out.println(
            " Filter1 is Filtering the request : ");
        out.println("Hello " + request.getParameter("name")
                    + "!");
  
        out.println("
");         out.println("
");         out.println("
");            // Calling  doFilter() calls the next filter in the         // chain will execute or if there is no filter then         // the requested web resource is executed.         chain.doFilter(request, response);            out.println("
");         out.println("
");         out.println("
");            // post-processing the request and after the         // requested web resource is called.         out.println(             " Filter2 is Filtering the response : ");         out.println("Bye " + request.getParameter("name")                     + "!");     } }

GfgFilter2。Java

Java

import java.io.*;
import javax.servlet.*;
  
public class GfgFilter2 implements Filter {
    public void init(FilterConfig filterConfig) {}
  
    public void destroy() {}
    // This method is called each time a client requests for
    // a web resource
    // which could be a Servlet or a JSP page i.e.
    // preprocessing request
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        out.println(
            " Filter2 is Filtering the request : ");
        out.println("Hello " + request.getParameter("name")
                    + "!");
  
        out.println("
");         out.println("
");         out.println("
");            // Calling  doFilter() calls the next filter in the         // chain will execute or if there is no filter then         // the requested web resource is executed.         chain.doFilter(request, response);            out.println("
");         out.println("
");         out.println("
");            // post-processing the request and after the         // requested web resource is called.         out.println(             " Filter1 is Filtering the response : ");         out.println("Bye " + request.getParameter("name")                     + "!");     } }

GfgServlet。 Java是一个 Servlet 类,在其随附的过滤器完成运行后运行。

GfgServlet。Java

Java

import java.io.*;
import javax.servlet.*;
public class GfgServlet extends GenericServlet {
    public void service(ServletRequest request,
                        ServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("GeekssForGeeks servlet is executed.");
    }
}

web.xml

根据Java Servlet 要求,每个基于 Servlet 的 Web 应用程序都必须包含一个名为 web.xml 的部署描述符文件(一个 XML 文件)。

XML


  GeeksForGeeks FilterChain
    

     Filter1
    GfgFilter1

  

     Filter2
    GfgFilter2

  

    Filter1
    /GfgServlet

  

    Filter2
    /GfgServlet

  

     Servlet
    GfgServlet

  

    Servlet
    /GfgServlet

  

输出:

在服务器上运行 index.html 文件,您将获得以下输出。

输出

单击提交按钮,将显示以下屏幕输出。

输出