为啥值没传过来啊?

为啥值没传过来啊?

package com.imooc.learn;


public class Employee {

private String emno;

private String position;

private String department;

public Employee() {

}


public Employee(String emno, String position, String department) {

super();

this.emno = emno;

this.position = position;

this.department = department;

}


public String getEmno() {

return emno;

}


public void setEmno(String emno) {

this.emno = emno;

}


public String getPosition() {

return position;

}


public void setPosition(String position) {

this.position = position;

}


public String getDepartment() {

return department;

}


public void setDepartment(String department) {

this.department = department;

}

}

package com.imooc.learn;


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 Ajax

 */

@WebServlet("/el")

public class Ajax extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Ajax() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<Employee> list=new ArrayList<Employee>();

list.add(new Employee("小红","职员","人事部 "));

list.add(new Employee("小明","职员","技术部 "));

list.add(new Employee("小白","经理","事业部 "));

String json=JSON.toJSONString(list);

response.setContentType("text/html;charset=utf-8");

}


}

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<input type="submit" value="员工列表" id="one">

<input type="submit" value="职位列表" id="two">

<input type="submit" value="部门列表" id="three">

<script type="text/javascript">

var xmlHttp;

if(window.XMLHttpRequest){

xmlHttp=new XMLHttpRequest();

}else{

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

}

xmlHttp.open("GET","/js/el",true);

xmlHttp.send();

    document.getElementById("one").onclick=function (){

    xmlHttp.onreadystatechange=function(){

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

            var text=xmlHttp.responseText;

            console.log(text);

            var jasn=JSON.parse(text);

            var empl="";

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

                var emp=jasn[i];

                empl=emp.emno;

                empl=emp.position;

                empl=emp.department;

            }

            document.getElementById("one").innerHTML=empl;

          }

    }

    }

</script>

</body>

</html>


正在回答

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

2回答

同学你好,很抱歉,上述贴出的截图未贴全,望谅解~

1、在Ajax类中,需添加response.getWriter().println()在servlet中输出html内容,使浏览器解析并显示出来。

http://img1.sycdn.imooc.com//climg/6006bd780987adda08690378.jpg

2、在页面中,如下代码放置位置有误,应放置在最开始位置,修改如下:

http://img1.sycdn.imooc.com//climg/6006bd9809015c3006950100.jpg

修改后完整代码如下所示:

@WebServlet("/el1")

public class Ajax extends HttpServlet {

private static final long serialVersionUID = 1L;

/**
*
* @see HttpServlet#HttpServlet()
*
*/

public Ajax() {

super();

// TODO Auto-generated constructor stub

}

/**
*
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*
*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

List<Employee> list = new ArrayList<Employee>();

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);
}

}
<%@ 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>

<input type="submit" value="员工列表" id="one">

<input type="submit" value="职位列表" id="two">

<input type="submit" value="部门列表" id="three">
<div id="div"></div>

<script type="text/javascript">
document.getElementById("one").onclick = function() {
var xmlHttp;

if (window.XMLHttpRequest) {

xmlHttp = new XMLHttpRequest();

} else {

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

}

xmlHttp.open("GET", "/ajax/el", true);

xmlHttp.send();

xmlHttp.onreadystatechange = function() {

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

var text = xmlHttp.responseText;

console.log(text);

var jasn = JSON.parse(text);

var empl = "";

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

var emp = jasn[i];

/* empl = emp.emno;

empl = emp.position;

empl = emp.department; */
empl = empl + emp.department + "<br/>";

}

document.getElementById("div").innerHTML = empl;

}

}

};
</script>

</body>

</html>

祝学习愉快~

好帮手慕小脸 2021-01-17 15:04:34

同学你好,经测试运行上述贴出代码存在如下问题:

1、能够正确获取但在页面无法正确显示是因为,如下这里应定义一个用来显示div,然后通过

document.getElementById("div").innerHTML= empl;

来获取id为div的对象的内嵌内容

http://img1.sycdn.imooc.com//climg/6003e19f09a0c2d005790184.jpg

2、根据同学for循环中内容的获取,只能得到一个部门,如下:

http://img1.sycdn.imooc.com//climg/6003e1390971d6ae03630111.jpg

优化建议:将其更改为如下内容即可

empl=empl+emp.department+"<br/>";

http://img1.sycdn.imooc.com//climg/6003e17c0968a89706260352.jpg

http://img1.sycdn.imooc.com//climg/6003e168097b9f2303460149.jpg

祝学习愉快~

  • 提问者 慕用4915122 #1

    老师我照着改完了。为啥我列表中不显示信息啊?

    2021-01-19 18:21:34
  • 提问者 慕用4915122 #2

    http://img1.sycdn.imooc.com//climg/6006ba140974b3ec06050138.jpg

    控制台显示这个,啥原因?

    2021-01-19 18:53:49
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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