Use Ajax Java struts to verify user names

Source: Internet
Author: User

// Struts for User Name authentication

Package com. mobi5.web. Struts. Action;

Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;

Import com. mobi5.commons. domain. sys_mobileuser_reginfo;

Public class validusernameaction extends baseaction {
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
String username = request. getparameter ("user_name ");
Username = new string (username. Trim (). getbytes ("iso8859_1"), "GBK ");
Sys_mobileuser_reginfo user = new sys_mobileuser_reginfo ();
User. setuser_name (username );
// User = This. getidservice (). getuserbyusername (username );
User = This. getidservice (). getuserinfoby (User );
String message = "";
If (user! = NULL ){
Message = "1 ";
Response. setcontenttype ("text/XML; charset = GBK ");
Response. getwriter (). Write (Message );
Response. getwriter (). Close ();
} Else {
Message = "0 ";
Response. setcontenttype ("text/XML; charset = GBK ");
Response. getwriter (). Write (Message );
Response. getwriter (). Close ();
}
Return NULL;
}
}

// JSP page

 

// Verify whether the user name exists
VaR XMLHTTP;
Function createxmlhttprequest (){
If (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
} Else if (window. activexobject)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
 
}
 
Function valid (){

If (form2.user _ name. value = "" | trim (form2.user _ name. Value). Length = 0 ){
Alert ("the user name cannot be blank! ")
Return false;
}
VaR message = Document. getelementbyid ("message1 ");

Message. innerhtml = "";
Createxmlhttprequest ();

Window. Status = '';
Url = "validusernameaction. do? User_name = "+ document. form2.user _ name. value;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = validusername;
XMLHTTP. Send (null );
 
}
 
Function validusername (){

If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200 | XMLHTTP. Status = 0)
{
Restext = XMLHTTP. responsetext;
If (restext = "1 "){
VaR message = Document. getelementbyid ("message1 ");
Message. innerhtml = "<span style = 'color: red'> & nbsp; <B> the user name already exists. select another user name! </B> </span> ";
Form2.user _ name. value = "";
Form2.user _ name. Focus ();
} Else {
VaR message = Document. getelementbyid ("message1 ");
Message. innerhtml = "<span style = 'color: red'> & nbsp; <B> the user name can be used! </B> </span> ";
}
}
}
}

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.