2-8自由编程

2-8自由编程

你好老师,我的按钮显示的是这样的。没有真实结果出现,怎么么回事啊?谢谢


http://img1.sycdn.imooc.com//climg/60752dce09a786b007460182.jpg

package com.wei.ajaxl;

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;
import com.wei.ajax.New;

/**
* Servlet implementation class ShowServlet
*/
@WebServlet("/show")
public class ShowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ShowServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List list=new ArrayList();
List list1=new ArrayList();
List list2=new ArrayList();
List list4=new ArrayList();
list.add(new Employee("小红"));
list.add(new Employee("小明"));
list.add(new Employee("小白"));

list1.add(new Job("职员") );
list1.add(new Job("经理") );

list2.add(new Apartment("人事部"));
list2.add(new Apartment("技术部"));
list2.add(new Apartment("无线事业部"));

list4.add(list);
list4.add(list1);
list4.add(list2);


String json= JSON.toJSONString(list4);
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>

<input type="button" name="employee1" id="employee" value="员工列表">
<input type="button" name="job1" id="job" value="职位列表">
<input type="button" name="depar1" id="depar" value="部门列表">
<br>
<div id="showDiv1">
</div>
<script>
//1.创建xmlhttprequest
document.getElementById("employee").onclick = function() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();

} else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET", "/FirstServlett/show", true);

xmlhttp.send();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var j = xmlhttp.responseText;
console.log(j);
var json = JSON.parse(j);
var n=json[0];
document.getElementById("showDiv1").innerHTML = n;

}

}

}

document.getElementById("job").onclick = function() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();

} else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET", "/FirstServlett/show", true);

xmlhttp.send();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var j = xmlhttp.responseText;

console.log(j);

var json = JSON.parse(j);

var n = json[1];

document.getElementById("showDiv1").innerHTML = n;

}

}

}

//第三个div

document.getElementById("depar").onclick = function() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();

} else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET", "/FirstServlett/show", true);

xmlhttp.send();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var j = xmlhttp.responseText;

console.log(j);

var json = JSON.parse(j);

var n = json[2];

document.getElementById("showDiv1").innerHTML = n;

}

}

}

</script>
</body>
</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小脸 2021-04-13 16:02:07

同学你好,代码存在如下问题:

1、由于同学没有贴出实体类,这里猜测同学实体类代码没有实现序列化,建议将其补全

http://img1.sycdn.imooc.com//climg/60754f7109597f8805560059.jpg

2、在html中需引入js文件,如下:

http://img1.sycdn.imooc.com//climg/60754ecb0901073507000038.jpg

3、无需创建三个实体类对象,这里同学可以直接定义一个对象,里面包含三个属性即可,如:

http://img1.sycdn.imooc.com//climg/60754fa7093049c305370126.jpg

Servlet类中代码简化如下:

http://img1.sycdn.imooc.com//climg/60754fc309b8144c08080306.jpg

4、展示数据这里应使用循环,不然得到的数据是有误的

http://img1.sycdn.imooc.com//climg/60754efe09a8ec7a07280337.jpg

其他同理

参考代码如下:

@WebServlet("/show")
public class ShowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShowServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

List<Employee> 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");
System.out.println(json);
response.getWriter().println(json);

}

}
document.getElementById("employee").onclick = function() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();

} else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}
console.log(xmlhttp);
xmlhttp.open("GET", "/ajax/show", true);

xmlhttp.send();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var j = xmlhttp.responseText;
console.log(j);
var json = JSON.parse(j);
console.log(json);
var html = "";

for (var i = 0; i < json.length; i++) {

html = html +json[i].emp + "<br/>";
}
document.getElementById("showDiv1").innerHTML = html;
}

}

}

​祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师