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()"/>
<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: |