1. Client page Code
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title> detect if user name is unique </title>
<script language= "JavaScript" >
function createrequest (URL) {
Http_request = false;
if (window. XMLHttpRequest) {//Non IE browser
Http_request = new XMLHttpRequest (); Creating XMLHttpRequest Objects
else if (window. ActiveXObject) {//IE browser
try {
Http_request = new ActiveXObject ("Msxml2.xmlhttp"); Creating XMLHttpRequest Objects
catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP"); Creating XMLHttpRequest Objects
catch (e) {
}
}
}
if (!http_request) {
Alert ("Cannot create a XMLHttpRequest object instance!") ");
return false;
}
Http_request.onreadystatechange = GetResult; Call return result handler function
Http_request.open (' Get ', url, true); Create a connection to the server
Http_request.send (NULL); Send a request to the server
}
function GetResult () {
if (http_request.readystate = = 4) {//Judge request status
if (Http_request.status = = 200) {//request succeeded, start processing return result
document.getElementById ("ToolTip"). InnerHTML = Http_request.responsetext; Set the content of the hint
document.getElementById ("ToolTip"). Style.display = "block"; Show Prompt Box
} else {//Request page has error
Alert ("The page you requested has an error!") ");
}
}
}
function CheckUser (userName) {
if (Username.value = = "") {
Alert ("Please enter a username!") ");
Username.focus ();
Return
} else {
Createrequest (' http://10.65.9.181:8090/ajax/checkUser.jsp?user= ' +username.value);
Createrequest (' http://10.65.9.181:8090/ajax/checkUser.action?user= '
+ Username.value);
}
}
</script>
<style type= "Text/css" >
<!--
#toolTip {
Position:absolute;
left:331px;
top:39px;
width:98px;
height:48px;
padding-top:45px;
padding-left:25px;
padding-right:25px;
Z-index:1;
Display:none;
color:red;
Background-image:url (images/tooltip.jpg);
}
-->
</style>
<body style= "margin:0px;" >
<form method= "POST" action= "Name=" Form1 ">
<table width= "509" height= "352" border= "0" align= "center"
cellpadding= "0" cellspacing= "0" background= "images/bg.gif" >
<tr>
<TD height= "Si" > </td>
</tr>
<tr>
<TD height= "253" valign= "Top" >
<div style= "Position:absolute;" >
<table width= "100%" height= "0" border= "0"
cellspacing= "0" >
<tr>
<TD width= "18%" height= "si" align= "right" style= "color: #8e6723" ><b> username:</b></td>
<TD width= "49%" ><input name= "username" type= "text"
Id= "username" size= "></td>"
<TD width= "33%" >height= "style=" Cursor:hand;
onclick= "CheckUser (form1.username);" ></td>
</tr>
<tr>
<TD height= "Wuyi" align= "right" style= "color: #8e6723" ><b> password:</b></td>
<td><input name= "pwd1" type= "password" id= "pwd1" size= "></td>"
<TD rowspan= "2" >
<div id= "ToolTip" ></div>
</td>
</tr>
<tr>
<TD height= "align=" "right" style= "color: #8e6723" ><b> Confirm password:</b></td>
<td><input name= "pwd2" type= "password" id= "Pwd2" size= "></td>"
</tr>
<tr>
<TD height= "align=" "right" style= "color: #8e6723" ><b>E-mail:</b></td>
<TD colspan= "2" ><input name= "Email" type= "text" id= "email"
Size= "></td>"
</tr>
<tr>
<td> </td>
<TD colspan= "2" ><input type= "image" Name= "ImageField"
Src= "Images/registerbt.jpg" ></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
</body>
2. Server-side code
The code for the Action class
Copy Code code as follows:
Package com.action;
Import Java.util.Map;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
Import Com.xzy.UserDAO;
public class Checkuseraction extends actionsupport{
Private String user;
Public String Finduserbyname () {
String info = null;
Userdao Userdao = new Userdao ();
if (userdao.finduserbyname (user)) {
info= "User name has been registered";
Map map = (map) actioncontext.getcontext (). Get ("request");
Map.put ("info", "User name has been registered");
Return "Success";
}else{
info= "User name can be registered";
Map map = (map) actioncontext.getcontext (). Get ("request");
Map.put ("info", "User name can be registered to use");
return "fail";
}
}
Public String GetUser () {
return user;
}
public void SetUser (String user) {
This.user = user;
}
}
Struts.xml Configuration
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name= "MyPackage" extends= "Struts-default" >
<!--define Action-->
<action name= "CheckUser" class = "com.action.CheckUserAction" method= "Finduserbyname" >
<!--define a successful mapping page after processing-->
<result >/info.jsp</result>
</action>
</package>
</struts>
info.jsp for Display Information page
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%=request.getattribute ("info")%>
INFO.JSP is a JSP page that, for JSP pages interacting with an Android client, omits unnecessary HTML code, only the code that controls the encoded format and the processing code between <%%>, so that unwanted spam code is not displayed on the Android client. It also improves execution efficiency and reduces server load.
Database screenshot:
Final Effect Diagram: