JSP: How to close browser window after flushing a file to the browser?

I have this simple JSP, which flushes a String to the browser as a file (message.txt). The JSP gets opened in a new window when the user clicks a link.

The problem is that after the JSP has run, the browser window won’t close automatically. I’ve tried adding a small javascript to achieve this, but i doesn’t seem to even get run.

How can I close the browser window after the JSP has run?


<%
 String someText = "bla bla bla";
 
 // Flush to browser 
 response.setContentType("text/plain");
 response.setHeader("Content-Disposition", "attachment;filename=message.txt");

 try {
  ServletOutputStream os = response.getOutputStream();
  os.write(someText.getBytes());
  os.flush();
  os.close();
 } catch (IOException e) {
  return;
 } catch (Exception e) {
  return;
 }

%>

<script type="text/javascript">
 window.close();
</script>

After the JSP has run, an empty html page with the following source can be seen:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>


 os.write(someText.getBytes());
os.write("<script type=\\"text/javascript\\"> window.close();</script>");
  os.flush();
  os.close();
 } catch (IOException e) {
  return;
 } catch (Exception e) {
  return;
 }

%>

  • the problem was that you closed the output stream before finishing writing to it this way the javascript will never appear on the page.

That doesn’t seem to work.

I have to do it like like this to be able to write it to os:


<%
 String someText = "bla bla bla";
 String javaScript = "<script type=\\"text/javascript\\">window.close();</script>";
 
 // Flush to browser 
 response.setContentType("text/plain");
 response.setHeader("Content-Disposition", "attachment;filename=message.txt");
 try {
  ServletOutputStream os = response.getOutputStream();
  os.write(someText.getBytes());
  os.write(javaScript.getBytes());
  os.flush();
  os.close();
 } catch (IOException e) {
  return;
 } catch (Exception e) {
  return;
 }
%>

The result of this, is that the javascript will be added to the end of the file that get saved. It will not be included in the html page itself. I want the opposite.

Any other suggestions?

Oh I see the problem. I dont’t think you can resolve that easily. You could try to redirect to a page with the window.close() ->
response.sendRedirect(“clospage.html”);
at the very beginning.

I think the problem may be you are sending plain text out to the browser:

response.setContentType(“text/plain”);

but you are expecting it to function like html (using the javascript etc)… why exactly are you sending plain text back out?

I’m setting the plain text content type to make sure the browser downloads the file as plain text.

It looks like that after I’ve closed the outputStream I can’t do any more processing in the JSP. Even if I remove the response.setContentType and response.setHeader methods it still behaves this way.

I’m starting to think that there’s no way around this…??