This article describes the simple Ajax invocation of the Jquery+json implementation. Share to everyone for your reference, specific as follows:
Userservlet.java Code:
Package com.iss.servlet;
Import org.json.JSONException;
Import Org.json.JSONObject;
Import Com.iss.pojo.User;
Import Com.iss.util.JSONUtil; public class Userservlet extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response
) throws Servletexception, IOException {doPost (request, response);
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
List Add object list<user> userlist = new arraylist<user> ();
User User1 = new User ("John", "Male", "18");
User User2 = new User ("Dick", "Male", "19");
User User3 = New User ("Harry", "Male", "20");
Userlist.add (user1);
Userlist.add (User2);
Userlist.add (USER3);
PrintWriter out = Response.getwriter ();
String str = NULL; try {//account password if matched, return the list to the interface if (Request.getparameter ("UserName"). Equals ("jquery") && Request.getparame ter ("password"). Equals ("Ajax"){str = jsonobject.quote (jsonutil.tojsonstring (userlist));
} out.print (str);
catch (Jsonexception e) {//TODO auto-generated catch block E.printstacktrace ();
} System.out.println ("str" +STR);
Out.flush ();
Out.close ();
}
}
HTML code:
<body> account jquery password ajax <form id= "mainform" > <ul> <li> Account <input type= "text" n Ame= "UserName"/> </li> <li> password <input type= "password" name= "password"/> </li>
; <li> <input onclick= "login ()" type= "button" value= "Login"/> </li> </ul> </form> query to Data <div id= "diva" > </div> <script type= "Text/javascript" > Function login () {//Get form's parameter var
Args =$ ("#mainform"). Serialize (); Call jquery's JSON fetch method///Three parameters are: request path, request parameter, callback function of return data $.getjson ("Servlet/userservlet", args,function (data) {if (da
Ta!=null) {//interface returns a JSON format string called Json.parse () converts the data to JSON//Format Object var jsondata =json.parse (data);
Parsedata (Jsondata);
}else{alert ("Incorrect account password input");
}})} function Parsedata (data) {var str= ""; Traversal JSON format data for (var key in data) {strstr =str+ "user" +data[key].username+ "age" +data[key].age+ "<br/>" alert(str);
///Add data to div $ ("#diva"). html (str);
} </script> </body>
Userservlet Remember to import the tool class Jsonstringobject Jsonutil
JSP to import Jquery.js and json.js
I hope this article will help you with the jquery program design.