Jquery Ajax Access servlet processing Json data __jquery

Source: Internet
Author: User

Original: http://blog.csdn.net/www314599782/article/details/7648724


Foreground JSP page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
	; String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/"
; %> <! DOCTYPE html>  


A servlet in the background is responsible for fetching data from MySQL and processing it into Jsonarray


Package com.action;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Net.sf.json.JSONArray;

public class Jsonservlet extends HttpServlet {public

	void doget (HttpServletRequest request, HttpServletResponse Response)
			throws Servletexception, IOException {Toto Toto
 
		= new Toto ();
		Toto.selectallusers ();
		
		Jsonarray json = Jsonarray.fromobject (Toto.getusers ());

	 Response.setcharacterencoding ("Utf-8");
		PrintWriter pw = Response.getwriter ();
		
		Pw.print (JSON);

	public void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {

		doget (request, response);
	}

}


The effect after the run:

Additional: For jquery to get the data asynchronously can also be written as follows: (recommended in this way, personally feel the comparison standard)

$.getjson ("Servlet/jsonservlet", function (htmlobj) {
	 $ (". SJ"). empty ();  
     var html = ' <table> ';
     
     $.each (Htmlobj,function (entryindex,entry) {  
    	 html = ' <tr><td> ' +entry[' id ']+ ' </td></tr > ';
     });
     
     HTML + = ' </table> ';                   
     $ (". SJ"). Append (HTML);}



$.ajax ({
			URL: ' servlet/jsonservlet ',
			type: ' Post ',
			success:function (msg) {
				var htmlobj = Jquery.parsejson (msg);

				var html = ' <table> ';
				$.each (Htmlobj,function (entryindex,entry) {  
			    	 html = ' <tr><td> ' +entry[' id ']+ ' </td></tr > ';
			    });
				HTML + = ' </table> ';   
				                
			     $ (". SJ"). HTML (HTML);  
			}


		};



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.