在jsp中,首先,你需要导入jquery的架包:
获取可返回站点的根路径:
<% String path = request.getContextPath(); %>
在jquery中写ajax请求:
<script type="text/javascript">
$(function(){
$(".B").click(function(){
$.ajax({
type: "GET",
//对应servlet中的方法
url: "<%=path%>" + "/queryEvaluateByuserId.do",
//返回是json数据
dataType: "json",
async:false,
data:{
},
success: function(data){
str = "";
if(data != null){
//循环表单列表
for (var i in data)
{
var num = parseInt(i) + 1 ;
str +="<tr><td>" + num + "</td><td>"
+ data[i]['name'] + "</td><td>"
+ data[i]['price'] + "元</td>"
+ "</tr>";
}
$(".trtd4").after(str);
}else{
}
},
error: function(data){
}
})
});
}
</script>
jsp部分:
<div class="tab-pane" id="B" style="text-align:center;">
<div class="row marg" >
<table border="2 " style="width:80%;text-align:center;">
<tr class="trtd4">
<th>序号</th>
<th>业主名</th>
<th>金额</th>
</tr>
</table>
</div>
</div>
在servlet中用到了阿里巴巴的快速转换json的包com.alibaba.fastjson.JSON:
private void queryEvaluateByuserId(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ServletException{
HttpSession session=request.getSession();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
Cookie[] cookies = request.getCookies();
int ownerId = 0;
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("ownerId")) {
ownerId = Integer.parseInt(cookie.getValue());
}
}
List<Order> orderList = new ArrayList<>();
List<Evaluate> queryEvaluateList = new ArrayList<>();
orderList = orderServiceImpl.queryOrderList(ownerId, null, null, null, null, null);
List<Map<String, String workers = new ArrayList<Map<String, String();
for(int i = 0;i < orderList.size();i++){
Map<String,String> order = new HashMap<String, String>();
order.put("description", orderList.get(i).getDescription());
order.put("name", orderList.get(i).getOwnerName());
System.out.println(orderList.get(i).getDescription());
order.put("type",orderList.get(i).getTypeName());
queryEvaluateList = orderServiceImpl.queryEvaluateListByUserId(orderList.get(i).getId());
order.put("comment", queryEvaluateList.get(0).getComment());
List<Allocation> allocation = orderServiceImpl.queryAllocationByOrderId(orderList.get(i).getId());
order.put("price", String.valueOf(allocation.get(0).getPrice()));
System.out.println(order);
workers.add(order);
}
//将map键值对转换成json,传给jsp
response.getOutputStream().write(JSON.toJSONBytes(workers));
}
以上这篇浅谈ajax在jquery中的请求和servlet中的响应就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“浅谈ajax在jquery中的请求和servlet中的响应”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。