总觉得哪里写的不对,老师帮忙看看。。。
package com.imooc.ajax.test; /** * 员工类 * @author 888 * */ public class Employee { private String name;//名字 private String job;//职务 private String department;//部门 public Employee() { } public Employee(String name, String job,String department) { super(); this.name = name; this.department = department; this.job = job; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } }
package com.imooc.ajax.test; 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; @WebServlet("/emp") public class EmployeeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List list=new ArrayList(); list.add(new Employee("小红","职员","人事部")); list.add(new Employee("小明","经理","技术部")); list.add(new Employee("小白","职员","无线事业部")); String json=JSON.toJSONString(list); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(json); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <input style="width: 200px" id="inp1" type="button" value="员工列表"> <input style="width: 200px" id="inp2" type="button" value="职位列表"> <input style="width: 200px" id="inp3" type="button" value="部门列表"> </div> <div id="content" align="center"></div> <script type="text/javascript"> var xmlhttp; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ smlhttp=new ActiveXOject("Microsoft","XMLHTTP"); } xmlhttp.open("GET","/ajax/emp",true); xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ var text=xmlhttp.responseText; var json=JSON.parse(text); //员工列表 document.getElementById("inp1").onclick=function(){ var html=""; for(emp in json){ html+=json[emp].name+"<br>"; } document.getElementById("content").innerHTML=html; } //职位列表 document.getElementById("inp2").onclick=function(){ var html=""; var num=0; for(emp in json){ var job=json[emp].job; if(job=='职员'){ num++; } if(num>1){ job=""; } html+=job+"<br>"; } document.getElementById("content").innerHTML=html; } //部门列表 document.getElementById("inp3").onclick=function(){ var html=""; for(emp in json){ html+=json[emp].department+"<br>"; } document.getElementById("content").innerHTML=html; } } } </script> </body> </html>
0
收起
正在回答 回答被采纳积分+1
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星