Jquery-based Ajax login Effect

Source: Internet
Author: User

Using jquery to implement Ajax is more convenient and concise than using the xmlhttp_request object. The following shows a column. First, create a JSP page and introduce JS files from outside.

Code:
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
  2. <HTML>
  3. <Head>
  4. <Title> jquery1.4.2 </title>
  5. <Link type = "text/CSS" rel = "stylesheet" href = "CSS/login1.css"> </link>
  6. <SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.js"> </SCRIPT>
  7. <SCRIPT type = "text/JavaScript" src = "JS/login. js"> </SCRIPT>
  8. <SCRIPT type = "text/JavaScript" src = "JS/loginajax. js"> </SCRIPT>
  9. </Head>
  10. <Body>
  11. <H1 align = "center"> User Logon page
  12. <HR color = "blue">
  13. <Div align = "center">
  14. <Table cellspacing = 5 Border = 5 bodercolor = # ffaa00>
  15. <Tr> <TH colspan = "3" align = "center" bgcolor = # ffaa00> User Logon </Th> </tr>
  16. <Tr>
  17. <TH rowspan = "3" background = "images/2.jpg" style =" width = 90px "> </Th>
  18. <TD> User Name: </TD> <input class = "text" id = "usernameid" type = "text" value = "Please input your name" name = "username"> <label id = "message"> </label> </TD> </tr>
  19. <Tr> <TD> password: </TD> <input class = "text" id = "userpasswordid" value = "123456" type = "password" name = "userpassword"> </TD> </tr>
  20. <Tr> <TD colspan = "2" align = "center"> <input name = "login" type = "Submit" value = "register"> <input type = "Reset "value =" reset "> </TD> </tr>
  21. </Table>
  22. </Div>
  23. </Body>
  24. </Html>

Create the loginajax. js file as follows:

Code:
  1. $ (Document). Ready (function (){
  2. // Obtain the lable label
  3. VaR $ lable = $ ("# message ");
  4. // Obtain the jquery object of the text box
  5. VaR $ txtusername = $ ("# usernameid ");
  6. $ Txtusername. Click (function (){
  7. $Lable.html ("detecting usernames ......");
  8. });
  9. // The cursor loss event
  10. $ Txtusername. Blur (function (){
  11. VaR $ name = $ txtusername. Val ();
  12. // Use the GER method to pass the value
  13. $. Get ("check? Name = "+ $ name, function (data ){
  14. // Use the POST method to pass the value
  15. // $. Post ("checkuser. Do", {Name: $ name, name1: $ name}, function (data ){
  16. // The Return Value of the server is displayed on the page.
  17. Export lable.html (data );
  18. // Alert (data );
  19. });
  20. });
  21. });

Finally, you need to create a servlet to process the values passed by the get () method in jquery, as follows:

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. Printwriter out = NULL;
  12. Out = response. getwriter ();
  13. String name = request. getparameter ("name ");
  14. System. Out. Print (name );
  15. If ("Liping". Equals (name )){
  16. Out. println ("sorry, User name:" + name + "has existed ");
  17. } Else {
  18. Out. println ("Congratulation, User name:" + name + "can use ");
  19. }
  20. Out. Flush ();
  21. Out. Close ();
  22. }
  23. }

Then, open tmocat to see the effect.

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.