ajax--An example of whether a user name exists asynchronously

Source: Internet
Author: User

When you register a user on any Web site, you will check to see if the user already exists. A long time ago the process was to submit all the data to the server side for verification, obviously the user experience in this way is very bad; then with Ajax, with asynchronous interaction, when the user loses the user name to fill out other information, Ajax sends the information to the server to check if the username has been registered, This way, if the user name already exists, you can give a hint without waiting for the user to submit all the data. This approach greatly improves the user experience.
regist.jsp

Copy Code code as follows:


<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "


pageencoding= "UTF-8"%>


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


<html>


<head>


<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 ">


<title>insert title here</title>


<script type= "Text/javascript" >


var xmlHttp;


//Create Ajax core Objects XMLHttpRequest


function Createxmlhttp () {


if (window. XMLHttpRequest) {


xmlHttp = new XMLHttpRequest ();


}else{


xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");


}


}


function Checkusername (username) {


createxmlhttp ();





//sets the request to get, sets the URL for the request, and sets it to be submitted asynchronously


Xmlhttp.open ("Get", "checkservlet?username=" +username,true);





//Copy the method address to the onReadyStateChange attribute


//Similar to phone number


Xmlhttp.onreadystatechange = Checkusernamecallback ();


//Send setup information to Ajax engine


xmlhttp.send (NULL);


}


function Checkusernamecallback () {


//ajax engine status for Success


if (xmlhttp.readystate = = 4) {


//http protocol status is successful


if (xmlhttp.status = = 200) {


var text = Xmlhttp.responsetext;


if (Text = = "true") {


document.getElementById ("msg"). InnerHTML = "This user name already exists and cannot be used!" ";


}else{


document.getElementById ("msg"). InnerHTML = "This user name can be used";


}


}


}


}


</script>


</head>


<body>


<form action= "regist.jsp" method= "POST" >


Username: <input type= "text" name= "username" onblur= "checkusername (this.value)" ><span id= "MSG" ></span ><br/>


Password: <input type= "password" name= "password" ><br/>


<input type= "Submit" value= "registered" >


<input type= "reset" value= "reset" >


</form>


</body>


</html>


Checkservlet.java

Copy Code code as follows:


public class Checkservlet extends HttpServlet {
private static final long serialversionuid = 1L;
public static final String dbdriver = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String Dburl = "Jdbc:sqlserver://localhost:1433;databasename=bbs";
public static final String dbuser = "sa";
public static final String dbpass = ' pass ';

Public Checkservlet () {
Super ();
}
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexc eption {
This.dopost (request, response);
}
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex ception {
request.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html");
Connection conn = null;
PreparedStatement PST = NULL;
ResultSet rs = null;
PrintWriter out = Response.getwriter ();
String username = request.getparameter("Usernaem");
try{
Class.forName (dbdriver);
conn = drivermanager.getconnection (Dburl,dbuser,dbpass);
String sql = "SELECT COUNT (username) from user where username=?";
PST = conn.preparestatement (SQL);
Pst.setstring (1,username);
rs = Pst.executequery ();
if (Rs.next ()) {
if (rs.getint (1) >0) {//user name already exists
Out.print ("true");
}else{
Out.print ("false");
}

}
}catch (Exception e) {
E.printstacktrace ();
}finally{
try{
Conn.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
}

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.