题目:
解决技术:用户名使用中文代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Insert title here</title> <script type="text/javascript"> function _click(){ var loginname = document.getElementById("loginname").value; var loginpass = document.getElementById("loginpass").value; var xmlHttp = new XMLHttpRequest(); var url = "/aWeb/AjaxServlet?loginname="+loginname+"&loginpass="+loginpass; url = encodeURI(url); url = encodeURI(url); xmlHttp.open("POST",url,false); alert(url); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4 || xmlHttp.status == 200) { document.getElementById("result").innerHTML = xmlHttp.responseText; } }; xmlHttp.send(null); } </script> </head> <body> <form action="/aWeb/AjaxServlet" method="post"> 用户名:<input type="text" name="loginname" id="loginname" /> <br /> 密码:<input type="text" name="loginpass" id="loginpass" /> <br /> <input type="button" value="提交" onclick="_click()"> </form> <div id="result"></div> </body> </html>
package com.zving.servlet; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { doPost(req,resp); } @Override public void doPost(HttpServletRequest req,IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8;"); String loginname = req.getParameter("loginname"); String loginpass = req.getParameter("loginpass"); String str = URLEncoder.encode(loginname,"utf-8"); str = URLDecoder.decode(str,"utf-8"); System.out.println(loginname + "接收到信息!" + loginpass+str); resp.getWriter().print("用户:" + loginname + "登陆了!" + str); } }
解决思路:
在后天servlet类中使用str = URLDecoder.decode(str,"utf-8");
使用一个时发现没有用于是在使用一次。
方法二:tomcat使用高版本,1.7,1.8;jre使用1.7,1.8
在高版本中是可以的。
原文链接:https://www.f2er.com/ajax/161688.html