当前位置:
编程学习 >
JAVA >>
Java乔晓松-ajax实现用户名和邮箱唯一验证实例(struts2框架和HIbernate)
ajax实现用户名和邮箱的唯一的验证
采用struts2+hibernate+ajax实现user用户注册时用户名和邮箱的唯一验证
index.jsp的源代码:
<%@ import="java.util.*" language="java" page="" pageencoding="UTF-8">%@>
<%@ import="java.util.*" language="java" page="" pageencoding="UTF-8">
<%@ prefix="s" taglib="" uri="/struts-tags">%@>
<%@ prefix="s" taglib="" uri="/struts-tags">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%< div="">
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/util.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/reg.js"></script>
reg.js(验证)文件的源代码:
window.onload = function() {
var name = $("adminName");
var email = $("adminEmail");
name.onblur = function() {
changeblur("cname", "name", name);
};
email.onblur = function() {
changeblur("cmail", "email", email);
};
};
function changeblur(msg, type, btn) {
var xhr = createXHR();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 304) {
$(msg).innerHTML = xhr.responseText;
}
}
};
var url = "./csdn/AdminAction_check.action?time=" + new Date().getTime();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var content = type + "=" + btn.value;
xhr.send(content);
}
util.js(工具js)文件的源代码:
function $(id) {
return document.getElementById(id);
}
function createXHR() {
var xmlHttpRequest;
var aVersion = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0",
"MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp" ];
try {
xmlHttpRequest = new XMLHttpRequest();
} catch (ex) {
for ( var i = 0; i < aVersion.length; i++) {
try {
xmlHttpRequest = new ActiveXObject(aVersion[i]);
return xmlHttpRequest;
} catch (ex) {
continue;
}
}
}
return xmlHttpRequest;
}
AdminAction.java文件的源代码:
package www.csdn.net.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import www.csdn.net.service.AdminService;
import www.csdn.net.service.AdminServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
* AdminAction 2013-3-13 下午4:42:14
*
* @author 乔晓松
*
*/
public class AdminAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private long time;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String reg() {
return "reg";
}
public String check() {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
补充:Web开发 , 其他 ,