中文乱码问题汇总(控制台responseget和post过滤器处理乱码springmvc乱码)
Posted zhai1997
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中文乱码问题汇总(控制台responseget和post过滤器处理乱码springmvc乱码)相关的知识,希望对你有一定的参考价值。
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方式提交:
表单:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="${pageContext.request.contextPath}/hello" method="get"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
springmvc的处理器:
@Controller public class HelloController{ @GetMapping("/hello") public String hello(String name,Model model){ model.addAttribute("msg",name); return "test"; } }
接收数据的表单:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
接收到的数据未出现乱码现象
(2)采用post方式提交:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="${pageContext.request.contextPath}/hello" method="post"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
@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>Encoding</filter-name> <filter-class>pers.zhb.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>Encoding</filter-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>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
测试:
以上是关于中文乱码问题汇总(控制台responseget和post过滤器处理乱码springmvc乱码)的主要内容,如果未能解决你的问题,请参考以下文章
ArcGIS Pro微课1000例0012:ArcGIS Pro属性表中文乱码完美解决办法汇总
ArcGIS Pro微课1000例0012:ArcGIS Pro属性表中文乱码完美解决办法汇总