java中jsonp JsonPTest.java
package cn.com.jsonp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JsonPTest extends HttpServlet {
private static final long serialVersionUID = -5143030889814730794L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.getRequestDispatcher("WEB-INF/view/jsonp.jsp").forward(request, response);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
}
java处理json数据并转发ServerTest.java
/**
* Project Name:jsonpTest
* File Name:ServerTest.java
* Package Name:cn.com.jsonp
* Date:2015年2月2日下午8:21:11
* Copyright (c) 2015, chenzhou1025@126.com All Rights Reserved.
*
*/
package cn.com.jsonp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
* ClassName:ServerTest <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* @version
* @since JDK 1.7
* @see
*/
public class ServerTest extends HttpServlet {
/**
* serialVersionUID:TODO(用一句话描述这个变量表示什么).
* @since JDK 1.7
*/
private static final long serialVersionUID = 1176528307423083925L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("jtusersessionid"));
String responseText = "[{ \"firstName\": \"Brett\", \"lastName\":\"McLaughlin\", \"email\": \"aaaa\" },{ \"firstName\": \"Jason\", \"lastName\":\"Hunter\", \"email\": \"bbbb\"},{ \"firstName\": \"Elliotte\", \"lastName\":\"Harold\", \"email\": \"cccc\" }]";
String callBack = request.getParameter("callback");
JSONObject jsonObj = new JSONObject();
jsonObj.put("data", responseText);
if (callBack != null) {
StringBuffer sb = new StringBuffer(callBack);
sb.append("(");
sb.append(jsonObj.toString());
sb.append(")");
response.getWriter().print(sb.toString());
} else {
response.getWriter().print(jsonObj.toString());
}
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
}
json接收文件 jsonp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="/js/jquery-1.8.2.js"></script>
<title>测试jsonP</title>
<script type="text/javascript">
function pointActivity() {
path = '/serverTest.do';
var jtusersessionid = '${cookie.JSESSIONID.value}';
if (jtusersessionid != null && jtusersessionid != ""
&& jtusersessionid != 'null') {
path = path + "?jtusersessionid=" + jtusersessionid;
}
$.ajax({
type : 'GET',
cache : false,
dispose : true,
reload : false,
url : path,
dataType : "jsonp",
success : function(json) {
var json1 = eval(json.data);
var jsonString = "";
for (var i = 0; i < json1.length; i++) {
jsonString += "姓名 :" + json1[i].firstName + "邮箱 :" + json1[i].email;
if(i!=json1.length-1){
jsonString += ",";
}
}
$("#returnJsonString").html(jsonString);
},
error : function() {
alert('fail');
}
});
}
function success_jsonpCallback(data) {
alert(data.data);
}
</script>
</head>
<body>
<h3>测试jsonP</h3>
<input type="button" value="提交" οnclick="pointActivity();" />
<div>jsonp返回的json数据:</div><div id="returnJsonString"></div>
</body>
</html>
前段jsp index.jsp
<html>
<body>
<%response.sendRedirect("/jsonP.do");%>
</body>
</html>