Ajax exercises return data in JSON format

Source: Internet
Author: User
JSON (JavaScript Object Notation) is a simple data format, which is lighter than XML. JSON is a javascript native format, which means that no special API or toolkit is required to process JSON data in JavaScript. Exercise example ::
First, create the object class user. Java:
public class User {Integer id;String name;String sex;public User() {super();// TODO Auto-generated constructor stub}public User(Integer id, String name, String sex) {super();this.id = id;this.name = name;this.sex = sex;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}

JSON. jsp

<Body> <Table border = "1px" align = "center"> <thead> <tr> <TH> id </Th> <TH> name </Th> <TH>> gender </Th> </tr> </thead> <tbody id = "users"> </tbody> </table> </body>

<SCRIPT type = "text/JavaScript"> <! -- Window. onload = function () {// obtain the xhr object var XMLHTTP = createxmlhttprequest (); XMLHTTP. onreadystatechange = function () {// determine whether the server returns the correct information if (XMLHTTP. readystate = 4) {// accept data var jsonstr = XMLHTTP. responsetext; var jsonemps = eval ("(" + jsonstr + ")"); display (jsonemps) ;}/// specify the request type XMLHTTP. open ("get ",". /listservlet? Timestamp = "+ new date (). gettime (), true); // sends the request XMLHTTP. send (null);} function display (OBJ) {var users = document. getelementbyid ("users"); For (VAR I = 0; I <obj. length; I ++) {// create tr var TR = document. createelement ("TR"); // create TD var TD1 = document. createelement ("TD"); var td2 = document. createelement ("TD"); var td3 = document. createelement ("TD"); // Add text td1.appendchild (document. createtextnode (OBJ [I]. ID); td2.appendchild (document. createtextnode (OBJ [I]. name); td3.appendchild (document. createtextnode (OBJ [I]. sex); // Add to tr. appendchild (TD1); tr. appendchild (td2); tr. appendchild (td3); // Add users to users. appendchild (TR) ;}} function createxmlhttprequest () {var XMLHTTP; try {// Firefox, opera 8.0 +, safarixmlhttp = new XMLHttpRequest ();} catch (E) {try {// Internet assumerxmlhttp = new activexobject ("msxml2.xmlhttp");} catch (e) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");} catch (e) {}} return XMLHTTP;} // --> </SCRIPT>

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");List<User> list=new ArrayList<User>();for(int i=0;i<5;i++){User user=new User(i,"ren"+i,"nv");list.add(user);}String json=JSONArray.fromObject(list).toString();response.setContentType("text/html;charset=UTF-8");PrintWriter out=response.getWriter();out.print(json);System.out.println("as");}

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.