Java request.getRequestDispatcher().forward(request, response) 在新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20019107/
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
request.getRequestDispatcher().forward(request, response) in a new page
提问by Martina
like the title say I would like to know if it's possible (and if it's possible, also how) to open a jsp page In a new TAB of the browser;
就像标题一样,我想知道是否有可能(如果可能,还有如何)在浏览器的新选项卡中打开一个 jsp 页面;
Now I'm using this istruction but the page will appear in the same tab of the browser.
现在我正在使用此结构,但该页面将出现在浏览器的同一选项卡中。
request.getRequestDispatcher("page.jsp").forward(request, response);
the call to the servlet, in the client side is this:
对 servlet 的调用,在客户端是这样的:
<img id="imgModAbb" src="imm/historyAbb.png"
title="Vedi storico modifiche"
onclick="window.location.href='/Spinning/InfoStoricoAbbonamento?id=<%=a.getIdAbbonamento()%>'">
SOLUTION
解决方案
I modified the calling in the client-side:
我修改了客户端的调用:
<a href="ServletAddress" target="_blank">
<img id="imgModAbb" src="imm/historyAbb.png" title="Vedi storico modifiche">
</a>
采纳答案by Debojit Saikia
That cannot be controlled in the server side. You need to control that in the client side. For example, you can use target="_blank"
on <form>
:
这在服务器端是无法控制的。您需要在客户端控制它。例如,您可以使用target="_blank"
on <form>
:
<form name="input" action="${toServlet}" method="POST" target="_blank">
...
</form>
回答by Martina
Yes it's possible, you should implement a servlet
是的,这是可能的,您应该实现一个 servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("page.jsp").forward(request, response);
}
回答by JB Nizet
Opening a URL in a new tab of the browser is something that must be done at client side (using target attributes of links, or JavaScript). The server doesn't know anything about tabs or even browsers. It just receives requests and sends back responses. And he couldn't care less if these requests come from one tab, 10 tabs, a bot, a wget command or whatever.
在浏览器的新选项卡中打开 URL 是必须在客户端完成的事情(使用链接的目标属性或 JavaScript)。服务器对选项卡甚至浏览器一无所知。它只是接收请求并发回响应。如果这些请求来自一个选项卡、10 个选项卡、一个机器人、一个 wget 命令或其他什么,他也毫不在意。
So no, you can't open a new browser tab from code running at server-side.
所以不,您不能从在服务器端运行的代码打开一个新的浏览器选项卡。