2014-09-01 98 views
0

嗨我想點擊打開多個PDF文件,我正在嘗試這種方式。在單獨的標籤中打開多個pdf文件

考慮到用戶將通過選擇多個單選按鈕來選擇多個選項來獲得pdf文件。

<html> 
<head> 
    <title>sendRedirect Program using Servlet</title> 
</head> 
<body> 
    <form name="pdf" target="_blank" method="get" action="http://localhost:8080/SendRedirectProg/sr"> 
    <table border="1" width="500"> 
    <tr> 
    <td>Select PDF:</td> 
    <td><INPUT type="radio" name="pdf" size="20" value="PDF 1">PDF 1 
      <INPUT type="radio" name="pdf" size="20" value="PDF 2">PDF 2 
      <INPUT type="radio" name="pdf" size="20" value="PDF 3">PDF 3</td> 
    </tr> 
    <tr> 
    <td><INPUT type="submit" size="20"></td> 
    </tr> 
    </form> 
    </table> 
</body> </html> 

因此,考慮一下我會選擇所有3個單選按鈕,並且我想在不同的選項卡中打開所有這些PDF文件。

我正在嘗試這種方式。

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class SendRedirectProg extends HttpServlet 
{ 
    public void method(ActionRequest req, ActionResponse res) throws 
{ 
    PrintWriter p=res.getWriter(); 
    res.setContentType("text/plain"); 
    String s=req.getParameter("pdf"); 
    if(s.equals("PDF 1") && s.equals("PDF 2") && s.equals("PDF 3")) 
    { 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF1.pdf"); 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF2.pdf"); 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoMCA 3.pdf"); 
    } 
} 

但它只顯示最後一個,我理解使用res.sendRedirect,但我們仍然可以如何實現這一點?

有什麼建議嗎?

回答

1

您無法在一次重定向中打開3個選項卡。你可以做的是使用jQuery或JavaScript

重定向到一個頁面,你寫的URL的3周不同的隱藏的div或生成腳本的頁面直接

的onload,使用JavaScript來打開它們。

window.open(url1,'_blank'); 
window.open(url2,'_blank'); 
window.open(url3,'_blank'); 
0

target="_blank"是你的答案;)

<form name="pdf" method="get" target="_blank" action="http://localhost:8080/SendRedirectProg/sr"> 

的更多信息:http://www.w3schools.com/tags/att_form_target.asp

還有在Java代碼中的問題,您應該只使用一個重定向,相應地傳遞價值。

if(s.equals("PDF 1")) 
{ 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF1.pdf"); 
} 

if(s.equals("PDF 2")) 
{ 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF2.pdf"); 
} 

if(s.equals("PDF 3")) 
{ 
    res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoMCA 3.pdf"); 
} 
+0

嘿@Skriptotajs,我已經用它,但它只是打開了一個新的tab.So最後一個(PDF),我需要從後端側導 – Java 2014-09-01 07:50:49

+0

對不起,我沒有在Java代碼中,我注意到錯誤更新了我的答案。 – Skriptotajs 2014-09-01 07:56:40

+0

感謝您的及時回覆。但根據您的實現,它也只會打開最後一個pdf,即PDF3.這是sendRedirect的行爲。 – Java 2014-09-01 08:04:12

相關問題