Java Web笔记 - Servlet中的Filter过滤器的介绍和使用 编写过滤器

发布于 2011-11-12 | 更新于 2020-09-20

1、过滤器介绍:

在Servlet规范2.3中定义了过滤器,它能够对Servlet容器的请求和响应对象进行检查和修改。

Servlet过滤器本身并不生成请求和响应对象,只是提供过滤功能。

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

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

Servlet过滤器可以过滤的Web组件包括Servlet,JSP和HTML等文件。

Filter类似于IO中的过滤流,实现也类似于Servlet。

2、Filter接口:

所有的Servlet过滤器都必须实现javax.servlet.Filter接口,并实现该接口中的三个方法:

init(FilterConfig filterConfig)

Servlet过滤器的初始化方法,Servlet容器创建Servlet过滤器实例后将调用该方法。该方法将读取web.xml文件中Servlet过滤器的初始化参数。

doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

该方法完成实际的过滤操作,当客户端请求方法与过滤器设置匹配的URL时,Servlet容器将先调用过滤器的doFilter方法。FilterChain用户访问后续过滤器。

这里的ServletRequest和ServletResponse一般需要转换成具体的Servlet实现对于的对象,如:HttpServletRequest和HttpServletResponse。

destroy()

Servlet容器在销毁过滤器实例前调用该方法,在该方法中释放Servlet过滤器占用的资源。

public interface Filter
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.

Filters are configured in the deployment descriptor of a web application
Examples that have been identified for this design are

  1. Authentication Filters
  2. Logging and Auditing Filters
  3. Image conversion Filters
  4. Data compression Filters
  5. Encryption Filters
  6. Tokenizing Filters
  7. Filters that trigger resource access events
  8. XSL/T filters
  9. Mime-type chain Filter

public interface FilterChain

A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource. Filters use the FilterChain to invoke the next filter in the chain, or if the calling filter is the last filter in the chain, to invoke the resource at the end of the chain.

2.1、FilterConfig的使用:

Filter的init方法中提供了一个FilterConfig对象,提供相关的操作:

如获取Filter中配置的初始化参数:

LoginFilter com.itzhai.login.LoginFilter username arthinking

在init方法中获取:

@Override
public void init(FilterConfig filterConfig) throws ServletException {
//获取Filter初始化参数
String username = filterConfig.getInitParameter(“username”);
}

2.2、在Filter中访问application:

ServletContext context = filterConfig.getServletContext();

也可以在doFilter方法中根据转换好的request获取:

HttpServletRequest req = (HttpServletRequest)request;
ServletContext context = req.getSession().getServletContext();

3、一个简单过滤器的实现:

编写Filter过滤器:

public class LoginFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    System.out.println("init LoginFilter");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    //把ServletRequest和ServletResponse转换成真正的类型
    HttpServletRequest req = (HttpServletRequest)request;
    HttpSession session = req.getSession();

    //由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url
    String requestURI = req.getRequestURI();
    if(requestURI.endsWith("login.jsp")){
        chain.doFilter(request, response);
        return;
    }

    //判断用户是否登录,进行页面的处理
    if(null == session.getAttribute("user")){
        //未登录用户,重定向到登录页面
        ((HttpServletResponse)response).sendRedirect("login.jsp");
        return;
    } else {
        //已登录用户,允许访问
        chain.doFilter(request, response);
    }
}

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

}

在web.xml中配置Filter:

LoginFilter com.itzhai.login.LoginFilter LoginFilter /*

注意:一般Filter配置在所有的Servlet之前。

4、过滤敏感词汇的Filter简单实现:

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//转换成实例的请求和响应对象
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
//获取评论并屏蔽关键字
String comment = req.getParameter(“comment”);
comment = comment.replace(“A”, “***”);
//重新设置参数
req.setAttribute(“comment”, comment);
//继续执行
chain.doFilter(request, response);
}

5、Filter的执行顺序

Filter的执行顺序与在web.xml配置文件中的配置顺序一致,一般把Filter配置在所有的Servlet之前。

本文作者: arthinking

本文链接: https://www.itzhai.comjava-web-notes-servlet-filters-in-the-filter-writing-the-introduction-and-use-of-filters.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。