JSON (in JSP) application instance(2011-04-14 13:58:02)
reproduced
Tags: json Ajax JSP |
Category: Java EE |
Jsp:
<script type= "Text/javascript" src= "json.js" ></script><script type= "Text/javascript" >var http_request = false;function send_request (URL) { http_ Request = false; if (window. XMLHttpRequest) {http_request = new XMLHttpRequest (); if (http_request.overridemimetype) {http_request.overridemimetype (' text/xml '); } }else if (window. ActiveXObject) { try { http_request = new ActiveXObject ("Msxml2.xmlhttp"); } catch (e) { try { http_request = new ActiveXObject ("Microsoft.XMLHTTP"); } catch (e) {}} } if (!http_request) {& Nbsp; window.alert ("Cannot create XMLHttpRequest object instance."); return false;} http_request.onreadystatechange = Processrequest; http_request.open ("GET", url, True); http_request. Send (null);} function ProcessRequest ()//return result handler {if (http_request.readystate = = 4) { & nbsp; if (http_request.status = =) { var f=json.parse (http_request.responsetext);data in JSON format is stored in the//f. JSON can be accessed via F.attributein the content of the} else {alert ("Your network has an exception"); }}} function GetFunction () {send_request ('/***.do? '); }import org.json.JSONException;import Org.json.JSONObject;public class Getfunctionaction extends Action {public Actionforward execute (actionmapping mapping, Actionform Form,https Ervletrequest request, HttpServletResponse response) {jsonobject jsonobj=new jsonobject ();try {jsonobj.put ("name", "* *");//...} catch (Jsonexception e) {e.printstacktrace ();}Response.setcontenttype ("text/html");response.setcharacterencoding ("gb2312");try {PrintWriter out=response.getwriter ();out.println (jsonobj.tostring ());Out.flush ();out.close ();} catch (IOException e) {e.printstacktrace ();} return null;}}
JSON Application Example 2