JSON Application Example 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.