JAVA servlets - 打开消息弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4064605/
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
JAVA servlets - open message popup
提问by Erik Sapir
I want to user HttpServletResponse object to compose a response that will tell the browser client to open a popup with some message - how can i do that?
我想使用 HttpServletResponse 对象来编写一个响应,该响应将告诉浏览器客户端打开一个带有一些消息的弹出窗口 - 我该怎么做?
采纳答案by Vuk
Every Servlet response is basically an Http doc/snippet. So you could return a call to a javascript function that will be executed on the client side. The function can be passed in that Servlet response or it can be pre-included in the .js file.
每个 Servlet 响应基本上都是一个 Http doc/snippet。因此,您可以返回对将在客户端执行的 javascript 函数的调用。该函数可以在该 Servlet 响应中传递,也可以预先包含在 .js 文件中。
just an example:
只是一个例子:
//servlet code
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<script type=\"text/javascript\">");
out.println("alert('deadbeef');");
out.println("</script>");
回答by darioo
Add to HttpServletResponse some Javascript code that will open a popup, something like
向 HttpServletResponse 添加一些将打开弹出窗口的 Javascript 代码,例如
<script type="text/javascript">
function popupWindow() {
window.open( "someLinkToBePoppedUp" )
}
</script>
回答by Quentin
Generally speaking, you can't.
一般来说,你不能。
Thanks to their popularity for annoying adverts, most browsers reject attempts to open popups that aren't a response to something the user does within a page.
由于它们因烦人的广告而广受欢迎,因此大多数浏览器拒绝尝试打开不是对用户在页面内所做的事情的响应的弹出窗口。
If you just want to display messaging, you could just include it in a page, or output a script elementwith an alert statementin it.
回答by Fazi
Basically, you cannot do that directly. You must send in response some code (probably HTML and JS) which will instruct client browser to show message window, eg
基本上,你不能直接这样做。您必须在响应中发送一些代码(可能是 HTML 和 JS)来指示客户端浏览器显示消息窗口,例如
String someMessage = "Error !";
PrintWriter out = response.getWriter();
out.print("<html><head>");
out.print("<script type=\"text/javascript\">alert(" + someMessage + ");</script>");
out.print("</head><body></body></html>");