Have the servlet automatically close the web browser that was started to run it

At the end of the servlet's doGet method, after the work has been done, the servlet needs to shut itself down.

You probably want the servlet to give up its resources, use: destroy(); session.invalidate();

The following code originated from: http://www.sitepoint.com/forums/printthread.php?t=319739

public void doGet(HttpServletRequest hsrq, HttpServletResponse hsrs) throws ServletException, IOException {
.
.
work
.
.
Write to the browser what is going on in case there's a problem.

String Text1 = "Servlet servSomething is shutting itself down.";
String Text2 = "Look in Tomcat's catalina.out log for information.";

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

// Flush to browser
hsrs.setContentType("text/plain");
try {
ServletOutputStream os = hsrs.getOutputStream();
os.write(Text1.getBytes());
os.write(Text2.getBytes());
os.write(javaScript.getBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println("IOException " + .e.toString());
} catch (Exception e) {
System.out.println("Exception " + e.toString());
}

Thanks

Very interesting information. Thanks to the author.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.