JAVA servlets - open message popup

房东的猫 提交于 2019-12-30 10:30:32

问题


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?


回答1:


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.

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>");



回答2:


Add to HttpServletResponse some Javascript code that will open a popup, something like

<script type="text/javascript">
function popupWindow() {
    window.open( "someLinkToBePoppedUp" )
}
</script>



回答3:


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

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>");



回答4:


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 element with an alert statement in it.



来源:https://stackoverflow.com/questions/4064605/java-servlets-open-message-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!