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

一:设置Post编码

post请求接收中文,需要在Servlet里写上 request.setCharacterEncoding("UTF-8"); 否则默认以iso-8859-1编码,中文显示乱码:webèé¢.doc,在每个Servlet里都写这句话,有点麻烦。

通过过滤器,统一设置post编码:

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

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

  最好是通过配置设置编码。这样修改方便<filter><init-param>…

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

java代码

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); 

    //设置响应编码,否则,默认为iso-8859-1,jsp展示中文乱码
    response.setContentType("text/html;charset=" + encoding);

//放行,必须要放行。
        chain.doFilter(request, response);
    }
    public void destroy() {
    }
}

 

第二步:将上面的类配置到web.xml
<!-- 编码过滤器 -->
  <filter>
      <filter-name>char</filter-name>
      <filter-class>com.lhy.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过滤 -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  

测试:request接收参数:  web考题.doc ,中文正常显示。

二:Get设置编码

在CharFilter中对reuqest进行包装。

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

第一步:声明包装类:
/**
 * 对get可以处理中文
 * 声明包装类
 * 在CharFilter中对reuqest进行包装。
 * 目的:修改增强getParameter方法,如果是get转码。
 */
public 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方法中,声明包装类的实例
public class CharFilter implements Filter{

  //声明编码的成员变量
  private String encoding;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    encoding = filterConfig.getInitParameter("bm"); 
  }
  
  @Override
  public void destroy() {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    request.setCharacterEncoding(encoding);
    //设置响应编码,否则,默认为iso-8859-1,jsp展示中文乱码
    response.setContentType("text/html;charset=" + encoding);
    // 判断是否需要包装
    HttpServletRequest req = (HttpServletRequest)request;
    if(req.getMethod().equals("GET")){
      request = new MyRequest(req);//实例化包装类。
    }
    //放行,必须要放行。
    chain.doFilter(request, response);
  }

}

test:

<a href="CharServlet?pwd=阿斯达">get</a>   req.getParameter("pwd");--阿斯达

 

转载于:https://www.cnblogs.com/lihaoyang/p/7374310.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值