Ajax+json+java

Source: Internet
Author: User

1. First download Json so rely on the package, json-lib the latest version of Json-lib-2.3-jdk15.jar, its official website is: http://json-lib.sourceforge.net/can directly download, Careful you will find that its web page has hints that it also needs other packages:
Json-lib requires (at least) the following dependencies in your classpath:
Jakarta Commons-lang 2.4
Jakarta commons-beanutils 1.7.0
Jakarta commons-collections 3.2
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6
respectively:
http://commons.apache.org/lang/
http://commons.apache.org/beanutils/
http://commons.apache.org/collections/
http://commons.apache.org/logging/
http://ezmorph.sourceforge.net/

2. First foresight a small web project
User class:

 PackageCom.json; Public classUser {String username;    String password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }

Servlet:

 PackageCom.json;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.ArrayList;Importjava.util.List;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportNet.sf.json.JSONArray; Public classTestjsonextendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request,response); }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); String Str= =request.getparameter ("name");//get the Ajax pass over the paramaterPrintWriter out=Response.getwriter (); List List=NewArrayList ();//Pass ListMap m=NewHashMap ();//Pass MapUser U1=NewUser (); U1.setusername ("Zah"); U1.setpassword ("123"); User U2=NewUser (); U2.setusername ("ZTF"); U2.setpassword ("456"); List.add (U1); //Add User ObjectList.add (U2);//Add User ObjectM.put ("U1", U1); M.put ("U2", U2); Jsonarray JsonArray2=jsonarray.fromobject (list); //Converts a Java array into a JSON objectJsonobject jo=Jsonobject.fromobject (m); //Convert Map ObjectOut.print (jsonArray2);//return to Ajax requestsOut.print (Jo);//return to Ajax requests    }}

Ajax implementations:

<Scripttype= "Text/javascript"src= "Js/jquery.js"></Script><Scripttype= "Text/javascript">     functionTest () {$.ajax ({type:"POST", //Request MethodURL:"Testjson", //Request PathCache:false, Data:"Name=zah",  //Pass the referenceDataType:'JSON',   //return value typeSuccess:function(JSON) {alert (json[0].username+" "json[0].password); //Popup Returns the list object.                   }                }); }</Script>
<Body> <inputtype= "button"name= "B"value= "Test"onclick=test ()></Body>

Output: Zah 123

To access the map, simply change the return function to the following:

Success:function (JSON) {     alert (json.u1.username)}

Ajax+json+java

Related Article

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.