客户端:
用ajax提交数据
<!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=gb2312">
<title>JSON demo</title>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
var xmlHttp;
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
function User(id, name, email, country, city) {
this.id = id;
this.name = name;
this.email = email;
this.country = country;
this.city = city;
}
function get(id) {
return document.getElementById(id);
}
function getUser() {
var id = get("id").value;
var name = get("name").value;
var email = get("email").value;
var country = get("country").value;
var city = get("city").value;
return new User(id, name, email, country, city);
}
function sendToServer() {
var user = getUser();
//use the JSON javascript library to stringify to the User object
var jsonUser = JSON.stringify(user);
alert(jsonUser);
var url = "json.do?ts=" + new Date().getTime();
xmlHttp = createXMLHttpRequest();
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = sendToServerCallback;
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
xmlHttp.send("jsonUser=" + jsonUser);
}
function sendToServerCallback() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var respDiv = get("resposeDiv");
alert(xmlHttp.responseText);
respDiv.innerHTML = xmlHttp.responseText;
}
}
</script>
</head>
<body>
Id:<input type="text" id="id"><br>
Name:<input type="text" id="name"><br>
Email:<input type="text" id="email"><br>
Country:<input type="text" id="country"><br>
City:<input type="text" id="city"><br>
<br>
Response message from server:
<div id="resposeDiv">
</div>
<br>
<input type="button" value="SendToServer" onclick="sendToServer()">
</body>
</html>
服务器:
是个servlet接收ajax的请求
package com.handson.json.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.handson.json.User;
import net.sf.json.JSONObject;
public class JSONServlet extends HttpServlet {
public JSONServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strJSON = request.getParameter("jsonUser");
System.out.println("strJSON: " + strJSON);
// use the JSON-Java binding library to create a JSON object in Java
try {
JSONObject jsonObj = JSONObject.fromObject(strJSON);
String respText = "<font color=\"red\">You post a user object[id:"
+ jsonObj.getInt("id") + ", name:"
+ jsonObj.getString("name") + ", email:"
+ jsonObj.getString("email") + ", country:"
+ jsonObj.getString("country") + ", city:"
+ jsonObj.getString("city") + "] to server.</font>";
PrintWriter out = response.getWriter();
out.println(respText);
out.flush();
// convert to java bean
User user = (User) JSONObject.toBean(jsonObj, User.class);
System.out.println("user.id" + user.getId() + ", user.name:"
+ user.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}