欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AJAX用于判定用户是否注册

程序员文章站 2022-09-01 23:34:28
在很多注册的页面上,我们可能会遇到以下情况,当我们注册用户名的时候可能会提示该用户名已注册,其实现是应用ajax技术。 首先写一个登陆页面

在很多注册的页面上,我们可能会遇到以下情况,当我们注册用户名的时候可能会提示该用户名已注册,其实现是应用ajax技术。

首先写一个登陆页面

<html>
<head>
<title></title>
<script type="text/javascript">
var xmlhttp;
var flag;
function createxmlhttp(){
if(window.xmlhttprequest){
xmlhttp=new xmlhttprequest();
}else{
xmlhttp=new activexobject("microsoft.xmlhttp");
}
}

function checkuserid(userid){
createxmlhttp();
xmlhttp.open("post","checkservlet?userid="+userid);
xmlhttp.onreadystatechange=checkuseridcallback;
xmlhttp.send();
document.getelementbyid("msg").innerhtml="正在验证。。。";
}

function checkuseridcallback(){
if(xmlhttp.readystate==400){
if(xmlhttp.status==200){
var text=xmlhttp.responsetext;
if(text=="true"){
flag=false;
document.getelementbyid("msg").innerhtml="用户id重复,无法使用";

}else{
flag=true;
document.getelementbyid("msg").innerhtml="此用户id可以注册";
}
}
}
}

function checkform(){
return flag;
}

</script>
</head>
<body>

<form action="tt.jsp" method="post" onsubmit="return checkform()">
用户id <input type="text" name="userid" onblur="checkuserid(this.value)"><span id="msg"></span><br>
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="button" value="注册">
<input type="reset" value="重置">
</form>
</body>
</html>

随后写一个servlet的java代码

import java.io.ioexception;
import java.io.printwriter;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class checkservlet extends httpservlet{
public static final string dbdriver = "oracle.jdbc.oracledriver";
public static final string dburl = "jdbc:oracle:thin:@59.173.240.149:1521:heer";
public static final string dbuser = "hnsyu_dev";
public static final string dbpass = "hnsyuok";

public void doget(httpservletrequest request,httpservletresponse response)
throws servletexception,ioexception{
this.dopost(request, response);
}

public void dopost(httpservletrequest request,httpservletresponse response)
throws servletexception,ioexception{
request.setcharacterencoding("gbk");
response.setcontenttype("text/html");
connection connection = null;
preparedstatement preparedstatement = null;
resultset resultset = null;
printwriter out = response.getwriter();
string userid = request.getparameter("userid");
try {
class.forname(dbdriver);
connection = drivermanager.getconnection(dburl, dbuser, dbpass);
string sql = "select count(userid) from userdemo where userid=?";
preparedstatement = connection.preparestatement(sql);
preparedstatement.setstring(1,userid);
resultset = preparedstatement.executequery();
if (resultset.next()) {
if(resultset.getint(1)>0){
out.print("false");
system.out.println("true");
}else {
out.print("false");
}
}
out.close();
} catch (exception e) {
e.printstacktrace();
}finally{
try {
connection.close();
} catch (exception e) {
e.printstacktrace();
}
}
}
}

其中还需要在web.xml中进行配置

<?xml version="1.0" encoding="utf-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name> 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
  <servlet>
 <servlet-name>checkservlet</servlet-name>
 <servlet-class>checkservlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>checkservlet</servlet-name>
 <url-pattern>/checkservlet</url-pattern>
 </servlet-mapping> 
</web-app>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。