Ajax verifies that the user name is registered

Source: Internet
Author: User

Ajax verifies that the user name is registered

var xmlHttp;
function Createxmlhttprequest () {//Create XMLHTTP Request object
if (window. ActiveXObject) {
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}else if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}
}
Xmlhttp.onreadystatechange= Treatment Method
XmlHttp: This is the XMLHttpRequest object name
Processing method: This is a custom JavaScript method name
function Startrequest (userName) {//Send page request
Createxmlhttprequest ();
Xmlhttp.onreadystatechange=handlestatechange; Specify a method for handling server responses
Xmlhttp.open ("GET", "regsiterservlet?username=" +username,true);
Xmlhttp.send (NULL);
}
<script type= "Text/javascript" >
var xmlHttp;
function Createxmlhttprequest () {//Create XMLHTTP Request object
if (window. ActiveXObject) {
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}else if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}
}
function Handlestatechange () {//Processing answer data
if (xmlhttp.readystate==4) {
if (xmlhttp.status==200) {
document.getElementById (' text '). Innerhtml=xmlhttp.responsetext;
}
}
}
function Startrequest (userName) {//Send page request
Createxmlhttprequest ();
Xmlhttp.onreadystatechange=handlestatechange;
Xmlhttp.open ("GET", "regsiterservlet?username=" +username,true);
Xmlhttp.send (NULL);
}
</script>
protected void ProcessRequest (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter (); Gets the character output stream of the reply object
try {
Userdao dao = new Userdao (); Create a Database action class
String userName = Request.getparameter ("UserName"); Gets the user name parameter of the request object
if (username==null| | Username.isempty ()) {
Back to prompt information
Out.print ("<font color= ' red ' > must enter user name </font>");
Return
}
Boolean hasuser = Dao.hasuser (userName); Determine if the database exists with the specified user name
if (hasuser) {//If there is a duplicate name
Return registered information
Out.print ("<font color= ' Red ' > The user name has been registered </font>");
} else {//otherwise
Prompt user to register
Out.print ("<font color= ' green ' > This user name can be used </font>");
}
} catch (Exception ex) {
Logger.getlogger (RegisterServlet.class.getName ()). log (Level.severe, NULL, ex);
} finally {
Out.close ();
}
}
public boolean hasuser (String userName) {
Boolean hasuser = false; Declares a check variable, false means no duplicate user
Connection conn = Getconn (); Connecting to a database
IF (conn! = null) {
Defining SQL query statements
String sql = "SELECT COUNT (*) from Tb_checkuser where username=?";
try {
PreparedStatement PS = conn.preparestatement (SQL);
Ps.setstring (1, userName); Set parameters for a query statement
ResultSet rs = Ps.executequery (); Execute SQL query
if (Rs.next ()) {
int count = Rs.getint (1); Get the number of users with the same name
if (Count > 0) {//If the number of users with the same name is greater than 0
Hasuser = true; Set check variable to true to represent user name
}
}
Rs.close (); Whether the resource
Ps.close ();
Conn.close ();
} catch (SQLException ex) {
Logger.getlogger (UserDao.class.getName ()). log (Level.severe, NULL, ex);
}
}
return hasuser;
}

Ajax verifies that the user name is registered

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.