A simple Ajax login example

Source: Internet
Author: User

1. View (loginajax. jsp)

Code:
  1. <% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
  2. <Head>
  3. <Meta http-equiv = Content-Type content = "text/html; charset = UTF-8">
  4. </Head>
  5. <Script language = "JavaScript">
  6. VaR xmlhttpreq = false;
  7. // Create an XMLHTTPRequest object
  8. Function createxmlhttprequest (){
  9. If (window. XMLHttpRequest) {// Mozilla Browser
  10. Xmlhttpreq = new XMLHttpRequest ();
  11. } Else if (window. activexobject) {// IE browser
  12. Try {
  13. Xmlhttpreq = new activexobject ("msxml2.xmlhttp ");
  14. } Catch (e ){
  15. Try {
  16. Xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ");
  17. } Catch (e ){
  18. }
  19. }
  20. }
  21. }
  22. // Send the request Function
  23. Function sendrequest (URL ){
  24. Createxmlhttprequest ();
  25. Xmlhttpreq. Open ("get", URL, true );
  26. Xmlhttpreq. onreadystatechange = processresponse; // specify the response function.
  27. Xmlhttpreq. Send (null); // send the request
  28. }
  29. // Function for processing returned information
  30. Function processresponse (){
  31. If (xmlhttpreq. readystate = 4) {// judge the object status
  32. // Window. Alert (xmlhttpreq. status); Test
  33. If (xmlhttpreq. Status = 200) {// The information has been returned successfully. Start to process the information.
  34. VaR res = xmlhttpreq. responsexml. getelementsbytagname ("res") [0]. firstchild. Data;
  35. Window. Alert (RES );
  36. } Else {// The page is abnormal.
  37. Window. Alert ("the page you requested has an exception. ");
  38. }
  39. }
  40. }
  41. // Identity verification function
  42. Function usercheck (){
  43. VaR uname = Document. myform. uname. value;
  44. VaR psw = Document. myform. psw. value;
  45. If (uname = ""){
  46. Window. Alert ("the user name cannot be blank. ");
  47. Document. myform. uname. Focus ();
  48. Return false;
  49. } Else {
  50. Sendrequest ('loginajax? Uname = '+ uname +' & psw = '+ psw );
  51. }
  52. }
  53. </SCRIPT>
  54. <Body vlink = "#006666" link = "#003366" bgcolor = "# e0f0f8">
  55. /images/enter.gif"
  56. Width = "148">
  57. <Form action = "" method = "Post" name = "myform">
  58. Username: <input size = "15" name = "uname">
  59. <P> password: <input type = "password" size = "15" name = "psw">
  60. <P> <input type = "button" value = "log on to Ajax" onclick = usercheck ();>
  61. </Form>

2. p21.loginaction. Java

Code:
  1. Package p21;
  2. Import java. Io. ioexception;
  3. Import java. Io. printwriter;
  4. Import javax. servlet. servletconfig;
  5. Import javax. servlet. servletexception;
  6. Import javax. servlet. http. httpservlet;
  7. Import javax. servlet. http. httpservletrequest;
  8. Import javax. servlet. http. httpservletresponse;
  9. Public class loginaction extends httpservlet {
  10. Private Static final long serialversionuid = 1l;
  11. Public void Init (servletconfig config) throws servletexception {
  12. }
  13. /*
  14. * Process the <get> request method.
  15. */
  16. Protected void doget (httpservletrequest request, httpservletresponse response)
  17. Throws servletexception, ioexception {
  18. // Set the character set for receiving information
  19. Request. setcharacterencoding ("UTF-8 ");
  20. // Receives information submitted by the browser.
  21. String uname = request. getparameter ("uname ");
  22. String psw = request. getparameter ("psw ");
  23. // Set the format and Character Set of output information
  24. Response. setcontenttype ("text/XML; charset = UTF-8 ");
  25. Response. setheader ("cache-control", "No-Cache ");
  26. // Create an output stream object
  27. Printwriter out = response. getwriter ();
  28. // Output different data information based on the verification result
  29. Out. println ("<response> ");
  30. System. Out. println ("coming to servlet ");
  31. If (uname. Equals ("nothing") & psw. Equals ("123 ")){
  32. Out. println ("<res>" + "Welcome! "+" </RES> ");
  33. } Else {
  34. Out. println ("<res>" + "sorry, Logon Failed! "+" </RES> ");
  35. }
  36. Out. println ("</response> ");
  37. Out. Close ();
  38. }
  39. /*
  40. * Process the <post> request method.
  41. */
  42. Protected void dopost (httpservletrequest request, httpservletresponse response)
  43. Throws servletexception, ioexception {
  44. Doget (request, response );
  45. }
  46. }

3. Web. xml configuration

Code:
  1. <Servlet>
  2. <Servlet-Name> loginajax </servlet-Name>
  3. <Servlet-class> p21.loginaction </servlet-class>
  4. </Servlet>
  5. <Servlet-mapping>
  6. <Servlet-Name> loginajax </servlet-Name>
  7. <URL-pattern>/loginajax </url-pattern>
  8. </Servlet-mapping>

 

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.