解决Ajax前台中文传到后台出现中文乱码

解决办法是前台两次编码, 后台一次解码即可.

前台jsp文件

1 var text = "张三";
3 var username = encodeURI(encodeURI(text));

后台servlet代码

1 String username =URLDecoder.decode("对应字段","utf-8");

简单登录校验案例.

案例说明:为验证Ajax请求后端控制台中文乱码问题是否解决.

简单登录界面, 用户输入用户名登录. 若用户名未输入用户名则提示"用户名不能为空";若用户名输入为"张三"则提示用户名已被占用.其它均无提示.

前台页面index.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: hejjon
 4   Date: 2019/6/3
 5   Time: 10:19
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <%
10     String path = request.getContextPath();
11     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
12             + path + "/";
13 %>
14 <html>
15 <head>
16     <base href="<%=basePath%>">
17     <title>用户登录界面</title>
18     <script type="text/javascript">
19         var xhr;
20         function change() {
21 
22             var val = document.getElementById("uname").value;
23             var uname = encodeURI(encodeURI(val));
24 
25             if (uname === "") {
26                 document.getElementById("namespan").innerHTML = "用户名不能为空";
27             } else {
28                 // 创建xhr对象
29                 xhr = new XMLHttpRequest();
30                 // 与服务器建立连接
31                 xhr.open("get", "servlet/login?uname=" + uname, true);
32                 // 执行回调函数
33                 xhr.onreadystatechange = process;
34                 // 发送数据
35                 xhr.send(null);
36             }
37             
38         }
39          
40         function process() {
41            if (xhr.readyState == 4 && xhr.status == 200) {
42                // 接收后台响应
43                var text = xhr.responseText;
44                document.getElementById("namespan").innerHTML = text;
45            }
46         }
47     </script>
48 </head>
49 <body>
50 <p>
51     用户: <input type="text" name="uname" id="uname" onblur="change()"/><span id="namespan"></span>
52 </p>
53 <p>
54     密码: <input type="password" name="pwd" id="pwd"/>
55 </p>
56 </body>
57 </html>

后台LoginServlet.java

 1 import javax.servlet.ServletException;
 2 import javax.servlet.annotation.WebServlet;
 3 import javax.servlet.http.HttpServlet;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import java.io.IOException;
 7 import java.io.PrintWriter;
 8 import java.net.URLDecoder;
 9 
10 
11 @WebServlet("/servlet/login")
12 public class LoginServlet extends HttpServlet {
13     @Override
14     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         // 设置后台响应文本格式
16         resp.setContentType("text/html;charset=utf-8");
17         // 接收前台请求
18         String uname = URLDecoder.decode(req.getParameter("uname"));
19 
20         // System.out.println(uname);
21 
22         PrintWriter out = resp.getWriter();
23         if ("张三".equals(uname)) {
24             out.print("用户名已被占用");
25         } else {
26             out.print("用户名可用");
27         }
28     }
29 }
30     

猜你喜欢

转载自www.cnblogs.com/hejjon/p/10966438.html