servlet 过滤器

来源:互联网 发布:java敏感词过滤算法 编辑:程序博客网 时间:2024/05/14 06:00

1:过滤器的概念

过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应。

在请求到达Servlet/JSP之前,过滤器截获请求。

在响应送给客户端之前,过滤器截获响应。

多个过滤器形成一个过滤器链,过滤器链中不同过滤器的先后顺序由部署文件web.xml中过滤器映射<filter-mapping>的顺序决定。

最先截获客户端请求的过滤器将最后截获Servlet/JSP的响应信息。

2:过滤器的链式结构

可以为一个Web应用组件部署多个过滤器,这些过滤器组成一个过滤器链,每个过滤器只执行某个特定的操作或者检查。这样请求在到达被访问的目标之前,需要经过这个过滤器链。
这里写图片描述

3:实现过滤器

在Web应用中使用过滤器需要实现javax.servlet.Filter接口,实现Filter接口中所定义的方法,并在web.xml中配置过滤器。

public class MyFilter implements Filter {    public void init(FilterConfig fConfig) throws ServletException {        // init 方法在 Filter 生命周期中仅执行一次,web 容器在调用 init 方法时    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        // 在这里可以对客户端请求进行检查        // 沿过滤器链将请求传递到下一个过滤器。        chain.doFilter(request, response);        // 在这里可以对响应进行处理    }    public void destroy() {        // 在Web容器卸载 Filter 对象之前被调用。        // 该方法在Filter的生命周期中仅执行一次。        // 在这个方法中,可以释放过滤器使用的资源。    }}

web.xml

<filter>    <filter-name>MyFilter</filter-name>    <filter-class>com.szmsd.filter.MyFilter</filter-class></filter><filter-mapping>    <filter-name>MyFilter</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

事例:字符编码过滤器

package com.szmsd.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class EncodeingFiter implements Filter {    String charEncodeing = "";    public EncodeingFiter() {    }    public void init(FilterConfig fConfig) throws ServletException {        charEncodeing = fConfig.getInitParameter("charEncodeing");        System.out.println("初始化字符编码 " + charEncodeing);        if(charEncodeing == null){            throw new ServletException("charEncodeing不能为空!");        }    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        if(!charEncodeing.equals(request.getCharacterEncoding())){            request.setCharacterEncoding(charEncodeing);        }        response.setCharacterEncoding(charEncodeing);        chain.doFilter(request, response);    }    public void destroy() {    }}
<?xml version="1.0" encoding="utf-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    version="2.5">    <display-name>Archetype Created Web Application</display-name>    <!-- 字符编码过滤器 -->    <filter>        <filter-name>EncodeingFiter</filter-name>        <filter-class>com.szmsd.filter.EncodeingFiter</filter-class>        <init-param>            <param-name>charEncodeing</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>EncodeingFiter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>