Ajax交互簡(jiǎn)單實(shí)例
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
下面以注冊(cè)過(guò)程中用戶(hù)名是否存在的驗(yàn)證為例介紹AJAX的應(yīng)用。 功能描述:用戶(hù)注冊(cè)的用戶(hù)名不允許重復(fù),所以在用戶(hù)提交時(shí)候需要判斷。為了讓用戶(hù)早知道結(jié)果,在用戶(hù)輸入用戶(hù)名之后就應(yīng)該進(jìn)行判斷,可以采用AJAX進(jìn)行處理。 假設(shè):為了簡(jiǎn)化代碼,該實(shí)例的驗(yàn)證過(guò)程不使用數(shù)據(jù)庫(kù),并且也不使用專(zhuān)門(mén)的JavaBean,直接在Servlet中驗(yàn)證,假設(shè)已有用戶(hù)“zhangsan”、“lisi”和“wangwu”。 實(shí)例包含兩個(gè)文件: l 界面文件; l 服務(wù)器端處理文件。 注冊(cè)界面的代碼如下: <%@ page language="java" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script language="javaScript"> var xMLHttpRequest=false; function createXMLHttpRequest(){ if(window.XMLHttpRequest){ // Mozilla瀏覽器 xMLHttpRequest = new XMLHttpRequest(); }else if(window.ActiveXObject){ try{ xMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){} } } } function processResponse(){ if(xMLHttpRequest.readystate==4){ // 判斷對(duì)象狀態(tài) if(xMLHttpRequest.status==200){ // 信息已經(jīng)返回,開(kāi)始處理信息 var res = xMLHttpRequest.responseText; //window.alert(res); document.getElementById("userlabel").innerText=res; }else{ // 頁(yè)面不正常 window.alert("您所請(qǐng)求的頁(yè)面有異常!"); } } } function usercheck(){ createXMLHttpRequest(); xMLHttpRequest.open("GET","check?username="+document.registerform.username.value,true); xMLHttpRequest.onreadystatechange=processResponse; //指定響應(yīng)函數(shù) xMLHttpRequest.send(null); //發(fā)送請(qǐng)求 } </script> <body> <form name="registerform" action="register" method="post"> <P> 注冊(cè)</P><P>用戶(hù)名: <input type="text" name="username" onClick="usercheck()"><label for=username id="userlabel">不能為空</label></P><P> 性別:<input type="radio" checked="checked" value="男" name="sex">男 <input type="radio" value="女" name="sex">女</P><p> <input type="submit" value="注冊(cè)"></p></form> </body> </html> 服務(wù)器端的主要代碼如下(Servlet的部分代碼): public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gb2312"); String username = request.getParameter("username"); response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); if(username.equals("zhangsan") ||username.equals("lisi") ||username.equals("wangwu")){ out.println("用戶(hù)名已經(jīng)被占用!"); }else { out.println("用戶(hù)名可以使用!"); } out.flush(); out.close(); } 該文章在 2010/8/18 14:23:16 編輯過(guò) |
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)... |