2013-03-19 71 views
0

美好的一天,我正在處理一個Servlet,它必須返回一個PDF文件以及用該文件完成的處理的消息日誌。使用Servlet返回多個項目

到目前爲止,我傳遞一個布爾值,我會計算並返回要麼日誌或文件,這取決於用戶的選擇,如下所示:

  //If user Checked the Download PDF 
     if (isDownload) { 
      byte[] oContent = lel; 
      response.setContentType("application/pdf"); 
      response.addHeader("Content-disposition", "attachment;filename=test.pdf"); 
      out = response.getOutputStream(); 
      out.write(oContent); 
     } //If user Unchecked Download PDF and only wants to see logs 
     else { 
      System.out.println("idCompany: "+company); 
      System.out.println("code: "+code); 
      System.out.println("date: "+dateValid); 
      System.out.println("account: "+acct); 
      System.out.println("documentType: "+type); 

      String result = readFile("/home/gianksp/Desktop/Documentos/Logs/log.txt"); 
      System.setOut(System.out); 

      // Get the printwriter object from response to write the required json object to the output stream  
      PrintWriter outl = response.getWriter(); 
      // Assuming your json object is **jsonObject**, perform the following, it will return your json object 
      outl.print(result); 
      outl.flush(); 
     } 

是否有在返回這兩個項目的有效方式同一時間?

非常感謝您

回答

0

你可以在DTO對象包裝它們或將它們放在會話從JSP參考。

1

HTTP協議不允許您爲每個HTTP請求發送多個HTTP響應。通過指定onclick事件處理

  1. 讓客戶火兩個HTTP請求,例如,或者,如果你在第一個響應返回的HTML頁面,你可以發射另:由於此限制記住,你能想到以下的替代品請求window.loadpage.ready;
  2. 爲您提供選擇他想要下載的機會,並相應地在servlet中運行:如果他選擇PDF - 返回PDF;如果他選擇了文本 - 返回文本,並且如果他選擇了兩個 - 將它們打包歸檔並返回。

請注意,第一個變體既笨拙又不便於用戶使用,而且就我而言,應該不惜一切代價避免使用。用戶控制他所得到的頁面是一個更好的選擇。