老师,我的一直在控制台报错

来源:2-8 自由编程

唏哩哗啦v

2019-01-26 14:29:08

报错信息:Uncaught SyntaxError: Unexpected end of input     employeelist.jsp:1

jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8" %>

 <

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>职员列表</title>

<style type="text/css">

.container{

width:1200px;

margin:0 auto;

}

.list{

width:1000px;

margin:0 auto;

overflow: hidden;

}

#empname {

width:300px;

height:30px;

float:left;

margin-left:30px;

}

#empjob{

width:300px;

height:30px;

float:left;

margin-left:20px;

}

#empdname{

width:300px;

height:30px;

float:left;

margin-left:20px;

}

button{

width:100%;

height:30px;

font-size:20px;

}

</style>

</head>

<body>

<div class="container">

<div class="list">

<div id="empname"><button>员工列表</button></div>

<div id="empjob"><button>职位列表</button></div>

<div id="empdname"><button>部门列表</button></div>

</div>

</div>

<script type="text/javascript">

//创建XMLHttpRequest对象

var xmlhttp;

if(window.XMLHttpRequest){

xmlhttp=new XMLHttpRequest();

}

console.log(xmlhttp);

//发送ajax请求

xmlhttp.open("GET","/ajax/employee",true);

xmlhttp.send();

//处理服务器响应

xmlhttp.onreadystatechange=function(){

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

var res=xmlhttp.responseText;

var emps=JSON.parse(res);

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

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

var emp=jsons[i];

out.println("<div>"+emp.ename+"</div>");

}

}

}

}

</script>

</body>

</html>



JavaBean:

package com.imooc.employees;


public class Employees {

private String ename;

private String job;

private String dname;

public Employees(String ename, String job, String dname) {

super();

this.ename = ename;

this.job = job;

this.dname = dname;

}

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public String getJob() {

return job;

}

public void setJob(String job) {

this.job = job;

}

public String getDname() {

return dname;

}

public void setDname(String dname) {

this.dname = dname;

}

}


servlet:

package com.imooc.employees;


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 EmplyeeList

 */

@WebServlet("/employee")

public class EmplyeeList extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public EmplyeeList() {

        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.add(new Employees("小红","职员","人事部"));

list.add(new Employees("小明","经理","研发部"));

list.add(new Employees("小白","职员","市场部"));

String json=JSON.toJSONString(list);//将java对象转换为json对象

System.out.println(json);

response.getWriter();

}


}


写回答

1回答

好帮手慕珊

2019-01-27

你好!可以按如下建议进行修改

1、把servlet的最后一行response.getWriter();改成:

response.getWriter().println(json);

2、将jsp页面中的jsons改成emps,名字要一致

http://img.mukewang.com/climg/5c4d3a380001eeae03360088.jpg

3、js中显示数据的时候,参照老师代码完成,如下所示:注意因为是js的代码,所以不能用out.println()进行输出。而是像下面代码一样,把for循环中的内容连成一个字符串,然后使用最后一条语句设置字符串内容到div中

http://img.mukewang.com/climg/5c4d3a800001e62714760394.jpg

祝学习愉快!

0

0 学习 · 10204 问题

查看课程