本篇內(nèi)容主要講解“jquery怎么訪問(wèn)servlet并返回?cái)?shù)據(jù)到頁(yè)面”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“jquery怎么訪問(wèn)servlet并返回?cái)?shù)據(jù)到頁(yè)面”吧!
本文實(shí)例講述了jquery訪問(wèn)servlet并返回?cái)?shù)據(jù)到頁(yè)面的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1. servlet:AjaxServlet.java如下:
復(fù)制代碼 代碼如下:
package com.panlong.servlet; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer total = (Integer) req.getSession().getAttribute("total"); int temp = 0; if(total == null ){ temp = 1; }else{ temp = total.intValue() + 1; } req.getSession().setAttribute("total",temp); try { //1.取參數(shù) resp.setContentType("text/html;charset=GBK"); PrintWriter out = resp.getWriter(); String old = req.getParameter("name"); //2.檢查參數(shù)是否有問(wèn)題 //String name = new String(old.getBytes("iso8859-1"),"UTF-8"); String name = URLDecoder.decode(old,"UTF-8"); if("".equals(old) || old == null){ out.println("用戶(hù)名必須輸入"); }else{ if("liling".equals(name)){ out.println("恭喜登錄成功"); return; }else{ out.println("該用戶(hù)名未注冊(cè),您可以注冊(cè)["+name+"]這個(gè)用戶(hù)名"+temp); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //3.檢驗(yàn)操作 } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
2. verify.js如下:
復(fù)制代碼 代碼如下:
function verify(){ //解決中文亂碼問(wèn)題的方法1,頁(yè)面端發(fā)出的數(shù)據(jù)作一次encodeURI,服務(wù)端使用new String(old.getBytes("iso8859-1"),"UTF-8"); //解決中文亂碼問(wèn)題的方法2,頁(yè)面端發(fā)出的數(shù)據(jù)作兩次encodeURI,服務(wù)端使用String name = URLDecoder.decode(old,"UTF-8"); var url = "servlet/AjaxServlet?name="+encodeURI(encodeURI($("#userName").val())); url = convertURL(url); $.get(url,null,function(data){ $("#result").html(data); }); } //給url地址增加時(shí)間蒫,難過(guò)瀏覽器,不讀取緩存 function convertURL(url){ //獲取時(shí)間戳 var timstamp = (new Date()).valueOf(); //將時(shí)間戳信息拼接到url上 if(url.indexOf("?") >=0){ url = url + "&t=" + timstamp; }else{ url = url + "?t=" + timstamp; } return url; }
3. 前臺(tái)頁(yè)面如下:
復(fù)制代碼 代碼如下:
<!DOCTYPE html> <html> <head> <title>AJAX實(shí)例</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=GBK"> <script type="text/javascript" src="js/verify.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <font color="blue" size="2">請(qǐng)輸入用戶(hù)名:</font> <input type="text" id="userName" /><font color="red" size="2"><span id="result" >*</span></font><br/><br/> <!-- <div id="result"></div> --> <input type="submit" name="提交" value="提交" onclick="verify()"/> </body> </html>
到此,相信大家對(duì)“jquery怎么訪問(wèn)servlet并返回?cái)?shù)據(jù)到頁(yè)面”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站標(biāo)題:jquery怎么訪問(wèn)servlet并返回?cái)?shù)據(jù)到頁(yè)面-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article46/ehpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、商城網(wǎng)站、App設(shè)計(jì)、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容