1.需要的包有:
1) /Files/ksyou/allJarOfJson.rar
2) json.js
程序如下 :
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP page</title>
<script type="text/javascript" src="ReadXML.js"></script>
<script type="text/javascript" src="json.js"></script>
</head>
<body>
name <input type="text" id="name"><br>
age <input type="text" id="age"><br>
sex <input type="text" id="sex"><br>
<input type="button" value="SendByPost" onclick="sendByPost()"><br><br>
<div id="showDiv"></div>
</body>
</html>
ReadXML.js
1var xmlHttp;
2
3function createXmlHttpRequest(){
4 if(window.XmlHttpRequest){
5 xmlHttp = new XmlHttpRequest();
6 }else if(window.ActiveXObject){
7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
8 }
9}
10
11function sendByPost(){
12 createXmlHttpRequest();
13 //在url上加一个当前时间是为了使每次发送的url唯一,防止浏览器缓存。
14 var personAsJson = JSON.stringify(getPerson());
15 var url = "message?timeStemp="+new Date().getTime();
16 xmlHttp.onreadystatechange=handleStateChange;
17 xmlHttp.open("POST",url,true);
18 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
19 xmlHttp.send(personAsJson);
20}
21
22function handleStateChange(){
23 var showDiv = document.getElementById("show");
24 if(xmlHttp.readyState==4){
25 if(xmlHttp.status==200){
26 var showDiv = document.getElementById("showDiv");
27 if(showDiv.hasChildNodes()){
28 showDiv.removeChild(showDiv.childNodes[0]);
29 }
30 //创建创建textNode
31 var responseText = document.createTextNode(xmlHttp.responseText);
32 //添加到showDiv元素中
33 showDiv.appendChild(responseText);
34 }
35 }
36}
37
38function getPerson(){
39 var name = document.getElementById("name").value;
40 var age = document.getElementById("age").value;
41 var sex = document.getElementById("sex").value;
42 return new person(name,age,sex);
43}
44
45function person(name,age,sex){
46 this.name=name;
47 this.age=age;
48 this.sex=sex;
49}
后台代码:
Message.java
1package com.kai;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.PrintWriter;
6
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import net.sf.json.JSONObject;
13
14
15public class Message extends HttpServlet {
16
17 private static final long serialVersionUID = 7196636596362343837L;
18
19 public void doGet(HttpServletRequest request, HttpServletResponse response)
20 throws ServletException, IOException {
21 response.setContentType("text/html");
22 StringBuffer json =new StringBuffer();
23 BufferedReader reader = request.getReader();
24 String line=null;
25 while((line=reader.readLine())!=null){
26 json.append(line);
27 }
28 String js = json.toString();
29 JSONObject object=JSONObject.fromObject(js);
30
31 PrintWriter out = response.getWriter();
32 String resText = "you name is "+object.getString("name")+"; age: "
33 +object.getString("age")+"; sex: "+object.getString("sex");
34 out.println(resText);
35 out.flush();
36 out.close();
37 }
38
39 public void doPost(HttpServletRequest request, HttpServletResponse response)
40 throws ServletException, IOException {
41 doGet(request,response);
42 }
43
44}