加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax异步检验会员注册是否存在

发布时间:2020-12-16 00:36:39 所属栏目:百科 来源:网络整理
导读:从网上整理所得: 注册页面调用ajax请求: %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" html head met

从网上整理所得:

注册页面调用ajax请求:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
//创建Ajax核心对象XMLHttpRequest
function createXMLHttp(){
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function checkUsername(username){
createXMLHttp();

//设置请求方式为GET,设置请求的URL,设置为异步提交
xmlHttp.open("GET","CheckServlet?username="+username,true);
//将方法地址复制给onreadystatechange属性
//类似于电话号码
xmlHttp.onreadystatechange = checkUsernameCallback();
//将设置信息发送到Ajax引擎
xmlHttp.send(null);
function checkUsernameCallback(){
//Ajax引擎状态为成功
if(xmlHttp.readyState == 4){
//HTTP协议状态为成功
if(xmlHttp.status == 200){
var text = xmlHttp.responseText;
if(text == "true"){

document.getElementById("msg").innerHTML = "此用户名已存在,无法使用!";

document.getElementById("msg").style.color="red";

document.getElementById("msg").innerHTML = "此用户名可以使用";

document.getElementById("msg").style.color="green";

</script>
</head>
<body>
<form action="regist.jsp" method="post">
用户名:<input type="text" name="username" onblur="checkUsername(this.value)"><span id="msg"></span><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>

</html>

CheckServlet.java

public class CheckServlet extends HttpServlet { //可以在其中把查询数据库的请求转到对应的model层
private static final long serialVersionUID = 1L;

public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String DBURL = "jdbc:sqlserver://localhost:1433;DatabaseName=bbs";
public static final String DBUSER = "sa";
public static final String DBPASS = "pass";
public CheckServlet() {
super();
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
this.doPost(request,response);
protected void doPost(HttpServletRequest request,sans-serif; font-size:14px; line-height:25px">request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
PrintWriter out = response.getWriter();
String username = request.getParameter("usernaem");
try{
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql = "select count(username) from user where username=?";
pst = conn.prepareStatement(sql);
pst.setString(1,username);
rs = pst.executeQuery();
if(rs.next()){
if(rs.getInt(1)>0){//用户名已经存在了
out.print("true");
out.print("false");
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读