zoukankan      html  css  js  c++  java
  • Java Web开发中的乱码问题

    POST方法乱码:

    1:存在乱码的示例:

    前端页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" //设置UTF8编码
        pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <!-- 头作用,告诉浏览器UTF8编码 -->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <base href="<%=basePath%>">
    <title>Insert title here</title>
    </head>
    <body>
        <div align="center">
            <form action="Encode" method="post">
                Name:<input name="name" type="text" /> <br> Sex:<input
                    type="text" name="sex" /><br>
                <button type="submit">发送</button>
            </form>
        </div>
    
    
    </body>
    </html>

    后台Servlet接受参数:

    package com.daxin;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Encode
     */
    public class Encode extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
            
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //默认编码是ISO8859-1
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            System.out.println(name);
            System.out.println(sex);
    
        }
    
    }

    当我们启动输入中文时候,打印的是:

    ?¤§é??
    ??·

    乱码,这个问题的原因就是:Servlet的方法的所有方法默认解码都是ISO-8859-1,而我们前端页面编码是UTF8,所以导致解码乱码!这种方式也存在解决方案:

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //默认编码是ISO8859-1
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            
            System.out.println(name);
            System.out.println(sex);
    
            //由于上面是ISO编码错误,所以还原之后再使用UTF8编码
            String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8");
            
            System.out.println(newName);
            System.out.println(newSex);
            
        }

    第二种更优解决方案:设置全局的UTF-8编码

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            //全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的。所以该设置只对post起作用。get方法不起作用
            request.setCharacterEncoding("UTF-8");
            //默认编码是ISO8859-1
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            
            System.out.println(name);
            System.out.println(sex);
    }

    GET乱码问题:

    由于request.setCharacterEncoding("UTF-8");方法只对实体内容编码起作用,换言之就是只对Post有效,对于GET方法请求依旧无法生效。所以针对Get请求我们只能手动解码:

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            //默认编码是ISO8859-1
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            //由于上面是ISO编码错误,所以还原之后再使用UTF8编码
            String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            String newSex = new String(sex.getBytes("ISO-8859-1"),"UTF-8");
            
            System.out.println(newName);
            System.out.println(newSex);
            
        }

    但是通常我们一般都只是实现一个Post方法逻辑,而是将GET请求逻辑转给Post处理,所以此时比较健全的写法是:

    package com.daxin;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Encode
     */
    public class Encode extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
    
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 全局设置UTF8编码,该方法只对实体内容中的数据起作用。post的数据是实体内容中的
            request.setCharacterEncoding("UTF-8");
            // 默认编码是ISO8859-1
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            if ("GET".equals(request.getMethod())) {
                name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
            }
    
            if ("GET".equals(request.getMethod())) {
                sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8");
            }
    
            System.out.println(name);
            System.out.println(sex);
    
        }
    
    }

    结论:乱码问题出现,就是因为编码和解码时候使用的不是一个编码,最终导致乱码。所以解决问题的核心思路就是使用同一个编码解码器。前端一般使用的是UTF8编码,后台默认是ISO8859-1,所以我们将后台解码器使用UTF8即可解决编码问题。

  • 相关阅读:
    javaweb消息中间件——rabbitmq入门
    virtual box 桥接模式(bridge adapter)下无法获取ip(determine ip failed)的解决方法
    Apache Kylin本地启动
    git操作
    Java学习总结
    Java中同步的几种实现方式
    hibernate exception nested transactions not supported 解决方法
    vue 中解决移动端使用 js sdk 在ios 上一直报invalid signature 的问题解决
    cookie 的使用
    vue 专门为了解决修改微信标题而生的项目
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7510862.html
Copyright © 2011-2022 走看看