Json必需的包
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.2.3-jdk13.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar
出现Java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。
出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。
servlet返回json到前台:
package cn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 cn.dao.ITelnumberDao;
import cn.dao.TelnumberDao;
import cn.po.Telnumber;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class telServlet
*/
@WebServlet("/telServlet")
public class telServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public telServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
StringBuffer getRequestURL = new StringBuffer(
request.getRequestURL().toString().replace("telServlet", "tel.jsp"));
String usercode = "";
String tel = "";
try {
String telphone = request.getParameter("telphone");// 获取前台url
if (telphone.indexOf("?") != -1) {
String str = telphone.substring(1);// 截取?后的字符串
String[] strs = str.split("&");
usercode = strs[0];// 获取usercode字符串,主叫
tel = strs[1];// 获取tel字符串,被叫
int index = usercode.indexOf("=");
String str2 = usercode.substring(index + 1, index + 4);// 截取usercode字符串值前三位
int index_tel = tel.indexOf("=");
String str_tel = tel.substring(index_tel + 1, index_tel + 6);// 截取tel字符串值6位
ITelnumberDao telnumberDao = new TelnumberDao();
Telnumber telnumber = new Telnumber();
telnumber.setTel_number(str2);//设置主叫号段
try {
JSONObject object =new JSONObject();//定义一个json对象
Telnumber telnumber2 = telnumberDao.getTelByNumber(telnumber);//进入数据访问层查询数据库
if (telnumber2!=null) {
if (str2.startsWith(telnumber2.getTel_number())) {//输入号段与数据库数据比对
object.put("usercode", telnumber2.getTel_iden());
}
} else {
//out.print("<script type='javascript/text'>alert('数据库中无该号段信息');</script>");
object.put("usercode", "号段错误!");
}
telnumber.setTel_number(str_tel);//设置被叫号段
Telnumber telnumber3 = telnumberDao.getTelByNumber(telnumber);//进入数据访问层查询数据库
if (str_tel.equals(telnumber3.getTel_number())) {
object.put("tel", telnumber3.getTel_iden());
} else {
}
if (getRequestURL != null) {
StringBuffer url = new StringBuffer(getRequestURL + telphone);
object.put("url", url.toString());
}
//object:{"usercode":"重要用户","tel":"电信","url":"http://localhost:8080/cheng/tel.jsp?usercode=18833333&tel=10000"}
JSONArray array=new JSONArray();
array.add(object);//将json对象转换成json数组
//array:[{"usercode":"重要用户","tel":"电信","url":"http://localhost:8080/cheng/tel.jsp?usercode=18833333&tel=10000"}]
String jsonstr=array.toString();
out.print(jsonstr);//返回前台ajax
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
前台js解析json:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<title>小沃</title>
</head>
<body>
<input id="usercode" type="text" name="usercode" value="" />
<input id="tel" type="text" name="tel" value="" />
<iframe id="url" width="100%" height="500"></iframe>
<script type='text/javascript'>
$(GetRequest);
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
$.get("telServlet", {telphone : url}, function(data) {
var obj = eval('(' + data + ')');//转换为json对象
$('#usercode').val(obj[0].usercode);
$('#tel').val(obj[0].tel);
$('#url').attr("src", obj[0].url);
});
}
</script>
</body>
</html>