Mode one: Asynchronous implementation with Ajax, and the example displayed on the page:
================start===================
--JSP Page---
<%@ 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" >
<base href= "<%=basePath%>" >
<script type= "Text/javascript" >
function Create () {
if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}else if (window. ActiveXObject) {
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP")
}
}
function run (URL) {
Create ();
Xmlhttp.open ("POST", url,true);
Xmlhttp.onreadystatechange=callback;
Xmlhttp.send ();
}
function Ajaxjson () {
Run ("ajj0001bg.do?operation=ajaxforjson&&msg=" +new Date ());
}
function callback () {
if (xmlhttp.readystate==4) {
if (xmlhttp.status==200) {
var Xmldoc=xmlhttp.responsetext;
var data=eval (xmldoc);//parsing JSON objects with the Eval function
var Jsondiv=document.getelementbyid ("Jsondata");
var jsonhtml= "<table border=1 ><tr><td> number </td><td> name </td><td> sex </td ></tr> ";
for (Var i=0;i<data.length;i++) {
jsonhtml=jsonhtml+ "<tr><td>" +data[i].userid+ "</td><td>" +data[i].username+ "</td> <td> "+data[i].usersex+" </td></tr>;
}
jsonhtml=jsonhtml+ "</table>";
jsondiv.innerhtml=jsonhtml;
}else{
Alert ("The AJAX server returned an error.") ");
}
}
}
</script>
<body>
This is Testjoson JSP page. <br>
<input type= "button" value= "Testjson" onclick= "Ajaxjson ()"/><br/>
<font color= "Red" >json data are as follows:</font>
<div id= "Jsondata" ></div>
</body>
----------The background action code----------------
Import net.sf.json.jsonarray;//requires importing this JSON package
public class Ajj0001bgaction extends topaction{
Public Actionforward Testjson (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
Return Mapping.findforward ("Testjson");
}
/**
* Test JSON
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
Public Actionforward Ajaxforjson (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
Manufacturing Fake data
List<usermodel> list=new arraylist<usermodel> ();
Usermodel user1=new Usermodel ()//User Object 1
User1.setuserid (1);
User1.setusername ("haha");
User1.setusersex ("male");
List.add (user1);
Usermodel user2=new Usermodel ()//User Object 2
User2.setuserid (2);
User2.setusername ("hehe");
User2.setusersex ("female");
List.add (User2);
Converting a list to JSON
Jsonarray json=jsonarray.fromobject (list);
SYSTEM.OUT.PRINTLN (JSON);//Output see if it's JSON format
Set encoding
Response.setcharacterencoding ("Utf-8");
Write to the foreground
Response.getwriter (). Write (json.tostring ());
return null;
}
}
----All of the jar packages you need are as follows-----
Json_lib.rar
===============end====================