Jquery的Ajax简例

  1. package com.june.servlet;
  2. import javax.servlet.http.HttpServlet;
  3. import javax.servlet.http.HttpServletResponse;
  4. import javax.servlet.http.HttpServletRequest;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import javax.servlet.ServletException;
  8. public class jqueryAjaxServer extends HttpServlet {
  9. public jqueryAjaxServer(){
  10. super();
  11. }
  12. public void doGet(HttpServletRequest request,HttpServletResponse response)
  13. throws IOException ,ServletException {
  14. response.setContentType("text/html;charset=utf-8");
  15. PrintWriter out=response.getWriter();
  16. String account=request.getParameter("account");
  17. if("iamcrzay".equals(account)){
  18. out.print("Sorry,the user is exist");
  19. }
  20. else{
  21. out.print("Congratulation,this accont you can use!!!!");
  22. }
  23. out.close();
  24. }
  25. public void doPost(HttpServletRequest request,HttpServletResponse response)
  26. throws IOException ,ServletException {
  27. this.doGet(request, response);
  28. }
  29. }  
  30.  第一页用的是$.ajax()
    Html代码 收藏代码
    1. <%@ page language="java" pageEncoding="utf-8"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    3. <html>
    4. <head>
    5. <title>jquery ajax</title>
    6. <meta http-equiv="pragma" content="no-cache">
    7. <meta http-equiv="cache-control" content="no-cache">
    8. <meta http-equiv="expires" content="0">
    9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    10. <meta http-equiv="description" content="This is my page">
    11. <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
    12. <script language="javascript">
    13. $(function(){
    14. $('.sumbit').click(function(){
    15. if($('#account').val().length==0){
    16. $('.hint').text("用户名不能位空").css({"background-color":"green"});
    17. }
    18. else{
    19. $.ajax({
    20. url:'jqueryAjax',
    21. data:{account:$('#account').val()},
    22. error:function(){
    23. alert("error occured!!!");
    24. },
    25. success:function(data){
    26. $('body').append("<div>"+data+"</div>").css("color","red");
    27. }
    28. });}
    29. });
    30. });
    31. </script>
    32. </head>
    33. <body>
    34. <h3 align="center">jquery AjaX</h3>
    35. <hr>
    36. <label>请输入用户名 :</label>
    37. <input id="account" name="account" type="text">
    38. <input class="sumbit" type="button" value="检测">
    39. <div class="hint">
    40. </div>
    41. </body>
    42. </html>


    第二个用的是 $.post()

    Html代码 收藏代码
    1. <%@ page language="java" pageEncoding="utf-8"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    3. <html>
    4. <head>
    5. <title>jquery ajax</title>
    6. <meta http-equiv="pragma" content="no-cache">
    7. <meta http-equiv="cache-control" content="no-cache">
    8. <meta http-equiv="expires" content="0">
    9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    10. <meta http-equiv="description" content="This is my page">
    11. <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
    12. <script language="javascript">
    13. $(function(){
    14. $('.sumbit').click(
    15. function(){
    16. if($('#account').val().length==0){
    17. $('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});
    18. }
    19. else{
    20. $.post("jqueryAjax","account="+$('#account').val(),function(data){
    21. $('.hint').text(data).css({"color":"red","background-color":"yellow"});
    22. })
    23. }
    24. });
    25. });
    26. </script>
    27. </head>
    28. <body>
    29. <h3 align="center">jquery Ajax</h3>
    30. <hr>
    31. <label>请输入用户名 :</label>
    32. <input id="account" name="account" type="text">
    33. <input class="sumbit" type="button" value="检测">
    34. <div class="hint">
    35. </div>
    36. </body>
    37. </html>



    第三个是用的$.get()

    Html代码 收藏代码
    1. <%@ page pageEncoding="utf-8"%>
    2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    3. <html>
    4. <head>
    5. <title>jquery get</title>
    6. <meta http-equiv="pragma" content="no-cache">
    7. <meta http-equiv="cache-control" content="no-cache">
    8. <meta http-equiv="expires" content="0">
    9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    10. <meta http-equiv="description" content="This is my page">
    11. <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
    12. <script type="text/javascript">
    13. $(function(){
    14. $('.sumbit').click(function(){
    15. if($('#account').val().length==0){
    16. $('.hint').html("用户名不能位空!!!").css({"color":"#ffoo11","background":"blue"});
    17. }
    18. else{
    19. $.get("jqueryAjax","account="+$('#account').val(),
    20. function(data){
    21. $('.hint').html(data).css({"color":"#ffoo11","background":"green"});
    22. });
    23. }
    24. });
    25. });
    26. </script>
    27. </head>
    28. <body>
    29. <h3 align="center">jquery AjaX</h3>
    30. <hr>
    31. <label>请输入用户名 :</label>
    32. <input id="account" name="account" type="text">
    33. <input class="sumbit" type="button" value="检测">
    34. <div class="hint">
    35. </div>
    36. </body>
    37. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值