Json
博客专区 > Favour 的博客 > 博客详情
Json
Favour 发表于1年前
Json
  • 发表于 1年前
  • 阅读 11
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

前台:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function loadInfo(){
		var xmlHttp;
		if(window.XMLHttpRequest){
			xmlHttp=new XMLHttpRequest();
		}else{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				alert(xmlHttp.responseText);
				var dataObj=eval("("+xmlHttp.responseText+")");
				alert(dataObj.name);
				alert(dataObj.age);
				document.getElementById("name").value=dataObj.name;
				document.getElementById("age").value=dataObj.age;
			}
		};
		xmlHttp.open("get", "GetAjaxInfoServlet?action=jsonObject", true);
		
	    xmlHttp.send();
	}
	
	function loadInfo2(){
		var xmlHttp;
		if(window.XMLHttpRequest){
			xmlHttp=new XMLHttpRequest();
		}else{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				alert(xmlHttp.responseText);
				var dataObj=eval("("+xmlHttp.responseText+")");
				var st=document.getElementById("studentTable");
				alert(dataObj.students.length);
				var newTr; // 行
				var newTd0; // 第一列
				var newTd1; // 第二列
				var newTd2; // 第三列
				for(var i=0;i<dataObj.students.length;i++){
					var student=dataObj.students[i];
					newTr=st.insertRow();
					newTd0=newTr.insertCell();
					newTd1=newTr.insertCell();
					newTd2=newTr.insertCell();
					newTd0.innerHTML=student.name;
					newTd1.innerHTML=student.age;
					newTd2.innerHTML="语文:"+student.score.chinese+",数学:"+student.score.math+",英语:"+student.score.english;
				}
			}
		};
		// xmlHttp.open("get", "getAjaxInfo?action=jsonArray", true);
		xmlHttp.open("get", "GetAjaxInfoServlet?action=jsonNested", true);
	    xmlHttp.send();
	}
</script>
</head>
<body>
<div style="text-align: center;">
	<div><input type="button" onclick="loadInfo()" value="Ajax获取信息"/>&nbsp;&nbsp;姓名:<input type="text" id="name" name="name" />&nbsp;&nbsp;年龄:<input type="text" id="age" name="age" /></div>
	<div style="margin-top: 20px;">
		<input type="button" onclick="loadInfo2()" value="Ajax获取信息2"><br/>
		<table id="studentTable" align="center" border="1px;" cellpadding="0px;">
			<tr>
				<th>姓名</th><th>年龄</th><th>得分</th>
			</tr>
		</table>
	</div>
</div>
</body>
</html>

后端:

package web;

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;
import net.sf.json.JSONObject;

public class GetAjaxInfoServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String action=request.getParameter("action");
		if("jsonObject".equals(action)){
			this.getJsonObject(request, response);
		}else if("jsonArray".equals(action)){
			this.getJsonArray(request, response);
		}else if("jsonNested".equals(action)){
			this.getJsonNested(request, response);
		}
		
	}

	
	private void getJsonObject(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		// String resultJson="{\"name\":\"张三\",\"age\":22}";
		JSONObject resultJson=new JSONObject();
		resultJson.put("name", "张三");
		resultJson.put("age", "22");
		out.println(resultJson);
		out.flush();
		out.close();
	}
	
	private void getJsonArray(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		JSONObject resultJson=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject1=new JSONObject();
		jsonObject1.put("name", "张三");
		jsonObject1.put("age", "22");
		JSONObject jsonObject2=new JSONObject();
		jsonObject2.put("name", "李四");
		jsonObject2.put("age", "23");
		JSONObject jsonObject3=new JSONObject();
		jsonObject3.put("name", "王五");
		jsonObject3.put("age", "24");
		jsonArray.add(jsonObject1);
		jsonArray.add(jsonObject2);
		jsonArray.add(jsonObject3);
		
		resultJson.put("students", jsonArray);
		out.println(resultJson);
		out.flush();
		out.close();
	}
	
	private void getJsonNested(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		JSONObject resultJson=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject1=new JSONObject();
		jsonObject1.put("name", "张三");
		jsonObject1.put("age", "22");
		
		JSONObject scoreObject1=new JSONObject();
		scoreObject1.put("chinese", "90");
		scoreObject1.put("math", "100");
		scoreObject1.put("english", "80");
		jsonObject1.put("score", scoreObject1);
		
		JSONObject jsonObject2=new JSONObject();
		jsonObject2.put("name", "李四");
		jsonObject2.put("age", "23");
		
		JSONObject scoreObject2=new JSONObject();
		scoreObject2.put("chinese", "70");
		scoreObject2.put("math", "90");
		scoreObject2.put("english", "90");
		jsonObject2.put("score", scoreObject2);
		
		JSONObject jsonObject3=new JSONObject();
		jsonObject3.put("name", "王五");
		jsonObject3.put("age", "24");
		
		JSONObject scoreObject3=new JSONObject();
		scoreObject3.put("chinese", "80");
		scoreObject3.put("math", "60");
		scoreObject3.put("english", "90");
		jsonObject3.put("score", scoreObject3);
		
		jsonArray.add(jsonObject1);
		jsonArray.add(jsonObject2);
		jsonArray.add(jsonObject3);
		
		resultJson.put("students", jsonArray);
		out.println(resultJson);
		out.flush();
		out.close();
	}
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>GetAjaxInfoServlet</servlet-name>
    <servlet-class>web.GetAjaxInfoServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>GetAjaxInfoServlet</servlet-name>
    <url-pattern>/GetAjaxInfoServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

共有 人打赏支持
粉丝 5
博文 182
码字总数 96186
×
Favour
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: