jquery訪問servlet并返回?cái)?shù)據(jù)到頁面的方法
本文實(shí)例講述了jquery訪問servlet并返回?cái)?shù)據(jù)到頁面的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1. servlet:AjaxServlet.java如下:
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ù)是否有問題
//String name = new String(old.getBytes("iso8859-1"),"UTF-8");
String name = URLDecoder.decode(old,"UTF-8");
if("".equals(old) || old == null){
out.println("用戶名必須輸入");
}else{
if("liling".equals(name)){
out.println("恭喜登錄成功");
return;
}else{
out.println("該用戶名未注冊,您可以注冊["+name+"]這個用戶名"+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如下:
//解決中文亂碼問題的方法1,頁面端發(fā)出的數(shù)據(jù)作一次encodeURI,服務(wù)端使用new String(old.getBytes("iso8859-1"),"UTF-8");
//解決中文亂碼問題的方法2,頁面端發(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地址增加時間蒫,難過瀏覽器,不讀取緩存
function convertURL(url){
//獲取時間戳
var timstamp = (new Date()).valueOf();
//將時間戳信息拼接到url上
if(url.indexOf("?") >=0){
url = url + "&t=" + timstamp;
}else{
url = url + "?t=" + timstamp;
}
return url;
}
3. 前臺頁面如下:
<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">請輸入用戶名:</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>
希望本文所述對大家的Ajax程序設(shè)計(jì)有所幫助。
相關(guān)文章
jQuery AJAX實(shí)現(xiàn)調(diào)用頁面后臺方法
這篇文章主要為大家詳細(xì)介紹了jQuery AJAX實(shí)現(xiàn)調(diào)用頁面后臺方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05使用Ajax方法實(shí)現(xiàn)Form表單的提交及注意事項(xiàng)
這篇文章主要介紹了使用Ajax方法實(shí)現(xiàn)Form表單的提交及注意事項(xiàng),需要的朋友可以參考下2017-07-07使用Ajax實(shí)現(xiàn)簡單的帶百分比進(jìn)度條實(shí)例
最近做項(xiàng)目遇到這樣的需求要求當(dāng)進(jìn)行文件長傳保存等操作時,能在頁面顯示一個帶百分比的進(jìn)度條,給用戶一個好的交互體驗(yàn),下面通過實(shí)例代碼給大家介紹基于ajax實(shí)現(xiàn)帶百分比進(jìn)度條效果,需要的的朋友參考下吧2017-07-07使用getJSON()異步請求服務(wù)器返回json格式數(shù)據(jù)的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄褂胓etJSON()異步請求服務(wù)器返回json格式數(shù)據(jù)的實(shí)現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06基于Ajax+div的“左邊菜單、右邊內(nèi)容”頁面效果實(shí)現(xiàn)
這篇文章主要介紹了基于Ajax+div的“左邊菜單、右邊內(nèi)容”頁面效果實(shí)現(xiàn) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07理解jquery ajax中的datatype屬性選項(xiàng)值
jquery中ajax的dataType屬性用于指定服務(wù)器返回的數(shù)據(jù)類型,如果不指定,jQuery 將自動根據(jù)HTTP包MIME信息來智能判斷,如果datatype選項(xiàng)不填寫的話,會將返回的數(shù)據(jù)當(dāng)成字符串處理。2015-10-10