Java Servlet生成JSON格式數(shù)據(jù)并用jQuery顯示的方法
本文實(shí)例講述了Java Servlet生成JSON格式數(shù)據(jù)并用jQuery顯示的方法。分享給大家供大家參考,具體如下:
1、Servlet通過json-lib生成JSON格式的數(shù)據(jù)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
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 json.Person;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@WebServlet("/JSONServlet")
public class JSONServlet extends HttpServlet {
public JSONServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-json");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
ArrayList<Person> items=new ArrayList<Person>();
items.add(new Person(2,"jack"));
items.add(new Person(2,"bob"));
items.add(new Person(2,"alex"));
JSONArray jsonArray=new JSONArray();
jsonArray.addAll(items);
out.print(jsonArray.toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
// Put your code here
}
}
2、前端頁(yè)面代碼
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$("#kick").click(
function() {
$.ajax({
type : "post",//post方法
url : "JSONServlet",
data : {
"legs" : "2",
"name" : "aa"
},
//ajax成功的回調(diào)函數(shù)
success : function(returnData) {
var arr = eval(returnData);
$.each(arr, function(index, content) {
$("#result").append(
"<div>" + content.legs
+ "</div>" + "<div>"
+ content.name
+ "</div><hr/>");
});
}
});
});
});
</script>
</head>
<body>
<input type="button" id="kick" value="kick">
<div id="result"></div>
</body>
</html>
jQuery也可以用.getJSON實(shí)現(xiàn)異步數(shù)據(jù)獲取
<script type="text/javascript">
$(document).ready(
function() {
$("#kick").click(function() {
$.getJSON("JSONServlet",function(returnData){
var arr = eval(returnData);
$("#result").html("");//清空info內(nèi)容
$.each(arr, function(index, content) {
$("#result").append(
"<div>" + content.legs
+ "</div>" + "<div>"
+ content.name
+ "</div><hr/>");
});
});
});
});
</script>
希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
實(shí)現(xiàn)論壇樹型結(jié)構(gòu)的具體算法
實(shí)現(xiàn)論壇樹型結(jié)構(gòu)的具體算法...2006-10-10
jsp頁(yè)面中EL表達(dá)式被當(dāng)成字符串處理不顯示值問題的解決方法
下面小編就為大家?guī)硪黄猨sp頁(yè)面中EL表達(dá)式被當(dāng)成字符串處理不顯示值問題的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
運(yùn)用JSP+ajax實(shí)現(xiàn)分類查詢功能的實(shí)例代碼
本篇文章主要介紹了運(yùn)用JSP+ajax實(shí)現(xiàn)分類查詢功能的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
實(shí)戰(zhàn) J2EE 開發(fā)購(gòu)物網(wǎng)站 - 創(chuàng)建數(shù)據(jù)庫(kù)
實(shí)戰(zhàn) J2EE 開發(fā)購(gòu)物網(wǎng)站 - 創(chuàng)建數(shù)據(jù)庫(kù)...2006-10-10
JSP學(xué)習(xí)之Java Web中的安全控制實(shí)例詳解
這篇文章主要介紹了JSP學(xué)習(xí)之Java Web中的安全控制,較為詳細(xì)的分析了JSP安全控制的常見技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09

