2011-09-15 26 views
1

我需要從jsp文件打開一個新窗口。在一個JSP文件中,我接受來自用戶的輸入,然後將其作爲參數發送給一個java方法,該方法以字符串形式返回url。然後我需要在新窗口中打開它。我怎麼做?以下是jsp文件(send_product.jsp)供您參考。如何從JSP文件打開新窗口?

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ page import="products.*" %> 
<!DOCTYPE html> 
<% 
    productManager pm= new productManagerImpl(); 
    String myUrl= null; 


    if (request.getParameter("product_id") != null) { 

     // get the paramter 
     String product_id = request.getParameter("product_id"); 

     try { 

      //myUrl has the url. Have to open this in a new window 
      myUrl = pm.getUrl(product_id); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 

%> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Insert</title> 
    </head> 
    <body bgcolor="wheat"> 
     <h4 align="right">Send Product</h4> 
     <hr color="red"/> 
     <br/><br/> 
     <a href="index.jsp">Index</a> 
     <form action="send_product.jsp" method="post"> 
      <% if (myUrl != null && !myUrl.equals("")) {%> 
      <%= myUrl%> 
      <p>&nbsp;</p> 

      <% 
       } 
      %> 

      <table width="75%" align="center" > 
       <tr> 
        <td> 
         Please enter the information below and click "<b>Submit</b>" 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <fieldset title="info"> 
          <table border="0" cellspacing="3" cellpadding="0" align="center"> 
           <tr> 
            <td align="right"> 
             * Product ID: 
            </td> 
            <td align="left"> 
             <input type="text" size="20" maxlength="70" name="product_id"> 
            </td> 
           </tr> 

          </table> 
          <hr /> 

          <div align="center"> 
           <input type="submit" name="saveInfo" value="Submit"> 
          </div> 
         </fieldset> 
        </td> 
       </tr> 
      </table> 
     </form> 
    </body> 
</html> 

回答

3

你需要讓你的JSP打印的JavaScript以下行每當URL已收到:

<script>window.open('<%= myUrl%>', 'YOUR_WINDOW_NAME');</script> 

這樣JS會打開一個新窗口/標籤。


無關的具體問題,把Java代碼中的JSP一樣,是一個poor practice。被警告!

+0

但我想打開字符串myUrl中的url(該字符串捕獲方法getUrl的返回值)。假設該方法返回www.google.com,我想在新窗口中打開google.com。 – Ravi

+0

哦,你正在處理表單提交內部相同的JSP?我會更新答案。 – BalusC

+0

是的,我是。我知道這不是最好的辦法。我也成功回覆了回覆。我不知道如何在新窗口中打開字符串中的網址 – Ravi