开发者

JAVA servlets - open message popup

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?


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


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

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜