How to Throw Exceptions in Servlets
If a servlet encounters an unhandled problem, it can throw a ServletException (for expected problems, such as an errornous user input, you should show a error message to the user). Alternatively, you can also throw a RuntimeException. On both types of exceptions the container will return the HTTP status code 500 (internal error).
A special kind of ServletException is the UnavailableException, which tells the server that the servlet is currently or permanently unavailable. You can either specify the number of seconds the application will be unavailable, causing the server to respond with a HTTP status code 503 (service unavailable) during that time. Or you create the exception without a duration, and the server will remove the servlet and start returning HTTP status code 404 (not found).
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
if (permanentlyUnavailable)
throw new UnavailableException("Servlet is gone");
if (temporarilyUnavailable)
throw new UnavailableException("Try again later..", 60); // unavailable for 60s
}

