實例1:
1)AJAXServer.java
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
public class AJAXServer extends HttpServlet{
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
doGet(httpServletRequest, httpServletResponse);
}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try{
httpServletResponse.setContentType("text/html;charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
Integer inte = (Integer) httpServletRequest.getSession().getAttribute("total");
int temp = 0;
if (inte == null) {
temp = 1;
} else {
temp = inte.intValue() + 1;
}
httpServletRequest.getSession().setAttribute("total",temp);
//1.取參數(shù)
String old = httpServletRequest.getParameter("name");
//String name = new String(old.getBytes("iso8859-1"),"UTF-8");
String name = URLDecoder.decode(old,"UTF-8");
//2.檢查參數(shù)是否有問題
if(old == null || old.length() == 0){
out.println("用戶名不能為空");
} else{
//3.校驗操作
if(name.equals("wangxingkui")){
//4。和傳統(tǒng)應(yīng)用不同之處。這一步需要將用戶感興趣的數(shù)據(jù)返回給頁面段,而不是將一個新的頁面發(fā)送給用戶
out.println("用戶名[" + name + "]已經(jīng)存在,請使用其他用戶名, " + temp);
} else{
out.println("用戶名[" + name + "]尚未存在,可以使用該用戶名注冊, " + temp);
}
}
} catch(Exception e){
e.printStackTrace();
}
}
}
2)ajax的html 頁面(index.html)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>itcast.cn用戶名校驗ajax實例</title>
<!--<meta http-equiv="content-type" content="text/html; charset=gb2312" />-->
<script type="text/javascript" src="jslib/jquery.js"></script>
<script type="text/javascript" src="jslib/verify.js"></script>
</head>
<body>
itcast.cn用戶名校驗的ajax實例,請輸入用戶名: <br/>
<input type="text" id="userName" />
<input type="button" value="校驗" onclick="verify()"/>
<div id="result"></div>
</body>
</html>
3)在web根目錄下建立jslib文件夾,然后引入jquery.js,同時建立verify.js文件
4)Verify.js
function verify() {
//解決中文亂麻問題的方法1,頁面端發(fā)出的數(shù)據(jù)作一次encodeURI,服務(wù)器段使用new String(old.getBytes("iso8859-1"),"UTF-8");
//解決中文亂麻問題的方法2,頁面端發(fā)出的數(shù)據(jù)作兩次encodeURI,服務(wù)器段使用URLDecoder.decode(old,"UTF-8")
var url = "AJAXServer?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上
//url = "AJAXServer"
if (url.indexOf("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "?t=" + timstamp;
}
return url;
}
5)發(fā)布即可
6)心得:
a.基于標準的一些好習(xí)慣,首先標簽名要小寫,其次標簽必須關(guān)閉,第三屬性名必須是小寫的,第四屬性值必須位于雙引號中
b.ajax方式下不需要使用表單來進行數(shù)據(jù)提交,因此不用寫表單標簽
c.ajax方式不需要name屬性,需要一個id的屬性
d.建立一個div用于存放服務(wù)器段返回的信息,開始為空,id屬性定義是為了利用dom的方式找到某一個節(jié)點,進行操作
e.div和span的直觀差異,div中的內(nèi)容獨占行,span中的內(nèi)容和前后其他內(nèi)容相處良好