Java Servlet generates JSON format data and displays it with jQuery,

Source: Internet
Author: User

Java Servlet generates JSON format data and displays it with jQuery,

This example describes how to use jQuery to display data in JSON format generated by Java Servlet. We will share this with you for your reference. The details are as follows:

1. Servlet uses json-lib to generate data in JSON format

import java.io.IOException;import java.io.PrintWriter;import java.util.*;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import json.Person;import net.sf.json.JSONArray;import net.sf.json.JSONObject;@WebServlet("/JSONServlet")public class JSONServlet extends HttpServlet {public JSONServlet() {super();}public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("application/x-json");response.setCharacterEncoding("GBK");PrintWriter out = response.getWriter();ArrayList<Person> items=new ArrayList<Person>();items.add(new Person(2,"jack"));items.add(new Person(2,"bob"));items.add(new Person(2,"alex"));JSONArray jsonArray=new JSONArray();jsonArray.addAll(items);out.print(jsonArray.toString());}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}public void init() throws ServletException {// Put your code here}}

2. Front-end Page code

<% @ Page language = "java" contentType = "text/html; charset = ISO-8859-1" pageEncoding = "ISO-8859-1" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html> 

JQuery can also use. getJSON to obtain asynchronous data.

<Script type = "text/javascript"> $ (document ). ready (function () {$ ("# kick "). click (function () {$. getJSON ("JSONServlet", function (returnData) {var arr = eval (returnData); $ ("# result" ..html (""); // clear info content $. each (arr, function (index, content) {$ ("# result "). append ("<div>" + content. legs + "</div>" + "<div>" + content. name + "</div> 

I hope this article will help you with JSP program design.

Articles you may be interested in:
  • Complete data paging method Example Using jsp + servlet + javabean
  • Detailed description of JSP + Servlet + JavaBean login webpage instance
  • Summary of ServletConfig in JavaEE
  • Java servlet Filter Example
  • Basic configuration of Java Web programming Servlet
  • Analysis of javax. servlet. Servlet and ServletContext Interfaces
  • Java servlet login verification method
  • Java servlet page Jump Methods
  • How to handle garbled code by using Servlet in java
  • Java Servlet and Cookie usage
  • Java Servlet and JSP tutorial

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.