Sneha Goswami
Sneha Goswami

Reputation: 85

Close BufferedOutputStream output after 30 seconds if download fails

On clicking Download, my code starts writing content from SAN location in a new opened tab window and once complete, downloads PDF to local system. I would like to close the new tab window if the file does not get writes/downloaded atmost in 30 seconds. and then display a text message on the parent window JSP. My servlet code as below:

    try
    {   
        GetPDAO getPDAO= new GetPDAO();

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

                 content=getPDAO.getPFromE( strPN);
                 break;
             }
          }
        response.setContentType("application/pdf");
        String strFileName=strPN+".pdf";
        response.setHeader("Content-Disposition","attachment; filename=\"" + strFileName + "\"");
        response.setHeader("Cache-Control","no-cache"); 
        response.setHeader("Cache-Control","no-store"); 
        response.setHeader("Pragma","no-cache");
        response.setDateHeader("Expires", 0);
        output =new BufferedOutputStream(response.getOutputStream());
        output.write(content); 
        output.flush();          
        } 
    finally 
     {
        output.close(); 
      }

and my JSP code as below:

function onSubmit()
{    
var url="<%=strCCPURL%>"+"/getpfromeservlet?PN="+document.getElementById("pn").value; 
       document.getElementById('alrt').innerHTML='<b>Please wait</b>'; 
           setTimeout(function() {document.getElementById('alrt').innerHTML='';},8000);
            window.open(url,"_blank");
           document.getElementById('viewPD').href=url;
           document.getElementById('viewPD').target='_blank';            
}

Upvotes: 0

Views: 206

Answers (1)

Siddharth Sid
Siddharth Sid

Reputation: 38

You may use sendRedirect from your servlet to replica of your JSP and then auto close the JSP. sendRedirect will close the servlet and then replica JSP will get closed via window.close method. Focus is back to your main JSP.

Upvotes: 2

Related Questions