老师这什么情况啊?我都搞不清我错在哪里了?复制您的代码过来都在浏览器上运行不了
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 Dpart
*/
@WebServlet("/Dpart")
public class Dpart extends HttpServlet {
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(employee);
response.getWriter().println(json);
}else{
String json=JSON.toJSONString(position);
response.getWriter().println(json);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
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 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>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星