<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    如何學好java

    如何學好java,其實很簡單,只要用心體會,慢慢積累!
    posts - 106, comments - 7, trackbacks - 0, articles - 3
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    jquery ajax servlet

    Posted on 2012-02-24 16:16 哈希 閱讀(3998) 評論(3)  編輯  收藏 所屬分類: Js and Jquery 常用總結
    假設:
    1、你的頁面在Web-Root下,內容為:  <div id="showMsg"></div><input type="text" id="userName" />,所用編碼為utf-8
    2、你的servlet為:  HelloWorldServlet.java  映射路徑為   servlet/helloWorldServlet
    步驟:
    1、引入jquery-1.6.4.min.js
    2、編寫id為userName的輸入框的點擊觸發函數:
          $("#userName").keyup(function(){
                $.ajax({
                      type: "post",
                      url: "servlet/helloWorldServlet?userName="+$(this).val(),
                      dataType: "json",
                      success: function(data){
                            $("#showMsg").html(data.msg);//修改id為showMsg標簽的html
                      }, error: function(){
                            alert("請求出錯");
                      }
                })
          })
    3、后臺處理接收到的內容:
          request.setCharactorEncoding("utf-8");
          String userName = request.getParameter("userName");
          response.setCharactorEncoding("utf-8");
          PringWriter out = response.getWriter();
          out.print("{\"msg\":\"你好~~"+userName+"!\"}");
    
    注意事項:
    1、這里的編碼統一為utf-8
    2、請求路徑servlet/helloWorldServlet為相對路徑,因此你的頁面必須在項目的Web-Root下(也就是默認的web文件夾下,名字可能因項目配置不同而改變)
    
    網上的 Jquery ajax Demo 大多都是基于php 
    很少 有java的 今天就把自己的Demo貼出來 和大家共同學習
    現在就  Jquery ajax 的 $.ajax(),$.post(),$.get();

    首先是  服務端的Servlet 演示這三個函數的用法對都是用的同一個 服務端
     package com.june.servlet;   
     
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletResponse;  
    import javax.servlet.http.HttpServletRequest;  
    import java.io.IOException;  
    import java.io.PrintWriter;  
    import javax.servlet.ServletException;  
     
    public class jqueryAjaxServer extends HttpServlet {  
         public jqueryAjaxServer(){  
             super();  
         }  
         public void doGet(HttpServletRequest request,HttpServletResponse response)  
         throws IOException ,ServletException {  
             response.setContentType("text/html;charset=utf-8");  
             PrintWriter out=response.getWriter();  
             String account=request.getParameter("account");  
             if("iamcrzay".equals(account)){  
                 out.print("Sorry,the user is exist");  
             }  
             else{  
                 out.print("Congratulation,this accont you can use!!!!");  
             }  
             out.close();  
         }  
         public void doPost(HttpServletRequest request,HttpServletResponse response)  
         throws IOException ,ServletException {  
             this.doGet(request, response);  
         }  

    下面是WEB.XML
    Xml代碼 
    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="2.5"   
        xmlns="    xmlns:xsi="    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
            <servlet> 
           <servlet-name>jqueryAjaxServer</servlet-name> 
           <servlet-class>com.june.servlet.jqueryAjaxServer</servlet-class> 
        </servlet> 
            <servlet-mapping> 
           <servlet-name>jqueryAjaxServer</servlet-name> 
           <url-pattern>/jqueryAjax</url-pattern> 
        </servlet-mapping> 
      <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
      </welcome-file-list> 
    </web-app> 

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
     xmlns="
     xmlns:xsi=" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      <servlet>
        <servlet-name>jqueryAjaxServer</servlet-name>
        <servlet-class>com.june.servlet.jqueryAjaxServer</servlet-class>
     </servlet>
            <servlet-mapping>
        <servlet-name>jqueryAjaxServer</servlet-name>
        <url-pattern>/jqueryAjax</url-pattern>
     </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

     

    下面是Jsp頁面
    第一個是 jqueryAjax.jsp  本頁使用的是$.ajax()
    Html代碼 
    <%@ page language="java"  pageEncoding="utf-8"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
      <head>    
        <title>jquery ajax</title>   
        <meta http-equiv="pragma" content="no-cache"> 
        <meta http-equiv="cache-control" content="no-cache"> 
        <meta http-equiv="expires" content="0">      
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
        <meta http-equiv="description" content="This is my page"> 
        <script src="js/jquery-1.2.6.js" type="text/javascript"></script> 
        <script language="javascript"> 
             $(function(){  
                   $('.sumbit').click(function(){  
                   if($('#account').val().length==0){  
                       $('.hint').text("用戶名不能位空").css({"background-color":"green"});   
                   }  
                   else{  
                   $.ajax({  
                    
    url:'jqueryAjax',  
                     data:{account:$('#account').val()},  
                     error:function(){  
                     alert("error occured!!!");  
                     },  
                     success:function(data){  
                      $('body').append("<div>"+data+"</div>").css("color","red");  
            
                     }  
                       
                   });}  
                   });  
                   });  
                       
             
                 
        </script> 
      </head> 
        
      <body> 
                    <h3 align="center">jquery AjaX</h3> 
                    <hr> 
                    <label>請輸入用戶名 :</label> 
                    <input id="account" name="account" type="text"> 
                    <input class="sumbit" type="button" value="檢測"> 
                    <div class="hint"> 
                    </div> 
      </body> 
    </html> 

    <%@ page language="java"  pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head> 
        <title>jquery ajax</title>
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
     <script language="javascript">
          $(function(){
                $('.sumbit').click(function(){
                if($('#account').val().length==0){
                    $('.hint').text("用戶名不能位空").css({"background-color":"green"});
                }
                else{
                $.ajax({
                  url:'jqueryAjax',
                  data:{account:$('#account').val()},
                  error:function(){
                  alert("error occured!!!");
                  },
                  success:function(data){
                   $('body').append("<div>"+data+"</div>").css("color","red");
      
                  }
                 
                });}
                });
                });
                    
       
           
     </script>
      </head>
     
      <body>
                    <h3 align="center">jquery AjaX</h3>
                    <hr>
                    <label>請輸入用戶名 :</label>
                    <input id="account" name="account" type="text">
                    <input class="sumbit" type="button" value="檢測">
                    <div class="hint">
                    </div>
      </body>
    </html>

     

    第二個用的是  $.post()

    Html代碼 
    <%@ page language="java"  pageEncoding="utf-8"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
      <head>    
        <title>jquery ajax</title>   
        <meta http-equiv="pragma" content="no-cache"> 
        <meta http-equiv="cache-control" content="no-cache"> 
        <meta http-equiv="expires" content="0">      
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
        <meta http-equiv="description" content="This is my page"> 
        <script src="js/jquery-1.2.6.js" type="text/javascript"></script> 
        <script language="javascript"> 
             $(function(){  
                 $('.sumbit').click(  
                  function(){  
                    if($('#account').val().length==0){  
                        $('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});  
                    }  
                    else{  
                    $.post("jqueryAjax","account="+$('#account').val(),function(data){  
                       $('.hint').text(data).css({"color":"red","background-color":"yellow"});  
                    })  
                    }  
                 });  
             });             
        </script> 
      </head> 
        
      <body> 
                    <h3 align="center">jquery Ajax</h3> 
                    <hr> 
                    <label>請輸入用戶名 :</label> 
                    <input id="account" name="account" type="text"> 
                    <input class="sumbit" type="button" value="檢測"> 
                    <div class="hint"> 
                    </div> 
      </body> 
    </html> 

    <%@ page language="java"  pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head> 
        <title>jquery ajax</title>
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
     <script language="javascript">
          $(function(){
              $('.sumbit').click(
               function(){
                 if($('#account').val().length==0){
                     $('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});
                 }
                 else{
                 $.post("jqueryAjax","account="+$('#account').val(),function(data){
                    $('.hint').text(data).css({"color":"red","background-color":"yellow"});
                 })
                 }
              });
          });       
     </script>
      </head>
     
      <body>
                    <h3 align="center">jquery Ajax</h3>
                    <hr>
                    <label>請輸入用戶名 :</label>
                    <input id="account" name="account" type="text">
                    <input class="sumbit" type="button" value="檢測">
                    <div class="hint">
                    </div>
      </body>
    </html>

     

    第三個是用的$.get()

    Html代碼 
    <%@ page  pageEncoding="utf-8"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
      <head>     
        <title>jquery get</title> 
          
        <meta http-equiv="pragma" content="no-cache"> 
        <meta http-equiv="cache-control" content="no-cache"> 
        <meta http-equiv="expires" content="0">      
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
        <meta http-equiv="description" content="This is my page"> 
        <script src="js/jquery-1.2.6.js" type="text/javascript"></script> 
        <script type="text/javascript"> 
             $(function(){  
                    $('.sumbit').click(function(){  
                          if($('#account').val().length==0){  
                             $('.hint').html("用戶名不能位空!!!").css({"color":"#ffoo11","background":"blue"});  
                          }  
                          else{  
                              $.get("jqueryAjax","account="+$('#account').val(),  
                                   function(data){  
                                    $('.hint').html(data).css({"color":"#ffoo11","background":"green"});  
                              });  
                          }  
                    });  
             });  
        </script> 
     
      </head> 
        
      <body> 
            <h3 align="center">jquery AjaX</h3> 
                    <hr> 
                    <label>請輸入用戶名 :</label> 
                    <input id="account" name="account" type="text"> 
                    <input class="sumbit" type="button" value="檢測"> 
                    <div class="hint"> 
                    </div> 
      </body> 
    </html> 

    <%@ page  pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>  
        <title>jquery get</title>
       
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
     <script type="text/javascript">
          $(function(){
                 $('.sumbit').click(function(){
                       if($('#account').val().length==0){
                          $('.hint').html("用戶名不能位空!!!").css({"color":"#ffoo11","background":"blue"});
                       }
                       else{
                           $.get("jqueryAjax","account="+$('#account').val(),
                                function(data){
                                 $('.hint').html(data).css({"color":"#ffoo11","background":"green"});
                           });
                       }
                 });
          });
     </script>

      </head>
     
      <body>
            <h3 align="center">jquery AjaX</h3>
                    <hr>
                    <label>請輸入用戶名 :</label>
                    <input id="account" name="account" type="text">
                    <input class="sumbit" type="button" value="檢測">
                    <div class="hint">
                    </div>
      </body>
    </html>

     
    http://iamcrzay.iteye.com/blog/237940 
     
     
     
     

    評論

    # re: jquery ajax servlet  回復  更多評論   

    2013-08-09 10:56 by 請求
    dfd

    # re: jquery ajax servlet  回復  更多評論   

    2015-06-16 14:08 by sdfas
    xcvcxvv

    # re: jquery ajax servlet  回復  更多評論   

    2015-06-16 14:08 by sdfas
    sdfasf
    主站蜘蛛池模板: 18禁网站免费无遮挡无码中文| 中国性猛交xxxxx免费看| 99久久久国产精品免费牛牛四川 | 午夜毛片不卡高清免费| ass亚洲**毛茸茸pics| 亚欧色视频在线观看免费| 亚洲成人免费在线观看| 国产大片线上免费观看| 亚洲а∨天堂久久精品9966| 影音先锋在线免费观看| 亚洲国产精品无码久久| 国产99视频精品免费视频7| 一级毛片免费全部播放| 亚洲成AV人在线观看天堂无码| 久久国产精品2020免费m3u8| 亚洲综合成人网在线观看| 97免费人妻无码视频| 亚洲av纯肉无码精品动漫| 亚洲综合激情另类专区| 色猫咪免费人成网站在线观看| 亚洲第一网站免费视频| 性感美女视频在线观看免费精品 | ww在线观视频免费观看| 亚洲娇小性色xxxx| 全部免费国产潢色一级| a在线视频免费观看在线视频三区| 亚洲女同成av人片在线观看| 91成人在线免费视频| 亚洲色大成网站www永久男同| 全免费A级毛片免费看网站| 国产无限免费观看黄网站| 亚洲图片一区二区| 在线观看免费国产视频| 成全视成人免费观看在线看| 亚洲熟妇av一区二区三区下载| 日本免费的一级v一片| 十八禁视频在线观看免费无码无遮挡骂过 | 色天使色婷婷在线影院亚洲| 亚洲区小说区图片区QVOD| 美女被cao免费看在线看网站| 国产精品亚洲va在线观看|