你的位置:首页 > Java教程

[Java教程]过虑器基础2


过虑器链FilterChain

  当一个web项目中有多个连接器,且这些过虑器对同个url进行过虑。这时就形成了过虑器链/。

在filter的配置当中,有两个配置:filter,filter-mapping.

       Filter-mapping在前,则先执行,在后则后执行。

<filter>

    <filter-name>first</filter-name>

    <filter-class>cn.itcast.filter.FirstFilter</filter-class>

  </filter>

    <filter>

    <filter-name>second</filter-name>

    <filter-class>cn.itcast.filter.SecondFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>second</filter-name>

    <url-pattern>/four</url-pattern>

  </filter-mapping>

   <filter-mapping>

    <filter-name>first</filter-name>

    <url-pattern>/four</url-pattern>

  </filter-mapping>

过虑器应用之1-设置request编码


    写一个过虑器,对所有url全部过虑,/*.在doFilter方法中,设置request的编码为utf-8。

一般情况下,这个过虑器永远是第一个要执行的过虑器。

    最好是通过配置设置编码。<filter><init-param>…

第一步:实现Filter接口,在doFIlter中接收初始化参数,设置编码

package cn.hongxin.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 CharFilter implements Filter {

    //声明编码的成员变量

    private String encoding;

    public void init(FilterConfig config) throws ServletException {

       encoding = config.getInitParameter("bm"); 

    }

    public void doFilter(ServletRequest request, ServletResponse response,

           FilterChain chain) throws IOException, ServletException {

       request.setCharacterEncoding(encoding);

       //放行,必须要放行。

       chain.doFilter(request, response);

    }

    public void destroy() {

    }

}

第二步:将上面的类配置到web.

<filter>

    <filter-name>char</filter-name>

    <filter-class>cn.itcast.filter.CharFilter</filter-class>

    <init-param>

        <!-- 为了便于配置,在配置文件中设置编码 -->

        <param-name>bm</param-name>

        <param-value>UTF-8</param-value>

    </init-param>

  </filter>

    <filter-mapping>

       <filter-name>char</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>


 

对get可以处理中文

在CharFilter中对reuqest进行包装。

    目的:修改增强getParameter方法,如果是get转码。

第一步:声明包装类:

//声明包装类

class MyRequest extends HttpServletRequestWrapper{

    public MyRequest(HttpServletRequest request) {

       super(request);

    }

    //增强getParamter

    @Override

    public String getParameter(String name) {

       String val = super.getParameter(name);

       if(super.getMethod().equals("GET")){

           try {

              val = new String(val.getBytes("ISO-8859-1"),super.getCharacterEncoding());

           } catch (UnsupportedEncodingException e) {

              e.printStackTrace();

           }

       }

       return val;

    }

}




 

第二步:在doFilter方法中,声明包装类的实例

package cn.hongxin.filter;

 

import java.io.IOException;

import java.io.UnsupportedEncodingException;

 

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.HttpServletRequestWrapper;

 

public class CharFilter implements Filter {

    // 声明编码的成员变量

    private String encoding;

 

    public void init(FilterConfig config) throws ServletException {

       encoding = config.getInitParameter("bm");

    }

 

    public void doFilter(ServletRequest request, ServletResponse response,

           FilterChain chain) throws IOException, ServletException {

       request.setCharacterEncoding(encoding);

       response.setContentType("text/html;charset=" + encoding);

       // 判断是否需要包装

       HttpServletRequest req = (HttpServletRequest) request;

       if (req.getMethod().equals("GET")) {

           request = new MyRequest(req);

       }

       // 声明包装类的实例

       // 放行

       chain.doFilter(request, response);

    }

    public void destroy() {

    }

}

 

// 声明包装类

class MyRequest extends HttpServletRequestWrapper {

    public MyRequest(HttpServletRequest request) {

       super(request);

    }

 

    // 增强getParamter

    @Override

    public String getParameter(String name) {

       String val = super.getParameter(name);

       try {

           val = new String(val.getBytes("ISO-8859-1"),

                  super.getCharacterEncoding());

       } catch (UnsupportedEncodingException e) {

           e.printStackTrace();

       }

       return val;

    }

}