aspose file tools*
The moose likes Servlets and the fly likes to open new window from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "to open new window from a servlet" Watch "to open new window from a servlet" New topic
Author

to open new window from a servlet

manisha Gupta Garg
Ranch Hand

Joined: Jul 03, 2009
Posts: 41
hi,

is it possible to open a new window from a servlet?

Actually i want to show a message "please wait..." in new window while servlet is doing some processing !
I have the code to display the "please wait message..." on the screen it self ...but the problem is that, my servlet is used for reading a file(doc,pdf,xls,text,html) from server. so if i display the message in servlet page itself like below -




so if i used the above code, the message gets displayed on the page. but the window should get closed automatically when the xls/doc file is open.

Please help !!
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
After the response text is sent back to the client, there's nothing you can do with it. So basically, once you close the PrintWriter instance, the response body is sent back to the client while you keep working with the OutputStream associated with the response. When you close the stream, you can't do anything about the page's display content.
Personally I don't think it's wise to work on both the PrintWriter and OutputStream associated with a HTTPServletResponse instance. More often than not, one gets to bump into an IllegalStateException.

What you should probably try to do is - when you send the control to this servlet, display the message on the screen at that point while you submit the request to this particular servlet. For eg - if you come here from a JSP, in your JSP you can write a small JavaScript snippet which directs the user's browser to a static HTML using something like - Thereafter, you can simply submit the JSP's form object. [your form object's action attribute should map to this servlet].
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11478
    
  94

Interesting solution Arnivan, however I am not sure about the implementation of it - when you reset the location.href are you appending all the form values and the submitting the form from the new page? Presumably with the entire form being hidden on the new page.

Personally I would submit the form as normal, and have the receiving servlet pass off the majority of the work to a pool of worker threads. The servlet is then free to return almost instantly to a screen that says "please wait". This new screen would then spawn an Ajax request to get the real response from the server once the worker thread completed.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Yes Andrew, it works although it sounds a bit strange. location.href simply changes the display page and doesn't submit the form. A snippet like this -
will display the HTML page while submitting the form to the servlet. When the servlet sends back its response, the browser will refresh with the servlet's content. I think the only limiting factor about this is that the time taken by the servlet to send back the response > time taken for moving to the HTML page [which usually is the case].
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11478
    
  94



Thanks for explaining.
manisha Gupta Garg
Ranch Hand

Joined: Jul 03, 2009
Posts: 41
Thanks Arivan and Andrew, for your interest!!

but my problem is xls,pdf reports have to be open in their native application and our code and property of IE does this. so if they open in microsoft applications and if we have applied something like the approaches i mentioend in my post and the approaches that you folks are suggesting then the jsp which has opened the servlet to read the file, would remain open as it is and we want to close the jsp/servlet page when the report is openned.

Please help!

Thanks,
Manisha
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11478
    
  94

The same basic approaches can be used.

You could have a secondary servlet tracking whether the report has been sent or not

Use Anirvan's suggestion to get the temporary page displaying, and have that page generate an Ajax request every 'x' seconds to the secondary servlet to check the status - if the report has been sent then the tab can close itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to open new window from a servlet