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

标签 javascript html jsp

我需要从 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>

最佳答案

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

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

这样JS会打开一个新的窗口/标签。


与具体问题无关,像这样将 Java 代码放入 JSP 是一个 poor practice .警告!

关于javascript - 如何从 JSP 文件打开一个新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7436918/

相关文章:

Javascript - 嵌套的父/子数组递归函数,仅展平父级

javascript - 在窗口滚动时更改导航事件类

html - 有没有办法让我的网页始终有一个垂直滚动条?

php - 检查用户名和密码后重定向 ajax 调用

java - 将数据从 JSP 动态地从 HTML 页面传递到 Servlet

java - 检查 JSP 中的数据库连接

javascript - WKWebView - 防止用户文本选择触发的自动滚动

javascript - 获取所选内容(包括所选内容)的 HTML

javascript - 从基于属性模式的对象中获取值?

java - 为什么我的jsp不显示输出?