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)文章
jsp中實(shí)現(xiàn)上傳圖片即時(shí)顯示效果功能
上傳圖片時(shí)即時(shí)顯示圖片效果,這在項(xiàng)目開發(fā)中時(shí)很常見的一項(xiàng)功能,接下來(lái)介紹此功能的實(shí)現(xiàn)過程,有需要的朋友可以參考下2013-01-01
js實(shí)現(xiàn)百度地圖同時(shí)顯示多個(gè)路書效果
百度地圖在顯示應(yīng)用中已經(jīng)很廣泛,本文主要是介紹了js實(shí)現(xiàn)百度地圖同時(shí)顯示多個(gè)路書效果,具有參考價(jià)值,有需要的可以了解一下。2016-10-10
jsp簡(jiǎn)單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
這篇文章主要介紹了jsp簡(jiǎn)單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符,需要的朋友可以參考下2014-03-03
詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交
這篇文章主要介紹了詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交的相關(guān)資料,需要的朋友可以參考下2017-06-06
jsp 使用request為頁(yè)面添加靜態(tài)數(shù)據(jù)的實(shí)例
下面小編就為大家分享一篇jsp 使用request為頁(yè)面添加靜態(tài)數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-12-12
JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁(yè)面
這篇文章主要介紹了JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁(yè)面,實(shí)現(xiàn)的原理,是檢測(cè)瀏覽器的 USER-AGENT 這個(gè)header,然后根據(jù)正則表達(dá)式來(lái)確定客戶端類型,需要的朋友可以參考下2014-09-09

