二、中文乱码问题

一、问题提出

示例代码:
用户注册的jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户注册</title>
</head>
<body>
    <form name="registerForm" id="registerForm" action="doUserCreate.jsp" method="get">
    <table class="tb" border="0" cellspacing="5" cellpadding="0" align="center">
        <tr><td align="center" colspan="2" style="text-align:center;" class="text_tabledetail2">用户注册</td></tr>
        <tr>
            <td class="text_tabledetail2">用户名</td>
            <td><input type="text" name="username" value=""/></td>
        </tr>
        <tr>
            <td class="text_tabledetail2">密码</td>
            <td><input type="password" name="password" value=""/></td>
        </tr>
        <tr>
            <td class="text_tabledetail2">确认密码</td>
            <td><input type="password" name="con_password" value=""/></td>
        </tr>
        <tr>
            <td class="text_tabledetail2">email</td>
            <td><input type="text" name="email" value=""/></td>
        </tr>
        <tr>
            <td class="text_tabledetail2">爱好</td>
            <td>
                <input type="checkbox" name="hobby" value="swim" />游泳<br/>
                <input type="checkbox" name="hobby" value="read" />阅读<br/>
                <input type="checkbox" name="hobby" value="climb" />爬山<br/>
                <input type="checkbox" name="hobby" value="travel" />旅游<br/>
            </td>
        </tr>
        <tr>
            <td style="text-align:center;" colspan="2">             
                <button type="submit" class="page-btn" name="save">注册</button>
                <button type="button" class="page-btn" name="return" onclick="javascript:location.href='<%=request.getContextPath() %>/index.jsp'">返回</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

处理用户请求的jsp页面:

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doUserCreate.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
        // 获取用户注册的数据:用户名、密码、email、爱好
        String username = request.getParameter("username");
        String pwd = request.getParameter("password");
        String email = request.getParameter("email");
        String[] hobbys = request.getParameterValues("hobby");
     %>
     用户名:<%
           if(username != null && !username.equals("")){
              %>
              <% out.println(username); %>
              <%
           } else {
              out.println("1");
           }

      %><br/>
     密码:<%=pwd %><br/>
     email:<%=email %><br/>
     爱好:<%
                if(hobbys != null && hobbys.length != 0){
                    // 用户选择了爱好,获得爱好并输出
                    for(String hobby : hobbys) {
                        out.println(hobby);
                    }
                }else{
                    out.println("没有选择任何爱好");
                }
           %><br/>
  </body>
</html>

当在运行结果里面如下输入的时候:


image.png

用户处理界面的结果如下:


image.png

可以看到出现了中文乱码。

二、解决方法

表单提交到jsp,可能会出现中文乱码问题。对于不同的表单提交方式,处理方法不同。
常见的表单提交方式有get和post。


image.png

url可传播是指的下面这种方式:


image.png

2.1post

当表单提交方式为post的时候,只需要加这么一句话:

request.setCharacterEncoding("utf-8");

加完之后的处理提交数据的jsp代码如下:

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doUserCreate.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
         // 表单post提交,中文乱码处理
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        // 获取用户注册的数据:用户名、密码、email、爱好
        String username = request.getParameter("username");
        String pwd = request.getParameter("password");
        String email = request.getParameter("email");
        String[] hobbys = request.getParameterValues("hobby");
     %>
     用户名:<%
           if(username != null && !username.equals("")){
              %>
              <% out.println(username); %>
              <%
           } else {
              out.println("1");
           }

      %><br/>
     密码:<%=pwd %><br/>
     email:<%=email %><br/>
     爱好:<%
                if(hobbys != null && hobbys.length != 0){
                    // 用户选择了爱好,获得爱好并输出
                    for(String hobby : hobbys) {
                        out.println(hobby);
                    }
                }else{
                    out.println("没有选择任何爱好");
                }
           %><br/>
  </body>
</html>

在上面的代码中,

 response.setCharacterEncoding("utf-8");

这句在已经设置了jsp的默认字符编码为utf-8的情况下是可以不加的。

2.2get

第一种方式(该方式治标不治本)

示例代码:

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doUserCreate.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
         // 表单post提交,中文乱码处理
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        // 获取用户注册的数据:用户名、密码、email、爱好
        String username = request.getParameter("username");
        byte[] userNames = username.getBytes("ISO-8859-1");
        username = new String(userNames, "utf-8");
        
        String pwd = request.getParameter("password");
        String email = request.getParameter("email");
        String[] hobbys = request.getParameterValues("hobby");
     %>
     用户名:<%
           if(username != null && !username.equals("")){
              %>
              <% out.println(username); %>
              <%
           } else {
              out.println("1");
           }

      %><br/>
     密码:<%=pwd %><br/>
     email:<%=email %><br/>
     爱好:<%
                if(hobbys != null && hobbys.length != 0){
                    // 用户选择了爱好,获得爱好并输出
                    for(String hobby : hobbys) {
                        out.println(hobby);
                    }
                }else{
                    out.println("没有选择任何爱好");
                }
           %><br/>
  </body>
</html>

这种方式的代码如下:

String username = request.getParameter("username");
 byte[] userNames = username.getBytes("ISO-8859-1");
username = new String(userNames, "utf-8");

或者也可以合写为一句话:

username=new String(username.getBytes("ISO-8859-1"),"utf-8" );

其逻辑是:将获取的字符串打散,然后重新拼接到一起,在这个过程中改变字符编码格式。
但是这种方式的缺点是只能改变这一个地方的编码方式,而其他地方如果有中文的话则需要再写一遍这段代码。

第二种方式:修改tomcat的配置文件

1、找到tomcat的安装路径——conf——server.xml
打开这个server.xml文件之后,去找设置端口号那个地方,如下所示:


image.png

2、如下修改文件


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。