您好,登錄后才能下訂單哦!
ajax對注冊名進(jìn)行驗(yàn)證檢測存在于數(shù)據(jù)庫中的實(shí)例分析,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
這個(gè)是我在網(wǎng)上看到的一個(gè)有錯(cuò)的ajax 我改正確了,卻找不到那個(gè)帖子了。
用servlet驗(yàn)證:
代碼如下:
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");// 接受驗(yàn)證的用戶名
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();
}
}
}
}
前臺(tái)顯示頁面:
復(fù)制代碼 代碼如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!doctype html>
<html lang="en">
<body>
<script>
//onload 當(dāng)頁面加載的時(shí)候會(huì)調(diào)用這個(gè)方法initPage
window.onload = initPage;
function initPage() {
alert("ee");
document.getElementById("userid").onblur = checkUserid;//引用函數(shù)方法。 onblur 事件在用戶離開輸入框時(shí)執(zhí)行 JavaScript 代碼
}
//創(chuàng)建請求對象
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;
}
//檢查用戶名是否重復(fù)
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="正在驗(yàn)證...";
}
//回調(diào)函數(shù)
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重復(fù)";
}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>
看完上述內(nèi)容,你們掌握ajax對注冊名進(jìn)行驗(yàn)證檢測存在于數(shù)據(jù)庫中的實(shí)例分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。