res.setContentType("text/plain");
PrintWriter out = res.getWriter();
ServletContext context = getServletContext();
Enumeration names = context.getServletNames();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
Servlet servlet = context.getServlet(name);
out.println("Trying to save the state of " + name + "...");
out.flush();
try {
Method save = servlet.getClass().getMethod("saveState", null);
save.invoke(servlet, null);
out.println("Saved!");
}
catch (NoSuchMethodException e) {
out.println("Not saved. This servlet has no saveState() method.");
}
catch (SecurityException e) {
out.println("Not saved. SecurityException: " + e.getMessage());
}
catch (InvocationTargetException e) {
out.print("Not saved. The saveState() method threw an exception: ");
Throwable t = e.getTargetException();
out.println(t.getClass().getName() + ": " + t.getMessage());
}
catch (Exception e) {
out.println("Not saved. " + e.getClass().getName() + ": " +
e.getMessage());
}
out.println();
}