Java servlet generates JSON-formatted data and _jsp programming using jquery display

Source: Internet
Author: User

The example in this article describes how the Java servlet generates JSON-formatted data and displays it in jquery. Share to everyone for your reference, specific as follows:

1. servlet generates JSON-formatted data via Json-lib

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 D Estroy () {Super.destroy ()//Just puts "destroy" string in Log//Put your code here} public void Doget (httpservletrequ EST 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 {DoG
ET (request,response);

 public void Init () throws Servletexception {//Put your code here}}

2, front 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" >  

jquery can also use. Getjson to implement asynchronous data acquisition

<script type= "Text/javascript" >
$ (document). Ready (
function () {
$ ("#kick"). Click (function () {
$.getjson ("Jsonservlet", function (returndata) {
var arr = eval (returndata);
$ ("#result"). HTML ("");//empty 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.

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.