Java/Servlets/Error Exceptions

Материал из Java эксперт
Перейти к: навигация, поиск

Error code

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Sender extends HttpServlet {
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    /*
     * if the servlet tries to access a resource and finds out that the
     * client is not authorized to access it - "401 Unauthorized"
     */
    //response.sendError(401,"You are not authorized to view the requested
    // component");
    /*
     * if the servlet tries to access a resource that is forbidden for this
     * client and there is no further information on it - "403 Forbidden"
     */
    response.sendError(403);
    /*
     * if the servlet tries to access a resource that is not found given the
     * client"s provided URL - "404 Not Found"
     */
    //response.sendError(404);
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    doPost(request, response);
  }
}





Generate errors

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ErrorGen extends HttpServlet {
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //check the servlet exception
    Throwable throwable = (Throwable) request
        .getAttribute("javax.servlet.error.exception");
    String servletName = (String) request
        .getAttribute("javax.servlet.error.servlet_name");
    if (servletName == null)
      servletName = "Unknown";
    String requestUri = (String) request
        .getAttribute("javax.servlet.error.request_uri");
    if (requestUri == null)
      requestUri = "Unknown";
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Error page</title>");
    out.println("</head>");
    out.println("<body>");
    if (throwable == null) {
      out.println("<h2>The error information is not available</h2>");
      out.println("Please return to the ");
    } else {
      out.println("<h2>Here is the error information</h2>");
      out
          .println("The servlet name associated with throwing the exception: "
              + servletName + "<br><br>");
      out.println("The type of exception: "
          + throwable.getClass().getName() + "<br><br>");
      out.println("The request URI: " + requestUri + "<br><br>");
      out.println("The exception message: " + throwable.getMessage());
    }
    out.println("</body>");
    out.println("</html>");
    out.close();
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    doPost(request, response);
  }
}





Servlet throws Exceptions

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Thrower extends HttpServlet {
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    throw new java.io.IOException("IO thrown");
    //response.sendError(403);
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    doPost(request, response);
  }
}