用户登录
用户注册

分享至

AJAX 用户注册时的应用实例

  • 作者: 尼古拉斯-梅耶斯-氟多-拉格朗-男爵
  • 来源: 51数据库
  • 2021-09-21
如果我们用ajax技术来实现以上的操作则不必等待服务器返回信息,用户输入用户名或企业名称的时候,当输入文本框失去焦点的时候,则会自动向服务器发出请求,用户继续做下面的操作,不必点击“检查”,也不必等待服务器返回信息,检查与用户操作是异步的,可同时进行。当服务器信息返回的时候,会自动在面页相应位置显示返回信息,不必刷新页面,相当于局部刷新的效果。 下面我们来看代码吧。
html页面的完整代码如下:
程序代码
<%@page language="java" contenttype="text/html;charset=utf-8"%>
<script language="javascript" type="text/javascript">
<!--
/**//**ajax 开始 by alpha 2005-12-31*/
var http = gethttpobject();
function handlehttpresponse(){
 if(http.readystate == 4){
 if(http.status == 200){
  var xmldocument = http.responsexml;
   if(http.responsetext!=""){
    document.getelementbyid("showstr").style.display = "";
     document.getelementbyid("username").style.background= "#ff0000";
     document.getelementbyid("showstr").innertext = http.responsetext;
   }else{
    document.getelementbyid("username").style.background= "#ffffff";
    document.getelementbyid("showstr").style.display = "none";
   }
  }
  else{
   alert("你所请求的页面发生异常,可能会影响你浏览该页的信息!");
   alert(http.status);
  }
 }
}
function handlehttpresponse1(){
 if(http.readystate == 4){
  if(http.status == 200){
   var xmldocument = http.responsexml;
   if(http.responsetext!=""){
    document.getelementbyid("comnmstr").style.display = "";
    document.getelementbyid("comnm").style.background= "#ff0000";
    document.getelementbyid("comnmstr").innertext = http.responsetext;
   }else{
    document.getelementbyid("comnm").style.background= "#ffffff";
    document.getelementbyid("comnmstr").style.display = "none";
   }
  }
  else{
   alert("你所请求的页面发生异常,可能会影响你浏览该页的信息!");
   alert(http.status);
  }
 }
}
function chkuser(){
 var url = "/chkuserandcom";
 var name = document.getelementbyid("username").value;
 url += ("&username="+name+"&oprate=chkuser");
 http.open("get",url,true);
 http.onreadystatechange = handlehttpresponse;
 http.send(null);
 return ;
}
function chkcomnm(){
 var url = "/chkuserandcom";
 var name = document.getelementbyid("comnm").value;
 url += ("&comname="+name+"&oprate=chkcom");
 http.open("get",url,true);
 http.onreadystatechange = handlehttpresponse1;
 http.send(null);
 return ;
}
//该函数可以创建我们需要的xmlhttprequest对象
function gethttpobject(){
 var xmlhttp = false;
 if(window.xmlhttprequest){
  xmlhttp = new xmlhttprequest();
  if(xmlhttp.overridemimetype){
   xmlhttp.overridemimetype('text/xml');
  }
 }
 else{
  try{
   xmlhttp = new activexobject("msxml2.xmlhttp");
  }catch(e){
   try{
    xmlhttp = new activexobject("microsoft.xmlhttp");
   }catch(e){
    xmlhttp = false;
   }
  }
 }
 return xmlhttp;
}
/**//**ajax 结束*/
//检测表单
function chkpassword()
{
var m=document.form1;
if(len(m.password.value) > 20 || len(m.password.value) < 5 || !isstr(m.password.value))
{
 document.getelementbyid("passwordstr").style.display = "";
  document.getelementbyid("password").style.background= "#ff0000";
  document.getelementbyid("passwordstr").innertext = "对不起,密码必须为英文字母、数字或下划线,长度为5~20!";
}
else
{
  document.getelementbyid("password").style.background= "#ffffff";
  document.getelementbyid("passwordstr").style.display = "none";
}
}
function chkconfirmpassword()
{
  var m=document.form1;
  if (m.password.value != m.confirmpassword.value)
  {
   document.getelementbyid("confirmpasswordstr").style.display = "";
   document.getelementbyid("confirmpassword").style.background= "#ff0000";
   document.getelementbyid("confirmpasswordstr").innertext = "对不起,密码与重复密码不一致!";
  }
  else
  {
   document.getelementbyid("confirmpassword").style.background= "#ffffff";
   document.getelementbyid("confirmpasswordstr").style.display = "none";
  }
}
function checkfield()
{
var m=document.form1;
if(m.username.value.length==0)
{
  alert("对不起,用户名必须为英文字母、数字或下划线,长度为5~20。");
  m.username.focus();
  return false;
}
if(m.password.value.length==0)
{
  alert("对不起,密码必须为英文字母、数字或下划线,长度为5~20。");
  m.password.focus();
  return false;
}
if (m.password.value != m.confirmpassword.value)
{
  alert("对不起,密码与重复密码不一致!");
  m.confirmpassword.focus();
  return false;
}
if(m.comnm.value.length==0)
{
  alert("对不起,企业名称不能为空!!");
  m.comnm.focus();
  return false;
}
m.submit();
}
//-->
</script>
<body topmargin="0">
<form name="form1" method="post" action="/control?act=register">
<table width="100%">
<tr><td align="center"> <h2>ajax 演示程序</h1></td></tr>
<tr><td align="center"> ------ 企业注册 by alpha</td></tr>
</table>
<hr>
<table width="400" border="0" cellpadding="1" cellspacing="1" align="center" >
<tr>
  <td><font color="red">*</font></td>
  <td>用户帐号:</td>
  <td>
   <input type="text" name="username" maxlength="20" style="background:#ffffff" onblur="chkuser()" value=""/>
   <div id="showstr" style="background-color:#ff9900;display:none"></div>
  </td>
</tr>
<tr>
  <td><font color="red">*</font></td>
  <td>企业名称:</td>
  <td>
   <input type="text" name="comnm" maxlength="100" style="background:#ffffff" onblur="chkcomnm()" value=""/>
   <div id="comnmstr" style="background-color:#ff9900;display:none"></div>
  </td>
</tr>
<tr>
  <td><font color="red">*</font></td>
  <td>用户密码:</td>
  <td><input type="password" name="password" maxlength="20" style="background:#ffffff" onblur="chkpassword()"/>
   <div id="passwordstr" style="background-color:#ff9900;display:none"></div>
  </td>
</tr>
<tr>
  <td><font color="red">*</font></td>
  <td>确认密码:</td>
  <td><input type="password" name="confirmpassword" maxlength="20" style="background:#ffffff" onblur="chkconfirmpassword()"/>
   <div id="confirmpasswordstr" style="background-color:#ff9900;display:none"></div>
  </td>
</tr>
</table>
<div align="center">
  <input type="button" name="ok" value=" 确 定 " onclick="checkfield()"> <input type="reset" name="reset" value=" 取 消 ">
</div>
</form>
</body>
</html>
  用javascript来创建xmlhttprequest 类向服务器发送一个http请求后,接下来要决定当收到服务器的响应后,需要做什么。这需要告诉http请求对象用哪一个javascript函数处理这个响应。可以将对象的onreadystatechange属性设置为要使用的javascript的函数名,如下所示:xmlhttp_request.onreadystatechange =functionname;
  functionname是用javascript创建的函数名,注意不要写成functionname(),当然我们也可以直接将javascript代码创建在onreadystatechange之后。
  我们调用request.open()-它用服务器打开套接字频道,使用一个http动词(get或post)作为第一个参数并且以数据提供者的url作为第二个参数。request.open()的最后一个参数被设置为true-它指示该请求的异步特性。注意,该请求还没有被提交。随着对request.send()的调用,开始提交-这可以为post提供任何必要的有效载荷。在使用异步请求时,我们必须使用request.onreadystatechanged属性来分配请求的回调函数。(如果请求是同步的话,我们应该能够在调用request.send之后立即处理结果,但是我们也有可能阻断用户,直到该请求完成为止。)
  再看看数据提供者的url,url = "/chkuserandcom",servlet如下:
程序代码
/**//*
* created on 2005-12-31
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package com.event;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.beans.ebaseinfo;
/** *//**
* @author alpha 2005-12-31
*
* <p>ajax 演示---企业注册时检查企业用户名和企业名称</p>
*
* todo to change the template for this generated type comment go to
* window - preferences - java - code style - code templates
*/
public class checkuserandcomnm {
private string msgstr = "";
protected void doget(httpservletrequest request,httpservletresponse response)
throws servletexception
{
 ecombaseinfo info=new ecombaseinfo();
 string oprate=request.getparameter("oprate")).trim();
 string username=request.getparameter("username");
 string password=request.getparameter("password");
 string comname=request.getparameter("comname");
 try
 {
  if(oprate.equals("chkuser"))
  {
   response.setcontenttype("text/html;charset=utf-8");
   if(username.length()<5||username.length()>20)
   {
    msgstr = "对不起,用户名必须为字母、数字或下划线,长度为5-20个字符!";
   }
   else
   {
    boolean btmp=info.finduser(username); //找查数据库中有无该用户名
    if(btmp)
     msgstr ="对不起,此用户名已经存在,请更换用户名注册!";
    else
     msgstr ="";
   }
   response.getwriter().write(msgstr);
  }
  else if(oprate.equals("chkcom"))
  {
   response.setcontenttype("text/html;charset=utf-8");
   if(comname.length()<6||comname.length()>100)
   {
    msgstr = "对不起,公司名称长度为6-100个字符(不包括字符内的空格)!";
   }
   else
   {
    boolean btmp=info.findcom(comname); //找查数据库中有无该企业名
    if(btmp)
     msgstr ="对不起,此企业名称已经存在,请更换企业名称注册!";
    else
     msgstr ="";
   }
   response.getwriter().write(msgstr);
  
  }
 }
 catch(exception ex)
 {
 }
 finally
 {
  request.setattribute("url",url);
 }
}
protected void dopost(httpservletrequest request,httpservletresponse response)
throws servletexception
{
 doget(request,response);
}
}
ajax技术小结
1. ajax(asynchronous javascript and xml)是一个结合了java技术、xml、以及javascript的编程技术,可以让你构建基于java技术的web应用,并打破了使用页面重载的惯例。
2. ajax,异步javascript与xml,是使用客户端脚本与web服务器交换数据的web应用开发方法。这样,web页面不用打断交互流程进行重新加裁,就可以动态地更新。使用ajax,你可以创建接近本地桌面应用的,直接的、高可用的、更丰富的、更动态的web用户接口界面。
3. 对于mozilla.netscape﹑safari、firefox等浏览器,创建xmlhttprequest 方法如下:
xmlhttp_request = new xmlhttprequest();
4. ie等创建xmlhttprequest 方法如下:
xmlhttp = new activexobject("msxml2.xmlhttp") 或 xmlhttp = new activexobject("microsoft.xmlhttp");
5. xmlhttp_request.open('get', url, true); xmlhttp_request.send(null);
6. open()的第一个参数是http请求方式—get,post或任何服务器所支持的您想调用的方式。 按照http规范,该参数要大写;否则,某些浏览器(如firefox)可能无法处理请求。第二个参数是请求页面的url。第三个参数设置请求是否为异步模式。如果是true,javascript函数将继续执行,而不等待服务器响应。这就是"ajax"中的"a"。
ajax技术运用的好的话,给我们的网页增添了许多友好的效果,给用户还来更好的感觉。ajax是个好东西。
软件
前端设计
程序设计
Java相关