ajax 非同步 通訊 小例子 servlet與 jsp非同步

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

get  請求參數通過 url那裡寫進去,然後send(null)

html檔案和 servlet進行通訊 通過ajax 進行通訊

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>AJAX進行get方式的請求測試</title> 6 </head> 7 <body> 8     <input type="button" id="b1" name="b1" value="測試與伺服器的通訊"/> 9     <div id="d1">10                 11     </div>12     <script type="text/javascript">13         14         window.onload=function(){//當頁面被全部加在完畢後再執行15             //給b1按鈕註冊事件16             document.getElementById("b1").onclick=function(){17                 //AJAX代碼18                 19                 //得到XmlHttpRequest對象20                 var xhr = createXmlHttpRequest();21                 //xhr的readyState改變都會觸發onreadystatechange事件22                     23                     /*24                      * readyState的取值:25                      *     0 (未初始化) 對象已建立,但是尚未初始化(尚未調用open方法) 26                      *     1 (初始化) 對象已建立,尚未調用send方法 27                      *  2 (發送資料) send方法已調用,但是當前的狀態及http頭未知 28                      *     3 (資料傳送中) 已接收部分資料,因為響應及http頭不全, 29                      *     4 (完成) 資料接收完畢,此時可以通過通過responseBody和responseText擷取完整的回應資料 30 31                      */32                 xhr.onreadystatechange=function(){33                     if(xhr.readyState==4){34                         //真正的處理35                         if(xhr.status==200||xhr.status==304){36                             //伺服器正確返回37                             var data = xhr.responseText;//伺服器返回的資料38                             //把返回的資料寫到div中39                             document.getElementById("d1").innerHTML=data;40                         }41                     }42                 }43                 //建立與伺服器的串連44                     /*45                      * oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);46                      * bstrMethod:請求方式。一般使用get或post47                      * bstrUrl:請求的資源地址:可以絕對或相對路徑48                      * varAsync:是否是非同步請求。預設是true。49                      */            //由於瀏覽器請求url如果都是一樣的話,就不請求了,所以後面加個時間資訊,讓url不同,50                 xhr.open("GET","/ajaxday02/servlet/ServletDemo1?time="+new Date().getTime());51                 //發送資料52                     //oXMLHttpRequest.send(varBody);  varBody:請求參數53                 xhr.send(null);54 55                 //接收伺服器返回的資料56                     57                 58             }59         }60         function createXmlHttpRequest(){61            var xmlHttp;62            try{    //Firefox, Opera 8.0+, Safari63                    xmlHttp=new XMLHttpRequest();64             }catch (e){65                    try{    //Internet Explorer66                           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");67                     }catch (e){68                           try{69                                   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");70                           }catch (e){}  71                    }72             }73            return xmlHttp;74          }75 76     </script>77 </body>78 </html>

servlet

 1 package cn.itcast.controller; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest;10 import javax.servlet.http.HttpServletResponse;11 12 @WebServlet("/servlet/ServletDemo1")13 public class ServletDemo1 extends HttpServlet {14     private static final long serialVersionUID = 1L;15        16    17     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {18         response.setContentType("text/html;charset=UTF-8");19         PrintWriter out = response.getWriter();20         out.write("hello ajax");21         System.out.println("ServletDemo1執行了");22     }23 24     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {25         doGet(request,response);26     }27 28 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.