This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Unwanted browser window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Unwanted browser window" Watch "Unwanted browser window" New topic
Author

Unwanted browser window

stuart armstrong
Greenhorn

Joined: May 15, 2006
Posts: 6
I am using the following code inside a Servlet to allow users to click on a button to download (open/save) an XML file - This works fine. However when the user clicks the download button a new browser window is opened. Is there anyway using Servlets to close this window? I thought about changing the content type then writing out some JavaScript to close the browser, however I don�t think this is possible.

Any help would be greatly appreciated

response.setContentType( "text/xml" );
response.setHeader( "Content Disposition", "attachment;filename=downloadXML.xml" );

//send the data
response.getOutputStream().println( xmlDataString );
response.getOutputStream().flush();
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Can you post the HTML for the button that the user clicks?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
stuart armstrong
Greenhorn

Joined: May 15, 2006
Posts: 6
<a href='/app/downloadReports.do?ctrl=publishedReportList&action=DownloadXml¶m=0' target='_blank' title='Download Report XML'>
<img vspace='0' align='absmiddle' border='0' src='images/auditlogicon/contract.gif'>
</a>
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
Get rid of target="_blank", that is what's forcing a new window to open.
stuart armstrong
Greenhorn

Joined: May 15, 2006
Posts: 6
If I don't open a new window how would I get the servlet to redirect to the screen which has the button that the user has just clicked on?
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
I guess you have a few options;

download page being the jsp/servlet that generates the download
source page being the page with the download link/button on

- add a link on the download page to return to the source page
- have the source page and download page as the same page (when you click the download link you remain on the same page)

IMO, what you shouldn't be doing is trying to control the user's browser by closing a window.
 
 
subject: Unwanted browser window