Ajax JS uses ajax to check whether the user name exists

Source: Internet
Author: User

JSP page: JS Code

  1. <SCRIPT type = "text/JavaScript" Language = "JavaScript">
  2. VaR XMLHTTP = false;
  3. Try {
  4. XMLHTTP = new activexobject ("msxml2.xmlhttp ");
  5. } Catch (e ){
  6. Try {
  7. XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
  8. } Catch (E2 ){
  9. XMLHTTP = false;
  10. }
  11. }
  12. If (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined '){
  13. XMLHTTP = new XMLHttpRequest ();
  14. }
  15. // Check whether the user name exists
  16. Function callservercheckuname (){
  17. // Use the get method to encapsulate the request in the URL
  18. VaR uname = Document. getelementbyid ("u_name"). value;
  19. XMLHTTP. Open ("Post", 'checkuname? U_name = '+ uname );
  20. // Set the Server Response Handling Method
  21. XMLHTTP. onreadystatechange = updatepagecheckuname;
  22. // Send the request
  23. XMLHTTP. Send (null );
  24. }
  25. Function updatepagecheckuname (){
  26. If (XMLHTTP. readystate = 4 ){
  27. VaR response = XMLHTTP. responsetext;
  28. If (Response> = 1 ){
  29. Alert ("the user name already exists! ");
  30. } Else {
  31. Alert ("registrable! ");
  32. }
  33. }
  34. }
  35. </SCRIPT>
<SCRIPT type = "text/JavaScript" Language = "JavaScript"> var XMLHTTP = false; try {XMLHTTP = new activexobject ("msxml2.xmlhttp");} catch (E) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ") ;}catch (E2) {XMLHTTP = false ;}} if (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined') {XMLHTTP = new XMLHttpRequest ();} // checks whether the user name has a function callservercheckuname () {// request by using the get method, encapsulate the request in the URL var uname = document. getelementbyid ("u_name "). value; XMLHTTP. open ("Post", 'checkuname? U_name = '+ uname); // sets the Server Response Processing Method XMLHTTP. onreadystatechange = updatepagecheckuname; // send the XMLHTTP request. send (null);} function updatepagecheckuname () {If (XMLHTTP. readystate = 4) {var response = XMLHTTP. responsetext; If (Response> = 1) {alert ("the user name already exists! ");} Else {alert (" registrable! ") ;}}</SCRIPT>

Web. xml configuration file: XML Code

  1. <Servlet>
  2. <Servlet-Name>Checkuname</Servlet-Name>
  3. <Servlet-class>Servlet. ajaxcheckuname</Servlet-class>
  4. </Servlet>
  5. <Servlet-mapping>
  6. <Servlet-Name>Checkuname</Servlet-Name>
  7. <URL-pattern>/Checkuname</Url-pattern>
  8. </Servlet-mapping>
<Servlet> <servlet-Name> checkuname </servlet-Name> <servlet-class> servlet. ajaxcheckuname </servlet-class> </servlet> <servlet-mapping> <servlet-Name> checkuname </servlet-Name> <URL-pattern>/checkuname </url-Pattern> </servlet-mapping>

Servlet page: Java code

  1. Public void dopost (httpservletrequest request, httpservletresponse response)
  2. Throws servletexception, ioexception {
  3. Response. setcontenttype ("text/html; charset = gb2312 ");
  4. Printwriter out = response. getwriter ();
  5. Try {
  6. String uname = request. getparameter ("u_name ");
  7. Registdaoimpl regdao = new registdaoimpl ();
  8. Out. Print (regdao. checkuname (uname ));
  9. } Catch (exception e ){
  10. }
  11. Out. Flush ();
  12. Out. Close ();
  13. }
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = gb2312"); printwriter out = response. getwriter (); try {string uname = request. getparameter ("u_name"); registdaoimpl regdao = new registdaoimpl (); out. print (regdao. checkuname (uname);} catch (exception e) {} Out. flush (); out. close ();}

DAO implementation class: Java code

  1. Public int checkuname (string uname ){
  2. Connection conn = NULL;
  3. Preparedstatement PSTM = NULL;
  4. Resultset rs = NULL;
  5. Int success = 0;
  6. String SQL = "select u_name from u_info where u_name = '" + uname + "' or u_mobtel = '" + uname + "' or u_email = '" + uname + "'";
  7. Try {
  8. Conn = getconnection ();
  9. PSTM = conn. preparestatement (SQL );
  10. Rs = p0000.exe cutequery (SQL );
  11. If (Rs. Next ()){
  12. Success = 1;
  13. }
  14. Return success;
  15. } Catch (exception e ){
  16. E. printstacktrace ();
  17. } Finally {
  18. Try {
  19. Conn. Close ();
  20. } Catch (exception e ){
  21. E. printstacktrace ();
  22. }
  23. }
  24. Return success;
  25. }
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.