views:

39

answers:

4

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?

A: 

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

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

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.

David Dorward
+1  A: 

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>");
Fazi
+2  A: 

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>");
Vuk
There is a problem - the original page becomes blank - i want the content to stay and the message to just pop up
Erik Sapir
Oh, well then you're looking for some Ajax-like functionality. Just send an async request to the servlet. Check out this example - http://www.hiteshagrawal.com/ajax/ajax-programming-with-jsp-and-servlets
Vuk