IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

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

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

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如下图的配置页面:

93_1.png

在此行添加编码格式:

93_2.png

控制台输出中文正常:

[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("你好");
    }

93_3.png

(3)解决方法:

加入代码:

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

93_4.png

可以正常显示。

3、get和post提交中文乱码(https://tech.souyunku.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>

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

93_5.png

(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";
      }
}

93_6.png

(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>

测试:

93_7.png

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>

测试:

93_8.png

文章永久链接:https://tech.souyunku.com/?p=22489


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(60) 打赏



未经允许不得转载:搜云库技术团队 » 中文乱码问题汇总(控制台、response、get和post、过滤器处理乱码、springmvc乱码)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367