狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

Ajax交互簡(jiǎn)單實(shí)例

admin
2010年8月18日 14:23 本文熱度 3886

下面以注冊(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>&nbsp;注冊(cè)</P><P>用戶(hù)名: <input type="text" name="username" onClick="usercheck()"><label for=username id="userlabel">不能為空</label></P><P>&nbsp;性別:<input type="radio" checked="checked" value="" name="sex">男 <input type="radio" value="" name="sex"></P><p>&nbsp;<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)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved