A simple Ajax invocation instance implemented by Jquery+json _jquery

Source: Internet
Author: User

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&gt
   ; <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.

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.