ajax--asynchronously checks for user names for example _ajax related

Source: Internet
Author: User
Tags reset
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" >
<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 ();

Set the request mode to get, set the URL of the request, set to commit asynchronously
Xmlhttp.open ("Get", "checkservlet?username=" +username,true);

Copy the method address to the onReadyStateChange property
Similar to phone number
Xmlhttp.onreadystatechange = Checkusernamecallback ();
Send setup information to an 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>
<body>
<form action= "regist.jsp" method= "POST" >
User name: <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>

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, IOException {
This.dopost (request, response);
}
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
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 ();
}
}
}
}
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.