练习打卡
来源:2-8 自由编程
星岳神话
2021-08-26 16:29:35
相关代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width: 620px;">
<form action="ajax3_ziyoubiancheng/employee_list" method="GET">
<input style="width:200px;" type="button" name="emp" id="emp" value="员工列表">
<input style="width:200px;" type="button" name="post" id="post" value="职位列表">
<input style="width:200px;" type="button" name="depart" id="depart" value="部门列表">
</form>
</div>
<div style="width:600px; text-align:center;">
<div id="container"></div>
</div>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript">
$("#emp").click(function(){
initAjax("GET","/ajax3_ziyoubiancheng/employee_list?flag=emp", true);
});
$("#post").click(function(){
initAjax("GET","/ajax3_ziyoubiancheng/employee_list?flag=post", true);
});
$("#depart").click(function(){
initAjax("GET","/ajax3_ziyoubiancheng/employee_list?flag=depart", true);
});
function initAjax(obj, url, flag){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open(obj,url,flag);
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++){
html=html+"<div>"+json[i]+"</div>";
}
$("#container").html(html);
}
}
}
</script>
</body>
</html>
相关代码:
package com.imooc.ziyoubiancheng;
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_list")
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<String> list=new ArrayList<String>();
String req=request.getParameter("flag");
if(req.equals("emp")) {
list.add("小红");
list.add("小明");
list.add("小白");
}else if(req.equals("post")) {
list.add("职员");
list.add("经理");
}else if(req.equals("depart")) {
list.add("人事部");
list.add("技术部");
list.add("无限事业部");
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println(JSON.toJSONString(list));
}
}
1回答
好帮手慕小小
2021-08-26
同学你好,已完成练习,代码实现符合题目要求,很好,继续加油!