json 用法

json.jsp

<%@ 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">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'JSON.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  <script type="text/javascript" src="json.js"></script>
  <script type="text/javascript">
  var xmlHttp;
    function createXMLHttpRequest()
    {
        if(window.ActiveXObject)
         {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if(window.XMLHttpRequest)
        {
            xmlHttp = new XMLHttpRequest();
        }
}

function Car(make,model,year,color){
  
   this.make=make;
   this.model=model;
   this.year=year;
   this.color=color;

}

function doJSON(){

   var car=new Car();
   car.make="mm";
   car.model="ee";
   car.year=1984;
   car.color="red";
  
  
   var carAsJSON=car.toJSONString();
  
   var a=carAsJSON.parseJSON();
     
   window.alert(carAsJSON);
   var url="TestJSON?time="+new Date().getTime();
   createXMLHttpRequest();
   xmlHttp.open("post", url, true);
   xmlHttp.onreadystatechange = callback;//指定响应函数
   xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlHttp.send(carAsJSON);
}

function callback(){
    if (xmlHttp.readyState == 4) { // 判断对象状态
       if (xmlHttp.status == 200) { // 信息已经成功返回,开始处理信息
           
              var data=xmlHttp.responseText;
              var e=document.getElementById("d");
              var a=document.createTextNode(data);
              e.appendChild(a);
      }
   
    }
}
 
 
  </script>
  </head>
 
  <body>
    <input type="button" onclick="doJSON();" value="aaa"/>
    <div id="d"></div>
  </body>
</html>

json.java

package test;

import java.io.BufferedReader;
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 org.json.*;
public class TestJSON extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String json=readJSON(request);

JSONObject jsonObject=null;

try {
jsonObject=new JSONObject(json);


} catch (JSONException e) {
e.printStackTrace();
}

try {
String r="1:"+jsonObject.getInt("year")+" 2:"+jsonObject.getString("make")+" 3:"
+jsonObject.getString("model")+" 4:"+jsonObject.getString("color");
    out.println(r);
    out.flush();
    out.close();
} catch (JSONException e) {
e.printStackTrace();
}


}

private String  readJSON(HttpServletRequest request){

StringBuffer json=new StringBuffer();
String s=null;
try{

BufferedReader reader=request.getReader();

while((s=reader.readLine())!=null){
System.out.println(s);
json.append(s);

}

}
catch(Exception e){
e.printStackTrace();
}

return json.toString();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
      this.doGet(request, response);
}

}

最后还要导入json.js解析器在json.jsp中导入

你可能感兴趣的:(java,json,jsp,servlet,Microsoft)