Use AJAX to verify the registration name to determine whether it exists in the database, the specific implementation of the following, perceptual friends can refer to the hope for everyone to help
This is a mistake I saw on the Internet Ajax I changed the correct, but can not find that post.
Verify with servlet:
Copy Code code as follows:
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");//authenticated user name
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 ();
}
}
}
}
Foreground Display page:
Copy Code code as follows:
<%@ page language= "java" pageencoding= "Utf-8"%>
<%@ page import= "java.sql.*"%>
<%@ page I Mport= "java.util.*"%>
<!doctype html>
<body>
<script>
//onload This method is called when the page loads initpage
window.onload = initpage;
Function Initpage () {
alert ("ee");
document.getElementById ("userid"). onblur = checkuserid;//Reference function method. The onblur event executes JavaScript code
}
///Create Request object
Function Createrequest () {
try {
Request = new XMLH When the user leaves the input box) Ttprequest ();
} catch (tryms) {
try {
request = new ActiveXObject ("Msxml2.xmlhttp");
} catch (otherms) {
try {
request = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
//Check to see if the user name repeats
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= "Verifying ...";
}
//callback function
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= "User ID duplicates";
}else{
document.getElementById ("msg"). innerhtml= "This user ID can be registered";
}
}
}
}
</script>
<form action= "check.jsp" method= "POST" >
User id:
& Lt;input type= "text" id= "userid" name= "userid" >
<span id= "msg" ></span>
<br>
Name
&L T;input type= "text" name= "name" >
</form>
</body>