还有要改进的地方吗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><style type="text/css">#all{ width: 600px; height: 50px; background-color: #D0D0D0; margin: auto;}.b{ width: 196px; height: 50px; margin: auto; background-color: #F0F0F0; font-size: 15px;}#list{ width: 600px; height: 600px; text-align: center; font-size: 20px; background-color: #F0F0F0; margin: auto;}</style></head><body> <div id="all"> <button class="b" id="emp">员工列表</button> <button class="b" id="depart">部门列表</button> <button class="b" id="postion">职位列表</button> </div> <div id="list"></div> <script type="text/javascript"> //创建XMLHttpRequest对象 var xmlhttp ; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } //实现员工列表 document.getElementById("emp").onclick = function(){ //发送Ajax请求 xmlhttp.open("GET","/Ajax/Dpart?flag=1",true); xmlhttp.send(); //处理服务器响应 xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ //获取响应文本 var text = xmlhttp.responseText; var json = JSON.parse(text); console.log(json); var html = ""; for(var i = 0; i < json.length; i ++){ var emp = json[i]; html = html + "<h3>" + emp + "</h3>"; document.getElementById("list").innerHTML = html; } } } } //实现部门列表 document.getElementById("depart").onclick = function(){ //发送Ajax请求 xmlhttp.open("GET" , "/Ajax/Dpart?flag=2",true); xmlhttp.send(); //处理服务器响应 xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ //获取响应文本 var text = xmlhttp.responseText; var json = JSON.parse(text); console.log(json); var html = ""; for(var i = 0; i < json.length; i ++){ var emp = json[i]; html = html + "<h3>" + emp + "</h3>"; document.getElementById("list").innerHTML = html; } } } } //实现职位列表 document.getElementById("postion").onclick = function(){ //发送Ajax请求 xmlhttp.open("GET" , "/Ajax/Dpart?flag=3",true); xmlhttp.send(); //处理服务器响应 xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ //获取响应文本 var text = xmlhttp.responseText; var json = JSON.parse(text); console.log(json); var html = ""; for(var i = 0; i < json.length; i ++){ var emp = json[i]; html = html + "<h3>" + emp + "</h3>"; document.getElementById("list").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 Dpartment
*/
@WebServlet("/Dpart")
public class Dpartment extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
List<String> department = new ArrayList<>();
List<String> employee = new ArrayList<>();
List<String> position = new ArrayList<>();
department.add("人事部");
department.add("市场部");
department.add("研发部");
employee.add("张三丰");
employee.add("杨过");
employee.add("黄老邪");
position.add("工程师");
position.add("经理");
position.add("董事长");
String flag = request.getParameter("flag");
int a = Integer.parseInt(flag);
if(a == 1) {
String json = JSON.toJSONString(employee);
response.getWriter().println(json);
}else if(a == 2) {
String json = JSON.toJSONString(department);
response.getWriter().println(json);
}else {
String json = JSON.toJSONString(position);
response.getWriter().println(json);
}
}
}
正在回答
同学写的代码的可以的,这里老师把html页面做了一下修改,:
我们用一个function来实现,根据传入的参数不同,显示不同的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><style type="text/css">#all{ width: 600px; height: 50px; background-color: #D0D0D0; margin: auto;} .b{ width: 196px; height: 50px; margin: auto; background-color: #F0F0F0; font-size: 15px;} #list{ width: 600px; height: 600px; text-align: center; font-size: 20px; background-color: #F0F0F0; margin: auto;}</style></head> <body> <div id="all"> <button class="b" id="emp" onclick="check(1)">员工列表</button> <button class="b" id="depart" onclick="check(2)">部门列表</button> <button class="b" id="postion" onclick="check(3)">职位列表</button> </div> <div id="list"></div> <script type="text/javascript"> //创建XMLHttpRequest对象 var xmlhttp ; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } //实现员工列表 function check(falg){ //发送Ajax请求 xmlhttp.open("GET","/Ajax/Dpart?flag="+falg,true); xmlhttp.send(); //处理服务器响应 xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ //获取响应文本 var text = xmlhttp.responseText; var json = JSON.parse(text); console.log(json); var html = ""; for(var i = 0; i < json.length; i ++){ var emp = json[i]; html = html + "<h3>" + emp + "</h3>"; document.getElementById("list").innerHTML = html; } } } } </script></body> </html> |
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
总感觉页面上的代码重复较多,是否可以优化
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧