日期:2014-05-16 浏览次数:20381 次
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="jquery_lib/jquery-1.6.2.min.js"></script> <script type="text/javascript"> function jsonTest(){ $.getJSON("<%=request.getContextPath()%>/servlet/JsonServlet",null,callback); function callback(data){ var resultObj = $("#result"); var showTable="<table>"; for(i=0;i<data.length;i++){ showTable+="<tr><td>"+data[i].deptno+"</td>"; showTable+="<td>"+data[i].dname+"</td>"; showTable+="<td>"+data[i].loc+"</td></tr>"; } showTable+="</table>"; resultObj.html(showTable); } } </script> </head> <body> <input type="button" value="json测试" onclick="jsonTest()"> <div id="result"></div> </body> </html>
package com.czq.entitiy; public class Dept { private String deptno; private String dname; private String loc; public String getDeptno() { return deptno; } public void setDeptno(String deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } }
package com.czq.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import com.czq.dao.DeptDAO; import com.czq.entitiy.Dept; public class JsonServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws Servlet