<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
var xmlHttpReq;
//创建一个XmlHttpRequest对象
function createXmlHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();//非IE浏览器
} else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器
}
}
function check(name){
if(name!=""){
alert(name);
//1.创建一个XmlHttpRequest对象
createXmlHttpRequest();
//2.调用XMLHTTPRequest对象的 open方法(),
//初始化XMLHttpRequest组件
//处理缓存问题 url后面再加个时间参数,保证每次请求的url都不同
var url = "jsonObject";
var query = "name="+name;
xmlHttpReq.open("POST",url,true);
// "Get"是请求方式,
//url向后台服务器发送请求的url
//true 代表使用异步请求, 可选参数,默认为true
//3.注册回调函数
xmlHttpReq.onreadystatechange=callBack;
//callBack 为自定义的回调函数的名字 注意:后面没有括号
//当xmlHttpReq对象的readystate状态改变时自动触发 回调函数callBack
//4.把请求发送到服务器
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpReq.send(query); //post请求
}else{
alert("请输入数据");
}
}
function callBack(){
//alert("readyState:"+xmlHttpReq.readyState);
if(xmlHttpReq.readyState==4){//ajax引擎初始化成功
if(xmlHttpReq.status==200){//与tomcat(服务器)交互成功,http协议成功
alert("xmlHttpReq.status:"+xmlHttpReq.status);
var json = xmlHttpReq.responseText;
//通过responseText 属性,取出服务器端返回的数据
alert(json);
var obj = eval('(' + json+ ')');//通过eval函数把jsonObject对象转换js中的对象
alert("obj"+obj);
alert(obj.name);
alert(obj.age);
alert(obj.sex);
}
}
}
</script>
</head>
<body>
<p align="center">
省市二级联动
</p>
<table align="center">
<tr>
<td>
省份
</td>
<td>
<input type="text" onblur="check(this.value)">
</td>
<td>
<span id="sp1"></span>
</td>
</tr>
</table>
</body>
</html>
后台json代码:
package com.wepull.servlet;
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 JsonObjectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
System.out.println("ajax传来的name:"+name);
PrintWriter pw = response.getWriter();
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "小希子");
jsonObj.put("age", new Integer(21));
jsonObj.put("sex", "男");
System.out.println(jsonObj);
pw.println(jsonObj);
}
}
分享到:
相关推荐
方便好用的json解析库,每个json元素都是一个JsonElement对象,通过这个对象,你可以很方便的从Json子类对象中得到你想要的值,不需要你进一步转换,用法如: JsonObject jsonObject = new JsonObject (...); 在...
json-lib包 博文链接:https://yanglei008.iteye.com/blog/215878
org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包 org.json.JSONObject引用依赖包
解析json的工具类,包括JSONObject、JSONArray、JSONException、JSONString等
Unity使用jsonreader读取json数据,里面还附送Litjson文件和Jsonobject下载方式
简单的Json转化类,Json格式与Json对象之间互相转换。 示例:(文件内附示例) string s = "{\r\n" + "\"name\":\"Bill Gates\",\r\n"+ "\"street\":\"Fifth Avenue New York 666\",\r\n"+ "\"age\":56,\r\n"+ ...
此资源包含服务端使用的JSONObject的jar包以及服务端和客户端的代码
JSONObject json=new JSONObject()                   //【这里在JSON中包含一个Map】                  Map map=new HashMap<...
最近在研究Webservice的时候遇到一个问题,把java对象转化问json后传过去,接受的是String类型,怎么恢复到原来的java对象呢,后来再一个项目中看到了转换方法,转换顺序为javaBean--jsonObject--jsonString,还原...
json.jar文件,编程使用JSONObject类需要添加的jar包。
org.json.JSONObject的6个jar包 commons-beanutils;commons-collections;commons-lang;commons-logging;ezmorph;json-lib
org.json.JSONObect的一个jar包org.json.JSONObect的一个jar包
JSONObject JSONArray JavaBean和json数据的转换等,都可以使用。
C#的一个解析JSON的工具类,像java一样优雅的解析JSON数据,包含有JSONObject和JSONArray对象。 JSONObject json = new JSONObject(); json.Put("sex", "男"); json.Put("age", 123); json.Put("name", "张三"); ...
net.sf.json.JSONObject相关依赖架包,导入项目后,JSONObject.fromObject()方法就可以使用了
NULL 博文链接:https://bijian1013.iteye.com/blog/2310861
解决net.sf.json.JSONObject 解决net.sf.json.JSONObject