为什么后端接受的是乱码
我通过post方法传递数据,现在为什么servlet接受的数据是乱码?
package com.imooc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
@WebServlet("/ManageServlet")
public class ManageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("flag");
System.out.println(name);
JSONObject json=null;
if(name.equals("员工列表")){
json=new JSONObject("{html:小红<br>小明<br>小白}");
} else if(name.equals("职位列表")){
json=new JSONObject("{html:职工<br>经理}");
} else {
json=new JSONObject("{html:人事部<br>技术部<br>无线事业部}");
}
response.getOutputStream().write(json.toString().getBytes("UTF-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="resource/js/jquery-3.3.1.js"></script>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
</head>
<body>
<input type="button" value="员工列表" onclick="showYG()" id="YG">
<input type="button" value="职位列表" onclick="showZW()" name="ZW">
<input type="button" value="部门列表" onclick="showOU()" name="OU">
<div id="content"></div>
</body>
<script>
function showYG(){
var xmlHttp=new XMLHttpRequest();
var params="flag="+$("input[name=ZW]").val();
xmlHttp.open("POST","<%=basePath %>/ManageServlet",true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState=="4"&&xmlHttp.status=="200"){
var result=xmlHttp.responseText;
document.getElementById("content").innerHTML=result;
}
}
}
function showZW(){
var xmlHttp=new XMLHttpRequest();
var params="flag="+$("input[name=ZW]").val();
xmlHttp.open("post", "<%=basePath %>/ManageServlet", true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState=="4"&&xmlHttp.status=="200"){
var result=xmlHttp.responseText;
document.getElementById("content").innerHTML=result;
}
}
}
function showOU(){
var xmlHttp=new XMLHttpRequest();
var params="flag="+$("input[name=OU]").val();
xmlHttp.open("post", "<%=basePath %>/ManageServlet", true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState=="4"&&xmlHttp.status=="200"){
var result=xmlHttp.responseText;
document.getElementById("content").innerHTML=result;
}
}
}
</script>
</html>请老师指点一下
0
收起
正在回答 回答被采纳积分+1
1回答
一叶知秋519
2018-11-09 18:11:18
1、后端接收乱码:
对name以ISO-8859-1进行解码,然后再对其以UTF-8的形式进行重新编码,即
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
这样再打印name时,就不会出现乱码了。
2、关于同学代码的几点建议:
1)获取员工时,

应该将input标签内的id="YG"改为name="YG"
在showYG()方法中,红框内应该是name=YG
2)由于是使用JavaScript版本的Ajax,接受返回数据时,是通过
var result=xmlHttp.responseText;
responseText 获取到的是字符串
所以在Servlet中直接回传String类型的数据就可以啦~
即
通过上面的修改以后,同学的代码就符合作业要求了。
如果解决了你的疑惑,请采纳,祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星