FileDocCategorySizeDatePackage
SaveState.javaAPI DocExample1679Tue Jan 25 10:45:14 GMT 2000None

SaveState.java

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SaveState extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    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();
    }
  }

  public String getServletInfo() {
    return "Calls the saveState() method (if it exists) for all the " +
           "currently loaded servlets";
  }
}