javascript 如何从 JSP 文件打开一个新窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7436918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:07:41  来源:igfitidea点击:

how to open a new window from a JSP file?

javascripthtmljsp

提问by Ravi

I need to open a new window from a jsp file. In a JSP file, I take in an input from the user and then send it as an argument to a java method which returns me back the url as a string. I then need to open this in a new window. How do I do that? Below is the jsp file(send_product.jsp) for your reference.

我需要从一个 jsp 文件打开一个新窗口。在 JSP 文件中,我接受来自用户的输入,然后将其作为参数发送给 java 方法,该方法将 url 作为字符串返回给我。然后我需要在新窗口中打开它。我怎么做?下面是jsp文件(send_product.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>

回答by BalusC

You need to let your JSP print the following line of JavaScript whenever the URL has been received:

每当收到 URL 时,您都需要让 JSP 打印以下 JavaScript 行:

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

This way JS will open a new window/tab.

这样 JS 将打开一个新窗口/选项卡。



Unrelatedto the concrete question, putting Java code inside JSP like that is a poor practice. Be warned!

具体问题无关,像这样将 Java 代码放在 JSP 中是一种糟糕的做法。被警告!