ajax对注册名进行验证检测是否存在于数据库中
程序员文章站
2022-04-28 23:05:55
这个是我在网上看到的一个有错的ajax 我改正确了,却找不到那个帖子了。 用servlet验证: 复制代码 代码如下: package servlet; import ja...
这个是我在网上看到的一个有错的ajax 我改正确了,却找不到那个帖子了。
用servlet验证:
package servlet;
import java.io.*;
import java.sql.*;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpservlet;
public class checkservlet extends httpservlet {
static httpservletrequest request = null;
static httpservletresponse response = null;
public static void main(string[] args) throws exception, ioexception{
checkservlet c = new checkservlet();
c.dopost(request, response);
}
public static final string dbdriver = "com.mysql.jdbc.driver";
public static final string dburl = "jdbc:mysql://localhost:3306/spring";
public static final string dbuser = "root";
public static final string dbpass = "liangke";
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 conn = null;
preparedstatement pstmt = null;
resultset rs = null;
printwriter out = response.getwriter();
string userid = request.getparameter("userid");// 接受验证的用户名
try {
class.forname(dbdriver);
conn = drivermanager.getconnection(dburl, dbuser, dbpass);
string sql = " select * from user where username=?";
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, userid);
rs = pstmt.executequery();
stringbuffer jsonstr=new stringbuffer();
jsonstr.append("[{flag:'");
if(rs.next()){
jsonstr.append("true',alerttest:'aaa'}]");
out.println(jsonstr.tostring());
}else{
jsonstr.append("false',alerttest:'bbb'}]");
out.println(jsonstr.tostring());
}
out.close();
} catch (exception e) {
e.printstacktrace();
} finally {
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
}
}
前台显示页面:
<%@ page language="java" pageencoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!doctype html>
<html lang="en">
<body>
<script>
//onload 当页面加载的时候会调用这个方法initpage
window.onload = initpage;
function initpage() {
alert("ee");
document.getelementbyid("userid").onblur = checkuserid;//引用函数方法。 onblur 事件在用户离开输入框时执行 javascript 代码
}
//创建请求对象
function createrequest() {
try {
request = new xmlhttprequest();
} catch (tryms) {
try {
request = new activexobject("msxml2.xmlhttp");
} catch (otherms) {
try {
request = new activexobject("microsoft.xmlhttp");
} catch (failed) {
request = null;
}
}
}
return request;
}
//检查用户名是否重复
function checkuserid(){
alert("dd");
request=createrequest();
var thename = document.getelementbyid("userid").value;
request.open("post","checkservlet?userid="+thename,true);
request.onreadystatechange=checkuseridcallback;
request.send(null);
document.getelementbyid("msg").innerhtml="正在验证...";
}
//回调函数
function checkuseridcallback(){
if(request.readystate==4){
if(request.status==200){
var v = request.responsetext;
var value1= eval(v)[0];
if( value1.flag=="true"){
document.getelementbyid("msg").innerhtml="用户id重复";
}else{
document.getelementbyid("msg").innerhtml="此用户id可以注册";
}
}
}
}
</script>
<form action="check.jsp" method="post">
用户id:
<input type="text" id="userid" name="userid">
<span id="msg"></span>
<br>
姓名
<input type="text" name="name">
</form>
</body>
</html>
用servlet验证:
复制代码 代码如下:
package servlet;
import java.io.*;
import java.sql.*;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpservlet;
public class checkservlet extends httpservlet {
static httpservletrequest request = null;
static httpservletresponse response = null;
public static void main(string[] args) throws exception, ioexception{
checkservlet c = new checkservlet();
c.dopost(request, response);
}
public static final string dbdriver = "com.mysql.jdbc.driver";
public static final string dburl = "jdbc:mysql://localhost:3306/spring";
public static final string dbuser = "root";
public static final string dbpass = "liangke";
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 conn = null;
preparedstatement pstmt = null;
resultset rs = null;
printwriter out = response.getwriter();
string userid = request.getparameter("userid");// 接受验证的用户名
try {
class.forname(dbdriver);
conn = drivermanager.getconnection(dburl, dbuser, dbpass);
string sql = " select * from user where username=?";
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, userid);
rs = pstmt.executequery();
stringbuffer jsonstr=new stringbuffer();
jsonstr.append("[{flag:'");
if(rs.next()){
jsonstr.append("true',alerttest:'aaa'}]");
out.println(jsonstr.tostring());
}else{
jsonstr.append("false',alerttest:'bbb'}]");
out.println(jsonstr.tostring());
}
out.close();
} catch (exception e) {
e.printstacktrace();
} finally {
try {
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
}
}
前台显示页面:
复制代码 代码如下:
<%@ page language="java" pageencoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!doctype html>
<html lang="en">
<body>
<script>
//onload 当页面加载的时候会调用这个方法initpage
window.onload = initpage;
function initpage() {
alert("ee");
document.getelementbyid("userid").onblur = checkuserid;//引用函数方法。 onblur 事件在用户离开输入框时执行 javascript 代码
}
//创建请求对象
function createrequest() {
try {
request = new xmlhttprequest();
} catch (tryms) {
try {
request = new activexobject("msxml2.xmlhttp");
} catch (otherms) {
try {
request = new activexobject("microsoft.xmlhttp");
} catch (failed) {
request = null;
}
}
}
return request;
}
//检查用户名是否重复
function checkuserid(){
alert("dd");
request=createrequest();
var thename = document.getelementbyid("userid").value;
request.open("post","checkservlet?userid="+thename,true);
request.onreadystatechange=checkuseridcallback;
request.send(null);
document.getelementbyid("msg").innerhtml="正在验证...";
}
//回调函数
function checkuseridcallback(){
if(request.readystate==4){
if(request.status==200){
var v = request.responsetext;
var value1= eval(v)[0];
if( value1.flag=="true"){
document.getelementbyid("msg").innerhtml="用户id重复";
}else{
document.getelementbyid("msg").innerhtml="此用户id可以注册";
}
}
}
}
</script>
<form action="check.jsp" method="post">
用户id:
<input type="text" id="userid" name="userid">
<span id="msg"></span>
<br>
姓名
<input type="text" name="name">
</form>
</body>
</html>
上一篇: Go语言每天必学之switch语句
下一篇: Lua中实现递归删除一个文件夹