Ajax verifies the registration name to see if it exists in the database _ regular expressions

Source: Internet
Author: User
Tags stringbuffer
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 import= "java.util.*"%>
<!doctype html>
<body>
<script>
OnLoad This method is invoked when the page is loaded Initpage
Window.onload = Initpage;
function Initpage () {
Alert ("EE");
document.getElementById ("userid"). onblur = checkuserid;//Reference function method. onblur Event executes JavaScript code when the user leaves the input box
}
Create a Request object
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;
}
Check that the user name is duplicated
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 repeat";
}else{
document.getElementById ("msg"). innerhtml= "This user ID can be registered";

}
}
}
}
</script>
<form action= "check.jsp" method= "POST" >
User id:
<input type= "text" id= "userid" name= "userid" >
<span id= "MSG" ></span>
<br>
Name
<input type= "text" name= "name" >
</form>
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.