Ajax学习(1) ---ajax调用Struts中的action

CRegister.jsp

<%@page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>Register</title>
<script language="javascript">
//common part.
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;
}
//get an instance of the XMLHttpRequest object
var http = getHTTPObject();
//be used in CRegister.jsp.start
function handleHttpResponseForChkU(){
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";
      }//if(http.responseText!="")
    }else{
          alert("http.status != 200; == "+ http.status);
    }//if(http.status == 200)
  }//if(http.readyState == 4)
}
function chkUser(){
  var name = document.getElementById("userName").value;
  var url = "http://localhost:7001/webModel/chkUserAction.do";
        url += "?userName="+name+"&oprate=chkUser";
  http.open("GET",url,true);
  http.onreadystatechange = handleHttpResponseForChkU;
  http.send(null);///or send the parameter as userName=??
  return;
}
function checkfield(){
 var m=document.form1;
 if(m.userName.value.length==0){
   alert("user name can not be null");
   m.userName.focus();
   return false;
 }
 if(m.password.value.length==0){
   alert("password can not be null");
   m.password.focus();
   return false;
 }
 m.submit();
}
//be used in CRegister.jsp.end
</script>
</head>
<body>
<form action="cRegisterAction.do" name="form1" method="post">
  <table width="100%">
    <tr>
      <td align="center"><H1>Ajax show</H1>
      </td>
    </tr>
    <tr>
      <td align="center">------ Register----</td>
    </tr>
  </table>
  <HR>
  <table width="400" border="0" cellpadding="1" cellspacing="1" align="center">
    <tr>
      <td>
        <font color="red">*</font>
      </td>
      <td>User Name:</td>
      <td>
        <input type="text" value="" maxlength="20" name="userName" style="background:#FFFFFF" οnblur="chkUser();"/><div id="showStr" style="background-color:#FF9900;display:none"></div>
      </td>
    </tr> 
    <tr>
      <td>
        <font color="red">*</font>
      </td>
      <td>password:</td>
      <td>
        <input maxlength="20" name="password" style="background:#FFFFFF" οnblur=""/>
        <div id="passwordStr" style="background-color:#FF9900;display:none">        </div>
      </td>
    </tr>
    <tr>
      <td>
        <font color="red">*</font>
      </td>
      <td>password:</td>
      <td>
        <input maxlength="20" name="confirmPassword" style="background:#FFFFFF" οnblur=""/>
        <div id="confirmPasswordStr" style="background-color:#FF9900;display:none">        </div>
      </td>
    </tr>
   <tr>
      <td>
        <font color="red">*</font>
      </td>
      <td>MSN:</td>
      <td>
        <input type="text" value="" maxlength="100" name="Msn" style="background:#FFFFFF" οnblur=""/>
        <div id="MsnStr" style="background-color:#FF9900;display:none">        </div>
      </td>
    </tr>
  </table>
  <div align="center">
    <input type="submit" value="confirm" name="ok" οnclick="checkfield()"/>
    &nbsp;
    <input type="reset" value="cancel" name="reset"/>
  </div>
</form>
</body>
</html>

Haved a file in Strut.

chkUserAction.java

public class chkUserAction
    extends Action {
  public ActionForward perform(ActionMapping actionMapping,
                               ActionForm actionForm,
                               HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse) throws
      IOException {
    CRegisterActionForm cRegisterActionForm = (CRegisterActionForm) actionForm;

   //get the request parameter.
    String name = servletRequest.getParameter("userName");
    System.out.println("user:"+name);
    //do you logic work here.......
    //response to the page.
    servletResponse.getWriter().write(name+" can not be used");
    return null;
  }
}

effect:

when the User Name 's input text lost focus, ajax's XMLHTTPRequest will open the URL(*.do) and excute the *.class file to complete the logic work, and return the process result.

------ Register----


* User Name:
user can not be used
* password:
* password:
* MSN:

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值