中文乱码问题汇总(控制台、response、get和post、过滤器处理乱码、springmvc乱码)

2021-02-01 19:15

阅读:578

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:mode   进入   输出   extends   ref   out   方法   str   代码   

1、IDEA控制台乱码

(1)在书写普通的java程序输出中文的时候,不会出现控制台乱码的问题:

public class Test {
    public static void main(String[] args) {
        System.out.println("劳动光荣");
    }
}
劳动光荣

(2)在servlet输出中文的时候,控制台出现乱码的问题:

public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           System.out.println("劳动光荣");
    }
}
[2020-05-01 01:21:20,094] Artifact mvc_01:war exploded: Artifact is deployed successfully
[2020-05-01 01:21:20,095] Artifact mvc_01:war exploded: Deploy took 3,243 milliseconds
???????

(3)解决方案:

在IDEA中作如下配置,进入IDEA如下图的配置页面:

技术图片

 在此行添加编码格式:

技术图片

 控制台输出中文正常:

[2020-05-01 01:23:02,634] Artifact mvc_01:war exploded: Artifact is deployed successfully
[2020-05-01 01:23:02,634] Artifact mvc_01:war exploded: Deploy took 4,541 milliseconds
劳动光荣

 

2、response中文乱码

(1)要确定代码的编码格式为UTF-8

(2)乱码原因:浏览器和服务器的编码格式不同:

服务器的默认编码为:ISO-8859-1,如果浏览器的编码不是ISO-8859-1,就会出现乱码:

 public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        response.getWriter().write("你好");
    }

 技术图片

 (3)解决方法:

加入代码:

    response.setCharacterEncoding("UTF-8");//设置服务器的编码,默认是ISO-8859-1
    response.setContentType("text/html; charset = utf-8");//告诉浏览器服务器的编码格式

技术图片

可以正常显示。

 

3、get和post提交中文乱码(https://www.cnblogs.com/zhai1997/p/12489507.html)

 

4、过滤器解决中文乱码问题

(1)使用表单采取get方式提交:

表单:

$Title$

springmvc的处理器:

@Controller
public class HelloController{
    @GetMapping("/hello")
      public String hello(String name,Model model){
          model.addAttribute("msg",name);
          return "test";
      }
}

接收数据的表单:


Title
${msg}

接收到的数据未出现乱码现象

技术图片

 

 (2)采用post方式提交:


  $Title$
@Controller
public class HelloController{
    @PostMapping("/hello")
      public String hello(String name,Model model){
          model.addAttribute("msg",name);
          return "test";
      }
}

技术图片

 

 (3)采用过滤器解决乱码问题:任何请求都会经过该过滤器

书写过滤器的代码:

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {

    }
}

在web.xml中配置过滤器:

    filter>
        filter-name>Encodingfilter-name>
        filter-class>pers.zhb.filter.EncodingFilterfilter-class>
    filter>
    filter-mapping>
        filter-name>Encodingfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>

测试:

技术图片

 

 

5、springmvc解决中文乱码问题(get方式乱码,post方式不会)

get方式提交并采用get方式接收数据:

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @GetMapping("/t1")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("msg","hhhha");
        return "test";
    }
}

不需要自己书写过滤器,直接在web.xml中配置过滤器即可:

filter>
        filter-name>encodingFilterfilter-name>
        filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        init-param>
            param-name>encodingparam-name>
            param-value>UTF-8param-value>
        init-param>
    filter>
    filter-mapping>
        filter-name>encodingFilterfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>

测试:

技术图片

 

中文乱码问题汇总(控制台、response、get和post、过滤器处理乱码、springmvc乱码)

标签:mode   进入   输出   extends   ref   out   方法   str   代码   

原文地址:https://www.cnblogs.com/zhai1997/p/12813363.html


评论


亲,登录后才可以留言!