Page Notifications Off
µÏ°²Õï¶Ï-ÐÅÏ¢¼¼Êõ²¿(IT)֪ʶ¿â (Di'an Diagnostics IT Knowledge base) > Java > JQuery¡¢JSON¡¢AjaxÔÚServletÖеÄÓ¦ÓÃ
JQuery¡¢JSON¡¢AjaxÔÚServletÖеÄÓ¦ÓÃ
´Ó $1
ÄÚÈݱí¸ñNo headers
×¼±¸Ìõ¼þ£º
1¡¢ÔÚJavaÖÐÕýÈ·µÃµ½JSONObject£¬ÐèÒªµ¼ÈëJSONµÄJAVAÖ§³Ö°ü“json-lib-2.3-jdk15.jar”£¬Í¬Ê±Ðèµ¼ÈëJSONÒÀÀµ°ü“commons-logging-1.0.4.jar”£¬“commons-lang.jar”£¬“commons-collections.jar”£¬“commons-beanutils.jar”£¬“ezmorph-1.0.4.jar”£»
2¡¢ÓÉÓÚÔÚ¿Í»§¶Ë½Å±¾ÖÐÒªÓõ½JQueryÓëJSON£¬ÐèÒýÈë"JQuery.js"Óë"json2.js"¡£
JAVA ´úÂëÈçÏ£º
package example;
import java.io.BufferedReader;
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.JSONObject;
public class JasonServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public JasonServlet() {
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 {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
String userStr = readJSONString(request);//µÃµ½requestContext
JSONObject jsonObj = JSONObject.fromObject(userStr);//ת»»³ÉJSONObject
System.out.println(jsonObj.getInt("userId"));//µÃµ½JSONObjectµÄuserIdÖµ
System.out.println(jsonObj.getString("name"));
JSONObject resultJSON = new JSONObject();//¹¹½¨Ò»¸öJSONObject
resultJSON.accumulate("errNum", 1);
resultJSON.accumulate("errInfo", "³É¹¦");
response.setContentType("application/x-json");//ÐèÒªÉèÖÃContentTypeΪ"application/x-json"
PrintWriter out = response.getWriter();
out.println(resultJSON.toString());//Ïò¿Í»§¶ËÊä³öJSONObject×Ö·û´®
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
public String readJSONString(HttpServletRequest request){
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
json.append(line);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
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">
<html>
<head>
<title>My JSP 'JasonTest.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
</head>
¡¡