Java乔晓松-ajax实现用户名和邮箱唯一验证实例(struts

发布时间:2017-09-11 12:10:25
Java乔晓松-ajax实现用户名和邮箱唯一验证实例(struts2框架和HIbernate) ajax实现用户名和邮箱的唯一的验证

采用struts2+hibernate+ajax实现user用户注册时用户名和邮箱的唯一验证

index.jsp的源代码:

</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();

}

AdminService adminService = new AdminServiceImpl();

boolean flag = false;

String msg = null;

if (name != null && !name.equals("")) {

flag = adminService.check("aname", name);

msg = "用户名";

} else if(email != null && !email.equals("")){

flag = adminService.check("aemail", email);

msg = "邮箱";

}

//System.out.println(flag);

if (flag) {

out.print(msg+"已存在!");

} else {

out.print(msg+"可以使用!");

}

out.flush();

out.close();

return "reg";

}

}

struts2.xml文件的源代码:

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

method="{1}">

/sc.jsp

UTF-8

AdminDaoInpl.java文件的源代码:

package www.csdn.net.dao;

import org.hibernate.Criteria;

import org.hibernate.criterion.Restrictions;

import www.csdn.net.domain.Admin;

import www.csdn.net.utils.BaseHibernateDao;

import www.csdn.net.utils.HibernateSessionFactory;

/**

* AdminDaoImpl 2013-3-13 下午4:44:29

*

* @author 乔晓松

*

*/

public class AdminDaoImpl extends BaseHibernateDao implements AdminDao {

public boolean check(String type, String value) {

boolean flag = false;

try {www.2cto.com

Criteria criteria = getSession().createCriteria(Admin.class);

criteria.add(Restrictions.eq(type, value));

if (criteria.list().size() > 0) {

flag = true;

}

return flag;

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

HibernateSessionFactory.closeSession();

}

}

}

,采集软件,站群

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉做网站多少钱 http://www.45qun.com