Simple Ajax-based login verification

Source: Internet
Author: User

Ajax is a collection of multiple technologies (Asynchronous JavaScript and XML)

1. javascript is responsible for operating XMLHttpRequest objects to deal with databases 2.dom( Document Object Model) is responsible for dynamic data display and interaction 3.xml( extensible identification language) is responsible for data exchange and processing 4. XMLHttpRequest is responsible for Asynchronous Data Reading 5. write structured web pages in XHTML (Extensible Hypertext Markup Language) and CSS (Cascading Style Sheets. JSON

Advantages
1. process data without refreshing new requests.
Disadvantages
1. ie5.0, mozilla1.0, and netscape7 are required.
2. The support for streaming media and PDA is not very good.

General Ajax process (Request/Server mode)
Object initialization ---> sending request ---> server acceptance ---> Server Response and Return ---> client acceptance ---> modifying client page content

First, write a JSP page (which contains JS Code), as follows:

Code:
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
  2. <HTML>
  3. <Head>
  4. <Title> logon page </title>
  5. <LINK rel = stylesheet href = "CSS/login.css" type = "text/CSS">
  6. <SCRIPT type = "text/JavaScript">
  7. // Declare a global XMLHTTPRequest object
  8. VaR xmlhttp_request;
  9. // Declare a tag
  10. VaR message;
  11. Function check (){
  12. VaR username = Document. getelementbyid ("usernameid"). value;
  13. Message = Document. getelementbyid ("message ");
  14. Message. innerhtml = "detecting username .....";
  15. Try {
  16. // Obtain the XMLHTTPRequest object that supports Internet Explorer.
  17. If (window. activexobject ){
  18. For (VAR I = 5; I --){
  19. Try {
  20. If (I = 2 ){
  21. Xmlhttp_request = new activexobject ("Microsoft. XMLHTTP"); // greater than 5.0
  22. //
  23. Alert ("1:" + xmlhttp_request.readystate );
  24. }
  25. Else {
  26. Xmlhttp_request = new activexobject ("msxml2.xmlhttp." + I + ". 0"); // 3.0 or 4.0, 5.0
  27. Xmlhttp_request.setrequestheader ("Content-Type", "text/XML ");
  28. Xmlhttp_request.setrequestheader ("charset", "gb2312 ");
  29. }
  30. Break;
  31. } Catch (e ){
  32. Xmlhttp_request = false;
  33. }
  34. }
  35. }
  36. Else if (window. XMLHttpRequest) // non-ie browsers such as Mozilla, Netscape, and Safari
  37. {
  38. Xmlhttp_request = new XMLHttpRequest ();
  39. If (xmlhttp_request.overridemimetype ){
  40. Xmlhttp_request.overridemimetype ('text/xml ');
  41. }
  42. }
  43. }
  44. Catch (e ){
  45. Xmlhttp_request = false;
  46. }
  47. // Open a connection
  48. Alert ("2:" + xmlhttp_request.readystate );
  49. Alert (username );
  50. // Xmlhttp_request.open ("get", "http: // localhost: 8080/ajax/check? Name = "+ username, true );
  51. Xmlhttp_request.open ("Post", "http: // localhost: 8080/ajax/check", true );
  52. // Set the Request Header for post requests
  53. Xmlhttp_request.setrequestheader ("Content-Type", "application/X-WWW-form-urlencoded ");
  54. // Specify the corresponding functions on the processing server.
  55. Alert ("3:" + xmlhttp_request.readystate );
  56. Xmlhttp_request.onreadystatechange = handle;
  57. // Get sends the request
  58. // Xmlhttp_request.send (null );
  59. // Send a POST request
  60. Xmlhttp_request.send ("name =" + username );
  61. }
  62. Function handle (){
  63. Alert ("4:" + xmlhttp_request.readystate );
  64. If (xmlhttp_request.readystate = 4 ){
  65. Alert (xmlhttp_request.status );
  66. If (xmlhttp_request.status = 200 ){
  67. VaR result = xmlhttp_request.responsetext;
  68. Alert (result );
  69. Message. innerhtml = result;
  70. Alert ("5:" + xmlhttp_request.readystate );
  71. }
  72. }
  73. }
  74. </SCRIPT>
  75. </Head>
  76. <Body>
  77. <H1 align = "center"> User Logon page
  78. <HR color = "blue">
  79. <Div align = "center">
  80. <Form action = "login. Do" method = "Post">
  81. <Table cellspacing = 5 Border = 5 bodercolor = # ffaa00>
  82. <Tr> <TH colspan = "3" align = "center" bgcolor = # ffaa00> User Logon </Th> </tr>
  83. <Tr>
  84. <TH rowspan = "3" background = "images/2.jpg" style =" width = 90px "> </Th>
  85. <TD> User name: </TD> <input id = "usernameid" type = "text" class = "message" name = "username" onblur = "check () "> <label id =" message "> </label> </TD> </tr>
  86. <Tr> <TD> password: </TD> <input id = "userpasswordid" class = "message" type = "password" name = "userpassword"> </TD> </tr>
  87. <Tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "register"> <input type = "reset" value = "Reset "> </TD> </tr>
  88. </Table>
  89. </Form>
  90. </Div>
  91. </Body>
  92. </Html>

The focus of this page is the xmlhttp_request object in JS Code, which is the object of the XMLHTTP component. the XMLHTTPRequest object has been implemented in most browsers and has a simple interface that allows data to be transmitted from the client to the server, however, the current operation is not interrupted. The data transmitted using XMLHttpRequest can be in any format, although the data in XML format is recommended from the name.

Next we will write a serlet to process the data sent from the xmlhttp_request object. The data transmitted in the current example is the username value, as shown below:

Code:
  1. Package myclass. serlet;
  2. Import java. Io. ioexception;
  3. Import java. Io. printwriter;
  4. Import javax. servlet. servletexception;
  5. Import javax. servlet. http. httpservlet;
  6. Import javax. servlet. http. httpservletrequest;
  7. Import javax. servlet. http. httpservletresponse;
  8. Public class check extends httpservlet {
  9. Protected void Service (httpservletrequest request, httpservletresponse response)
  10. Throws servletexception, ioexception {
  11. // Output stream
  12. Printwriter out = NULL;
  13. Out = response. getwriter ();
  14. String name = request. getparameter ("name ");
  15. System. Out. Print (name );
  16. If ("Liping". Equals (name )){
  17. Out. println ("sorry, User name:" + name + "has existed ");
  18. } Else {
  19. Out. println ("Congratulation, User name:" + name + "can use ");
  20. }
  21. Out. Flush ();
  22. Out. Close ();
  23. }
  24. }

The servlet uses the printwriter output stream to return the processing result to the client. In the previous JS, the function handle () function is used for processing, thus implementing partial Ajax refresh. After you enter the user name, an onchange () event is generated when the cursor leaves. The user name value is immediately passed to the servlet through the xmlhttp_request object pair value for verification, after verification, the information is immediately returned and displayed on the page, achieving partial refresh.

This is Ajax.

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.