java ajax json 前后端数据传输

传输单个对象

example.java

package example;
import servlet.*;
import java.io.IOException;
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 net.sf.json.JSON;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class example
 */
@WebServlet("/example")
public class example extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public example() {
        super();
    }


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


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		people p=new people("张三", "123");
		
		JSONObject obj=new JSONObject();
		obj.put("message",p.getMessage());
		obj.put("time", p.getTime());
		
		JSONObject firstp=new JSONObject();
		firstp.put("obj",obj);
		
		response.getWriter().append(firstp.toString());
	}

}

index.jsp

<%@ 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">
<script src="js/jquery.min.js"></script>
	    <script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
	    <script src="js/my.js" type="text/javascript" charset="utf-8"></script>
	    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
	    <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
	    <link rel="stylesheet" type="text/css" href="css/my.css"/>
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$.ajax({
        url:"example/example",
        data:{},
        type:"POST",
        dataType:"JSON",
        success:function(httpdata){
        	$("#a").append("<li>"+httpdata.obj.message+"</li>");
            $("#a").append("<li>"+httpdata.obj.time+"</li>");
        }
    })
});
</script>
</head>
<body>
<ul id="a"></ul>

</body>
</html>

web.xml插入如下代码

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>example.example</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/example/example</url-pattern>
  </servlet-mapping>

传输多个对象


example.java

package example;
import servlet.*;
import java.io.IOException;
import java.util.ArrayList;

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

/**
 * Servlet implementation class example
 */
@WebServlet("/example")
public class example extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public example() {
        super();
    }


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


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		people p1=new people("zhangsan", "123");
		people p2=new people("lisi", "123324345");
		people p3=new people("wangwu", "332123");
		ArrayList<people> list=new ArrayList<people>();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		JSONArray arr=new JSONArray();
		for(people p: list)
		{
			JSONObject obj=new JSONObject();
			obj.put("message",p.getMessage());
			obj.put("time",p.getTime());
			arr.add(obj);
		}
		response.getWriter().append(arr.toString());
	}

}


index.jsp

<%@ 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">
<script src="js/jquery.min.js"></script>
	    <script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
	    <script src="js/my.js" type="text/javascript" charset="utf-8"></script>
	    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
	    <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
	    <link rel="stylesheet" type="text/css" href="css/my.css"/>
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$.ajax({
        url:"example/example",
        data:{},
        type:"POST",
        dataType:"JSON",
        success:function(httpdata){
        	 for(var i=0;i<httpdata.length;i++){
                 var n=httpdata[i].message
                 var a=httpdata[i].time
                 var tr="<tr>"
                     tr+="<td>"+n+"</td>"
                     tr+="<td>"+a+"</td>"
                     tr+="</tr>"
                     $("#a").append(tr)
             }    
        }
    })
});
</script>
</head>
<body>
<table width="100%" id="a" border="1px">
</table>

</body>
</html>






  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不染心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值