Java Servlet Filter 详解

简介: Servlet Filter 可以拦截所有指向服务端的请求。Servlet Filter.png如果你想创建一个ServletFilter ,你需要实现一个接口javax.

Servlet Filter 可以拦截所有指向服务端的请求。

img_2a41fb5fa44f682fc1667c60f305fae1.png
Servlet Filter.png

如果你想创建一个ServletFilter ,你需要实现一个接口javax.servlet.Filter

import javax.servlet.*;
import java.io.IOException;

public class SimpleServletFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                                    FilterChain filterChain)
    throws IOException, ServletException {

    }

    public void destroy() {
    }
}

servlet filter 一旦被装载,首先会调用它的init()方法。

当HTTP请求指向过滤器截获的服务端,过滤器可以检查URI,请求参数和请求头,并根据它决定是否要将请求阻止或转发到目标Servlet,JSP 等

具有拦截功能的方法是doFilter()

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain filterChain)
throws IOException, ServletException {

    String myParam = request.getParameter("myParam");

    if(!"blockTheRequest".equals(myParam)){
        filterChain.doFilter(request, response);
    }
}

Notice how the doFilter() method checks a request parameter, myParam, to see if it equals the string "blockTheRequest". If not, the request is forwarded to the target of the request, by calling the filterChain.doFilter() method. If this method is not called, the request is not forwarded, but just blocked.

The servlet filter above just ignores the request if the request parameter myParam equals "blockTheRequest". You can also write a different response back to the browser. Just use the ServletResponse object to do so, just like you would inside a servlet.

You may have to cast the ServletResponse to a HttpResponse to obtain a PrintWriter from it. Otherwise you only have the OutputStream available via getOutputStream().

Here is an example:
这个doFilter()方法检查request参数:myParam,看它是不是和"blockTheRequest"相爱南瓜灯,如果不是,这个请求会被filterChain.doFilter()方法调用,如果没有被调用,线程挂起。如果相等,你能在ServletResponse对象中写一写返回给浏览器的数据。

必须将ServletResponse强制转换为HttpResponse才可以从中获取PrintWriter。 否则,只能通过getOutputStream()获得OutputStream。

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain filterChain)
throws IOException, ServletException {

    String myParam = request.getParameter("myParam");

    if(!"blockTheRequest".equals(myParam)){
        filterChain.doFilter(request, response);
        return;
    }

    HttpResponse httpResponse = (HttpResponse) httpResponse;
    httpResponse.getWriter().write("a different response... e.g in HTML");
}

在web.xml里配置过滤器/拦截器

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>servlets.SimpleServletFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>*.simple</url-pattern>
</filter-mapping>

通过这种配置,所有URL以.simple结尾的请求都将被servlet过滤器拦截。

相关文章
|
1天前
|
JavaScript Java 容器
servlet过滤器Filter简要回顾-过滤请求字符编码,/和/*和/**的区别
本文简要回顾了Servlet过滤器Filter的概念和使用,通过实例演示了如何创建过滤器以过滤请求字符编码,并解释了在web.xml中配置过滤器时使用`/`、`/*`和`/**`的区别。
servlet过滤器Filter简要回顾-过滤请求字符编码,/和/*和/**的区别
|
29天前
|
缓存 安全 Java
Java服务器端技术:Servlet与JSP的集成与扩展
Java服务器端技术:Servlet与JSP的集成与扩展
22 3
|
29天前
|
存储 缓存 前端开发
Servlet与JSP在Java Web应用中的性能调优策略
Servlet与JSP在Java Web应用中的性能调优策略
24 1
|
1月前
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
32 3
|
29天前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
基于Servlet和JSP的Java Web应用开发指南
17 0
|
29天前
|
前端开发 安全 Java
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
16 0
|
30天前
|
安全 Java API
Java 8 流库的魔法革命:Filter、Map、FlatMap 和 Optional 如何颠覆编程世界!
【8月更文挑战第29天】Java 8 的 Stream API 通过 Filter、Map、FlatMap 和 Optional 等操作,提供了高效、简洁的数据集合处理方式。Filter 用于筛选符合条件的元素;Map 对元素进行转换;FlatMap 将多个流扁平化合并;Optional 安全处理空值。这些操作结合使用,能够显著提升代码的可读性和简洁性,使数据处理更为高效和便捷。
34 0
|
2月前
|
安全 Java API
Java中的Servlet编程详解
Java中的Servlet编程详解
|
2月前
|
Java 数据库连接 开发者
Java中的Servlet生命周期详解
Java中的Servlet生命周期详解
|
2月前
|
安全 Java API
Java中的Servlet编程详解
Java中的Servlet编程详解