为什么没有显示

来源:2-8 自由编程

慕勒2048820

2019-01-23 21:38:04

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 MyTest
 */
@WebServlet("/test")
public class MyTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyTest() {
        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> list1 = new ArrayList<String>();
			list1.add("xiaohong");
			List<String> list2 = new ArrayList<String>();
			list2.add("zhigong");
			List<String> list3 = new ArrayList<String>();
			list3.add("jishu");
			String json1 = JSON.toJSONString(list1);
			String json2 = JSON.toJSONString(list2);
			String json3 = JSON.toJSONString(list3);
			response.setContentType("text/html;utf-8");
			if(request.getParameter("id").equals("employee")) {
				System.out.println(json1);
				response.getWriter().println(json1);
			}else if(request.getParameter("id").equals("jober")) {
				System.out.println(json2);
				response.getWriter().println(json2);
			}else if(request.getParameter("id").equals("department")) {
				System.out.println(json3);
				response.getWriter().println(json3);
			}
	}

}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
 table{
 	width:100%;
 	
 }
 td{
 	width:33%;
 }
 button{
 	width:100%;
 	text-algin:center;
 }
</style>
</head>
<body>
		<table>
	 	 <tr>
			<td ><button id="employee">员工列表</button></td>
			<td ><button id="jober">职工列表</button></td>
			<td ><button id="department">部门列表</button></td>
		 </tr>
		</table>
		<div id="content"></div>
</body>
		<script type="text/javascript">
			var xmlhttp,id;
			if(window.XMLHttpRequest){
				xmlhttp = new XMLHttpRequest();
			}else {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			 $("#employee,#jober,#department").onclick(function(){
			var url;
			url = "/ajax/test?id="+this.id;
				xmlhttp.open("get",url,true);
				xmlhttp.send();
			    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 style='text-algin:center;'>" + json[i] + "</div>";
                    }
                    document.getElementById("content").innerHTML = html;
                }
            }
			 })
		</script>
	
</body>
</html>


写回答

1回答

一叶知秋519

2019-01-24

同学的jsp页面中没有jQuery的js文件,引入之后再试下~

如果还有问题,同学可以通过F12看一下浏览器的控制台,根据浏览器控制台的报错进行调整试下~

如果还有问题,可以到问答区提问,我们的老师会为你解决。

祝学习愉快!

0

0 学习 · 10204 问题

查看课程