📜  Servlet – 过滤器

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

Servlet – 过滤器

过滤器是在请求的预处理和后处理阶段使用的对象。过滤器主要用于过滤任务,例如服务器端日志记录、身份验证和授权、输入验证等。

Servlet - 过滤器

servlet 是可插入的,这意味着该条目是在 web.xml 文件中指定的。如果从 web.xml 文件中删除该条目,则过滤器将立即停用。它必须实现 javax.servlet 才能生成过滤器。因为 servlet 是用高度可移植的Java语言构建的,并且遵循一个通用框架,所以它们是非常可移植的。因此,它可以在彼此独立的服务器和操作系统中创建高级服务器扩展。

Servlet过滤器的需求:

  • 登录服务器。
  • 请求参数记录到日志文件。
  • 服务器上的身份验证和授权。
  • 压缩和解压
  • 加密和解密是两个不同的东西。
  • 服务器上的验证。

Servlet 过滤器的好处:

  • 它可以插入。
  • 过滤器不依赖于第三方资源。
  • 它几乎不需要维护。

Servlet 过滤器方法:

过滤器接口由 3 个生命周期方法组成。

Methods

Description

public void init(FilterConfig config) throws ServletExceptionThis invokes the web container to indicate to a filter that is being placed into service. It takes one parameter, i.e. FilterConfig type or FilterConfig object.
public void doFilters(ServletRequest request,ServletResponse response,FileterChain chain) throws ServletException,IOExceptionThe doFilter() function is called whenever a user requests a resource that is mapped to the filter. It’s used for filtering purposes.
public void destroy()When the filter is removed from the service, this function is only called once.

例子

在此示例中,我们将创建一个网页index.html ,其中包含“单击此处”的链接。单击链接以调用 servlet“ ServletFilter. Java ” 但是在这个 Servlet 执行之前,过滤器“ MyFilter. Java ”将被执行,这是在部署描述符中指定的。

索引.html

HTML

    
        HttpSession Event Listeners
    
    
        click here
    


Java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ServletFilter extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print(
            "
welcome to servlet filter example
");     } }


Java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter {
    FilterConfig config;
    public void init(FilterConfig config)
        throws ServletException
    {
        this.config = config;
    }
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain)
        throws IOException, ServletException
    {
        PrintWriter out = resp.getWriter();
        String s = config.getInitParameter("construction");
        if (s.equals("yes")) {
            out.print("This page is under construction");
        }
        else {
            // sends request to next resource
            chain.doFilter(req, resp);
        }
    }
    public void destroy() {}
}


XML


    
        ServletFilter
        ServletFilter
    
    
        ServletFilter
        /ServletFilter
    
    
        f1
        MyFilter
        
            construction
            no
        
    
    
        f1
        /servlet1
    


小服务过滤器。Java

Java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ServletFilter extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print(
            "
welcome to servlet filter example
");     } }

我的过滤器。Java

Java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter {
    FilterConfig config;
    public void init(FilterConfig config)
        throws ServletException
    {
        this.config = config;
    }
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain)
        throws IOException, ServletException
    {
        PrintWriter out = resp.getWriter();
        String s = config.getInitParameter("construction");
        if (s.equals("yes")) {
            out.print("This page is under construction");
        }
        else {
            // sends request to next resource
            chain.doFilter(req, resp);
        }
    }
    public void destroy() {}
}

web.xml

XML



    
        ServletFilter
        ServletFilter
    
    
        ServletFilter
        /ServletFilter
    
    
        f1
        MyFilter
        
            construction
            no
        
    
    
        f1
        /servlet1
    

输出:

点击“点击这里”

单击以下链接后,您将收到此消息