LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

Ajax交互简单实例

admin
2010年8月18日 14:23 本文热度 3194

下面以注册过程中用户名是否存在的验证为例介绍AJAX的应用。

功能描述:用户注册的用户名不允许重复,所以在用户提交时候需要判断。为了让用户早知道结果,在用户输入用户名之后就应该进行判断,可以采用AJAX进行处理。

假设:为了简化代码,该实例的验证过程不使用数据库,并且也不使用专门的JavaBean,直接在Servlet中验证,假设已有用户“zhangsan”、“lisi”和“wangwu”。

实例包含两个文件:

l         界面文件;

l         服务器端处理文件。

注册界面的代码如下


<%@ 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){ // 判断对象状态

      if(xMLHttpRequest.status==200){ // 信息已经返回,开始处理信息

         var res = xMLHttpRequest.responseText;

         //window.alert(res);

         document.getElementById("userlabel").innerText=res;

      }else{ // 页面不正常

         window.alert("您所请求的页面有异常!");

      }

   }

}

function usercheck(){

   createXMLHttpRequest();

   xMLHttpRequest.open("GET","check?username="+document.registerform.username.value,true);

   xMLHttpRequest.onreadystatechange=processResponse; //指定响应函数

   xMLHttpRequest.send(null); //发送请求

}

</script>


   

<body>

   <form name="registerform" action="register" method="post">

   <P>&nbsp;注册</P><P>用户名: <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="注册"></p></form>

</body>

</html>

服务器端的主要代码如下(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("用户名已经被占用!");

       }else

       {

           out.println("用户名可以使用!");

       }

       out.flush();

       out.close();

    }

该文章在 2010/8/18 14:23:16 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved