2011-09-15 5 views
1

jsp 파일에서 새 창을 열어야합니다. JSP 파일에서 사용자의 입력을 받아서 URL을 문자열로 반환하는 Java 메소드에 인수로 전송합니다. 그런 다음이 창을 새 창에서 열어야합니다. 어떻게해야합니까? 아래는 jsp 파일 (send_product.jsp)입니다.JSP 파일에서 새 창을 여는 방법은 무엇입니까?

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

이 방법 JS 새 창/탭을 열 것입니다 :

<%@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

당신은 URL를받은 때마다 JSP는 자바 스크립트의 다음 줄을 인쇄 할 수 있도록해야합니다. 그런 JSP 안에 자바 코드를 넣어 콘크리트 질문에


관련없는

poor practice입니다. 경고 받다!

+0

하지만 문자열 myUrl에있는 URL을 열려고합니다 (이 문자열은 getUrl 메서드의 반환 값을 캐치합니다). 이 메소드가 www.google.com을 반환한다고 가정하면 google.com을 새 창에서 열려고합니다. – Ravi

+0

오, 같은 JSP에서 양식 제출을 처리하고 있습니까?! 나는 대답을 업데이트 할 것이다. – BalusC

+0

예. 나는 그것이 최선의 방법이 아니라는 것을 알고 있습니다. 나는 또한 응답을 성공적으로 다시 얻고있다. 새 창에서 문자열의 URL을 여는 방법을 모르겠다. – Ravi

관련 문제