Jquery+ajax+json+servlet principle and demo__ garbled problem

Source: Internet
Author: User
Tags stub
Jquery+ajax+json+servlet Principle and Demo
Approximate process:
User Time Click, Trigger JS, set $.ajax, start request. The server responds, gets the value that Ajax passes, and then processes it. Returns to Ajax in JSON format. Ajax parses the returned JSON data in the corresponding function in sucess and outputs it to the JSP page.

1. Front desk index.jsp

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

2. Background servlet

* * $filename: JSONAJAXSERVLET.JAVA,V $ * $Date: Sep 1, 2013 $ * Copyright (C) Zhenghaibo, Inc.
 All rights reserved.
 * This software was Made by Zhenghaibo.

* * Package com.njupt.zhb.test;
Import java.io.IOException;
Import Java.io.PrintWriter;

Import Java.net.URLDecoder;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; * * @author: Zhenghaibo *WEB:HTTP://BLOG.CSDN.NET/NUPTBOYZHB *mail:zhb931706659@126.com *sep 1, 2013 Nanji Ng,njupt,china/public class Jsonajaxservlet extends httpservlet{/** * */private static final long serialvers

	Ionuid = 1L; @Override protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex
	ception {//TODO auto-generated Method stub doPost (request, response); } @Override protected void DoPost (HttpServletRequest request, HttpServletResponse RespoNSE) throws Servletexception, IOException {//TODO auto-generated Method Stub request.setcharacterencoding ("Utf-8"
		String userName = Request.getparameter ("UserName");
		Username=urldecoder.decode (UserName, "UTF-8");
		String content = request.getparameter ("content");
		Content=urldecoder.decode (Content, "UTF-8");
		System.out.println ("UserName:" +username);
		System.out.println ("content:" +content);
		Response.setcharacterencoding ("Utf-8");
		PrintWriter out = Response.getwriter ();
		Stitching data into JSON format out.print ("{\" yourname\ ": \" "+ UserName +" \ ", \" yourcontent\ ": \" "+content+" \ "}");
		Out.flush ();
	Out.close ();

3. configuration file Web.xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.5" xmlns= "http://java.sun.com/xml/ns/" 
	Java ee " 
	xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " 
	xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
		<servlet-class> com.njupt.zhb.test.jsonajaxservlet</servlet-class>
		<url-pattern>/jsonajaxaction</ url-pattern>

4. Other
1. Need to import Jquery.min.js
2. Note The garbled solution:
String encoding in 2.1:js, namely: encodeURI (encodeURI (usernameobj))
2.2: Need to decode with Java.net.URLDecoder in the servlet
5. Results Demo :
Enter in Browser: http://localhost:8080/AjaxServletJson/
Enter first and then click the button:

Source code Download: http://download.csdn.net/detail/nuptboyzhb/6193851
reference materials :

1.jquery $.ajax Reference Manual: Http://www.w3school.com.cn/jquery/ajax_ajax.asp

Not allowed for commercial purposes without permission

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.