老师,这个为什么错了?

这是代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<input type="button" id="employee" value="员工列表" style="width:300px">
<input type="button" id="position" value="职工列表" style="width:300px">
<input type="button" id="department" value="部门列表" style="width:300px">
</div>
<div align="center" id="container"></div>
<script type="text/javascript">
var xmlhttp;
if(window.XMLHttpRequest){
xml=new XMLHttpRequest();
}else{
xml=new ActiveXObject("Microsoft","XMLHTTP");
}
document.getElementById("employee").onclick=function(){
xmlhttp.open("GET","/ajax/employee?flag=1",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var text=xmlhttp.responseText;
var json=JSON.parse(text);
var html="";
for(var i=0;i<json.length;i++){
var emp=json[i];
html=html+"<h3>"+emp+"</h3>";
}
document.getElementById("container").innerHTML=html;
}
}
}
</script>
</body>
</html>
package com.imooc.ajax;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 com.alibaba.fastjson.JSON;
/**
* Servlet implementation class EmployeeServlet
*/
@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EmployeeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list1=new ArrayList();
list1.add("小红");
list1.add("小明");
list1.add("小白");
List list2=new ArrayList();
list2.add("职员");
list2.add("经理");
List list3=new ArrayList();
list3.add("人事部");
list3.add("技术部");
list3.add("无线事业部");
String json1=JSON.toJSONString(list1);
String json2=JSON.toJSONString(list2);
String json3=JSON.toJSONString(list3);
response.setContentType("text/html;charset=UTF-8");
String num=request.getParameter("flag");
int num1=Integer.parseInt(num);
if(num1==1) {
response.getWriter().println(json1);
}else if(num1==2) {
response.getWriter().println(json2);
}else if(num1==3) {
response.getWriter().println(json3);
}
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星