Java Web过滤器笔记

来源:互联网 发布:广州多迪网络要交钱吗 编辑:程序博客网 时间:2024/05/22 07:52

Java Web过滤器笔记

Filter接口

web.xml定义过滤器

<filter>    <filter-name>filter1</filter-name>    <filter-class>web.CommentFilter1</filter-class>    <init-param>        <param-name>illgalStr</param-name>        <param-value>猪</param-value>    </init-param>  </filter><filter-mapping>    <filter-name>filter1</filter-name>    <url-pattern>/comment</url-pattern></filter-mapping>

过滤器方法:

  • void destroy()

    Called by the web container to indicate to a filter that it is being taken out of service.当一个过滤器被销毁时调用
  • void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

    The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.过滤器的doFilter方法在每次当一个request/response对通过chain 时被容器调用,由于一个对资源的客户端请求在chain的一端。
  • void init(FilterConfig filterConfig)

    Called by the web container to indicate to a filter that it is being placed into service.就是初始化过滤器的方法,在过滤器生命周期中只调用一次。被web容器调用,以指示一个过滤器正在投入服务。

FilterChain接口
需实现方法:

  • void doFilter(ServletRequest request, ServletResponse response)

    Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.导致链中的下一个过滤器被调用,或者如果调用过滤器是链中的最后一个过滤器,则会导致链末端的资源被调用。

实例

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CommentFilter1 implements Filter {    private FilterConfig config;    /**     * 容器启动之后,会立即创建过滤器实例 (只会创建一个实例)     */    public CommentFilter1() {        System.out.println("Filter1的构造器被调用");    }    public void destroy() {    }    /**     * 容器在完成初始化操作之后(即调用init方法), 会调用doFilter方法来处理请求。 如果调用了FilterChain(过滤器链)的     * doFilter方法,容器会继续向后调用。     */    public void doFilter(ServletRequest arg0, ServletResponse arg1,            FilterChain arg2) throws IOException, ServletException {        System.out.println("Filter1's doFilter begin...");        HttpServletRequest request = (HttpServletRequest) arg0;        HttpServletResponse response = (HttpServletResponse) arg1;        request.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=utf-8");        PrintWriter out = response.getWriter();        String comment = request.getParameter("comment");        String illgalStr = config.getInitParameter("illgalStr");        if (comment.indexOf(illgalStr) != -1) {            out.println("评论内容非法");        } else {            // 向后调用            arg2.doFilter(arg0, arg1);        }        System.out.println("Filter1's doFilter end.");    }    /**     * 容器在创建完过滤器实例之后,会立即 调用该实例的init方法。 该方法只会执行一次。     */    public void init(FilterConfig arg0) throws ServletException {        System.out.println("Filter1's init...");        config = arg0;    }}