這篇文章主要介紹ajax在jquery中請求和servlet中響應(yīng)的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了源匯免費建站歡迎大家使用!
在jsp中,首先,你需要導入jquery的架包:
獲取可返回站點的根路徑:
<% String path = request.getContextPath(); %>
在jquery中寫ajax請求:
<script type="text/javascript"> $(function(){ $(".B").click(function(){ $.ajax({ type: "GET", //對應(yīng)servlet中的方法 url: "<%=path%>" + "/queryEvaluateByuserId.do", //返回是json數(shù)據(jù) dataType: "json", async:false, data:{ }, success: function(data){ str = ""; if(data != null){ //循環(huán)表單列表 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" > <div class="row marg" > <table border="2 " > <tr class="trtd4"> <th>序號</th> <th>業(yè)主名</th> <th>金額</th> </tr> </table> </div> </div>
在servlet中用到了阿里巴巴的快速轉(zhuǎn)換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鍵值對轉(zhuǎn)換成json,傳給jsp response.getOutputStream().write(JSON.toJSONBytes(workers)); }
以上是“ajax在jquery中請求和servlet中響應(yīng)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:ajax在jquery中請求和servlet中響應(yīng)的示例分析
網(wǎng)站地址:http://jinyejixie.com/article10/jposgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、自適應(yīng)網(wǎng)站、企業(yè)建站、定制網(wǎng)站、關(guān)鍵詞優(yōu)化、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)