Jquery Ajax(jsp Servlet)

刚进入一个公司实现,上头让我做几个简单的小模块,需要用到局部刷新的功能,知道ajax可以实现,在网上上搜集很多资料,由于自己的接受能力比较差,很多功能之类的吧是了解的很清楚,下面是自己尝试写的简单的使用 jquery ajax的例子,新手可以参考一下,个人能力有限。


使用jquery 的ajax必须要下载jquery的相关js文件,http://jquery.com/这是jquery的下载地址,在jsp页面引入jquery的就不再赘述,


jsp页面的代码:点击一个按钮,提交(ajax传参数)到后台servlet,后台返回json后,将json中的数据在前台展示。

//查找按钮点击事件
function submita() {
var nid = $("#xq").val();//获取class=xq的值
var usernum = $("#usernum").val();


if (nid > 0) {//小区是否选择


if (usernum == null || usernum == "") {//小区选择之后判断用户号是否填写
//用户号没有输入


$("#usernumerror").dialog();


} else {//用户号输入


var ldh = usernum.split(" ");
var len = ldh.length;
if (len != 3) {
//用户号格式不正确
$("#userformat").dialog();


} else {//用户号格式正确
$
.ajax({
type : "post",
url : "../../CustomerServlet",
dataType : "json",
contentType : "application/x-www-form-urlencoded; charset=utf-8",
data : {
xq : nid,
usernum : usernum,
text : "名"
},
success : function(json) {//返回数据


if ($(json)[0].pid == 0) {
//用户不存在,返回“用户不存在”
$("#usererror").dialog();
} else {
//用户存在,异步加载
$("#xiaoqu").val($(json)[0].neiname);//更改class=xiaoqu的值
$("#usernum1").val($(json)[0].usernum);
$("#username").val($(json)[0].custname);
$("#tel").val($(json)[0].tel);
$("#address").val($(json)[0].address);
$("#cid").val($(json)[0].pid);
if (($(json)[0].pre) == 0) {


$("#check").attr("checked", "");
}
$("#balance").val($(json)[0].balance);
// alert($(json)[0].usernum);
}
}
});


}
}


} else {


$("#neierror").dialog();
}
}



servlet代码(dopost中的代码):



String usernum = request.getParameter("usernum");
String xq = request.getParameter("xq");
String test = request.getParameter("text");


// 截取用戶號 ex:1-1-1


// 這里用的數組的形式截取的 可能會出現下標越界的Exception
String l = null, d = null, h = null, all[];
int nid = 0;
if (usernum == null || xq == null) {


l = "";
d = "";
h = "";
} else {


all = usernum.split(" ");
l = all[0];
d = all[1];
h = all[2];


nid = Integer.parseInt(xq);


}


// 向前臺傳遞參數
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
Customer cust = new Customer();
cust = CustomerDao.getCusByLDHI(l, d, h, nid);
if (cust.getPid() != 0) {
// 拼出来的json数据
// json數據可以通過導入包直接將對象或者數組生成json對象,詳情google一下吧 通过导入相关的jar包可以直接生成json对象

//list转换成json 好像是这样,具体的可以google下。JsonArray json=JsonArray.fromObject();


String data = ("[{\"usernum\":\"" + cust.getLou() + " "
+ cust.getDy() + " " + cust.getHu() + "\",\"address\":\""
+ cust.getAddr() + "\",\"tel\":\"" + cust.getTel()
+ "\",\"id\":\"" + cust.getApid() + "\",\"email\":\""
+ cust.getEmail() + "\",\"custname\":\""
+ cust.getCustomerName() + "\",\"balance\":\""
+ cust.getBalance() + "\",\"dy\":\"" + cust.getDy()
+ "\",\"hkid\":\"" + cust.getHkid() + "\",\"hu\":\""
+ cust.getHu() + "\",\"key\":\"" + cust.getKey()
+ "\",\"loginname\":\"" + cust.getLoginName()
+ "\",\"lou\":\"" + cust.getLou() + "\",\"neiname\":\""
+ cust.getNei().getName() + "\",\"pid\":\"" + cust.getPid()
+ "\",\"pre\":\"" + cust.getPre() + "\",\"remark\":\""
+ cust.getRemark() + "\",\"valid\":\"" + cust.getValid() + "\"}]");

out.write(data);
} else {


String data = ("[{\"pid\":\"" + "0" + "\"}]");
out.write(data);
}


}
String usernum = request.getParameter("usernum");
String xq = request.getParameter("xq");
String test = request.getParameter("text");


// 截取用戶號 ex:1-1-1


// 這里用的數組的形式截取的 可能會出現下標越界的Exception 这个是自己需要的 不需要的可以更改下 或者直接删除
String l = null, d = null, h = null, all[];
int nid = 0;
if (usernum == null || xq == null) {


l = "";
d = "";
h = "";
} else {


all = usernum.split(" ");
l = all[0];
d = all[1];
h = all[2];


nid = Integer.parseInt(xq);


}


// 向前臺傳遞參數
response.setContentType("text/json;charset=UTF-8");//这个建议写上去 缺少这句可能会引起返回数据的时候前台显示中文乱码
PrintWriter out = response.getWriter();
Customer cust = new Customer();
cust = CustomerDao.getCusByLDHI(l, d, h, nid);
if (cust.getPid() != 0) {
// 拼出来的json数据,cust.get...之类的是从数据库查询出的数据,拼成json
// json數據可以通過導入包直接將對象或者數組生成json對象,詳情google一下吧


String data = ("[{\"usernum\":\"" + cust.getLou() + " "
+ cust.getDy() + " " + cust.getHu() + "\",\"address\":\""
+ cust.getAddr() + "\",\"tel\":\"" + cust.getTel()
+ "\",\"id\":\"" + cust.getApid() + "\",\"email\":\""
+ cust.getEmail() + "\",\"custname\":\""
+ cust.getCustomerName() + "\",\"balance\":\""
+ cust.getBalance() + "\",\"dy\":\"" + cust.getDy()
+ "\",\"hkid\":\"" + cust.getHkid() + "\",\"hu\":\""
+ cust.getHu() + "\",\"key\":\"" + cust.getKey()
+ "\",\"loginname\":\"" + cust.getLoginName()
+ "\",\"lou\":\"" + cust.getLou() + "\",\"neiname\":\""
+ cust.getNei().getName() + "\",\"pid\":\"" + cust.getPid()
+ "\",\"pre\":\"" + cust.getPre() + "\",\"remark\":\""
+ cust.getRemark() + "\",\"valid\":\"" + cust.getValid() + "\"}]");

out.write(data);
} else {


String data = ("[{\"pid\":\"" + "0" + "\"}]");
out.write(data);
}


}


jquery ajax简单的差不多就是这个样子,刚毕业没怎么学好,不好 的请见谅。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值