在很多注冊(cè)的頁(yè)面上,我們可能會(huì)遇到以下情況,當(dāng)我們注冊(cè)用戶名的時(shí)候可能會(huì)提示該用戶名已注冊(cè),其實(shí)現(xiàn)是應(yīng)用AJAX技術(shù)。
首先寫(xiě)一個(gè)登陸頁(yè)面
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="正在驗(yàn)證。。。";
}
function checkUseridCallback(){
if(xmlHttp.readyState==400){
if(xmlHttp.status==200){
var text=xmlHttp.responseText;
if(text=="true"){
flag=false;
document.getElementById("msg").innerHTML="用戶ID重復(fù),無(wú)法使用";
}else{
flag=true;
document.getElementById("msg").innerHTML="此用戶ID可以注冊(cè)";
}
}
}
}
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="注冊(cè)">
input type="reset" value="重置">
/form>
/body>
/html>
隨后寫(xiě)一個(gè)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中進(jìn)行配置
?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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- jquery ajax 檢測(cè)用戶注冊(cè)時(shí)用戶名是否存在
- PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
- JQuery運(yùn)用ajax注冊(cè)用戶實(shí)例(后臺(tái)asp.net)
- PHP+AJAX實(shí)現(xiàn)無(wú)刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(cè)(驗(yàn)證用戶名是否存)
- jquery+ajax實(shí)現(xiàn)注冊(cè)實(shí)時(shí)驗(yàn)證實(shí)例詳解
- PHP+Ajax檢測(cè)用戶名或郵件注冊(cè)時(shí)是否已經(jīng)存在實(shí)例教程
- ajax對(duì)注冊(cè)名進(jìn)行驗(yàn)證檢測(cè)是否存在于數(shù)據(jù)庫(kù)中
- asp ajax注冊(cè)驗(yàn)證之 防止用戶名輸入空格
- Ajax+asp應(yīng)用實(shí)例 注冊(cè)模塊,表單提交