ajax和struts结合实现无刷新验证用户名是否存在

ajax和struts结合实现无刷新验证用户名是否存在

关键字: ajax, struts
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<TITLE>Ajax验证用户名是否存在例子</TITLE>
<script>

//设一个变量

var XMLHttpReq=false;
    //创建一个XMLHttpRequest对象
    function createXMLHttpRequest(){
      if(window.XMLHttpRequest){ //Mozilla
       XMLHttpReq=new XMLHttpRequest();
       }
       else if(window.ActiveXObject){
        try{
         XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
         }catch(e){
          try{
           XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
           }catch(e){}
           }
          }
         }
    //发送请求函数
    function send(url){
     createXMLHttpRequest();
     XMLHttpReq.open("get",url,true);
     XMLHttpReq.onreadystatechange=proce;     //指定响应的函数
     XMLHttpReq.send(null);    //发送请求
     }
    function proce(){
     if(XMLHttpReq.readyState==4){ //对象状态
      if(XMLHttpReq.status==200){//信息已成功返回,开始处理信息    
                  <!--测试读取xml开始-->
      var root=XMLHttpReq.responseXML;
      var res=root.getElementsByTagName("content")[0].firstChild.data;
      window.alert(res);
       <!--测试读取xml结束-->   
      //var xmlReturn = XMLHttpReq.responseText;
      //window.alert(xmlReturn);
      }else{
       window.alert("所请求的页面有异常");
       }
       }
       }
    //身份验证
    function check(){
     var name=document.getElementById("name").value;
    
      if(name==""){
       alert("请输入姓名!");
       return false;
       }
       else{
        //send('login?name='+name);  
        document.getElementById("load").style.display='';    
        send('login.do?name='+name);
        document.getElementById("load").style.display='none';          
        }
       }
     
    </script>
</head>

<body>
<form action="login" method="post">
    <table>
        <tr><td>姓名:&nbsp;<input id="name" type="text" name="name"/><p>
         <div id=load style="display:none; position:absolute;right:0px;top:0px;background:#FF5B5B;border:1px solid">正在验证用户名,请稍后……</div><p>
           <input type="button" value="检测!" onClick="check()"/>
        </td>
       </tr>
       </table>
       </form>
</body>
</html>



structs-config 配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" " http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans >
     <form-bean name="loginForm" type="org.viman.struts.form.LoginForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
     <action
       attribute="loginForm"
       input="/login.jsp"
       name="loginForm"
       path="/login"
       scope="request"
       type="org.viman.struts.action.LoginAction" />

</action-mappings>

<message-resources parameter="org.viman.struts.ApplicationResources" />
</struts-config>



action实现(LoginAction.java):

package org.viman.struts.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.viman.struts.form.LoginForm;

/**
* MyEclipse Struts
* Creation date: 01-06-2007
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
//LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
try {
       request.setCharacterEncoding("gb2312");
      } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
      }
      try{
       String msgStr="";
       response.setContentType("text/xml;charset=GB2312");
       response.setHeader("Cache-Control","no-cache");
       String name=(String)request.getParameter("name");
       System.out.println("name = "+name);
       if("gong".equals(name)){ //找查数据库中有无该用户名
       msgStr ="对不起,此用户名已经存在,请更换用户名注册!";
       }else{
       msgStr ="用户未被注册,可以使用!";
       }
       response.getWriter().println("<?xml version='1.0' encoding='GB2312' ?>");
       response.getWriter().println("<root>");
       response.getWriter().println("<content>");
       response.getWriter().print(msgStr);
       response.getWriter().println("</content>");
       response.getWriter().println("</root>");
       response.getWriter().close();
       }catch(Exception ex){

       }  
      return null;


}

你可能感兴趣的:(ajax和struts结合实现无刷新验证用户名是否存在)