Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

中文乱码真的很烦人的。而且每次都要写Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第2张,可麻烦了,而且有时候写了还不一定管用,所以我们可以试试过滤器

1.每个jsp头上当然要写上utf8啦

Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第3张

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

 

2.然后添加一个过滤器

Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第4张

在过滤器的doFilter里写上

Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第5张

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here

        /*
         * 设置request、response的编码
         */
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

 

3.别忘了在web.xml里配置

Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第6张

  <filter>
    <filter-name>CharFilter</filter-name>
    <filter-class>com.xx17.cys.filter.CharacterFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CharFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

大功告成!!!

Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器) 随笔 第7张

 

 舒服!!!

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄