2016-11-06 56 views
1

我的JSP頁面onDownload函數調用一個具有以下功能的servlet。我想在單擊下載按鈕時顯示警告「Please Wait」。處理完成時在servlet上,關閉警報。單擊按鈕時,在JSP上顯示'Please wait'警報。當在servlet上完成處理時,請移除警報

JSP代碼:

function onDownload(){ 
     var dType = 'download'; 
     var url = "<%=strDownloadURLTest%>"+"/downloadservlet?downloadType="+dType+"&PNumber="+'<%=custPNo%>'; 
     document.getElementById('downloadP').href = url; 
     document.getElementById('downloadP').target='_blank'; 
    } 

,處理在Servlet的一側,代碼:

 byte content[]=null; 
    try 
    { GetPDAO getPDAO= new GetPDAO(); 

      for(int i=0;i<5;i++) 
      { 
       content=getPDAO.getPFromEb(strPN); 
       DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); 
       Date dateobj = new Date(); 
      content=null; 
      if(content==null) 

       { 
       Thread.sleep(5000); 

      } 
      else { 
       content=getPDAO.getPFromEb(strPN); 
       break; 
      } 
      } 

回答

1

最後我同意使用AJAX(以後我不覺得輪其他方式),以得到迴應從下面的servlet。不過,我第一次使用AJAX。我在JSP中添加了下面的代碼,並且servlet代碼與之前保持相同。請建議如果它正確完成?

$.ajax({ 
    type: 'post', 
    url: '"<%=strDownloadURLTest%>"+"/downloaddocservlet?downloadType="+dType+"&PNumber="+"<%=custPNo%>"', 
    data: { 
     name: $('#dType').val() 
    }, 
    beforeSend:function(){ 
    alert("Please wait....."); 
     // this is where we append a loading image 
    }, 
    success:function(responseText){ 
    alert.close(); 
     // successful request; do something with the data    
    }, 
    error:function(){ 
     // failed request; give feedback to user 
    } 
});