/webportal/src/main/java/au/org/emii/portal/servlet/ErrorHandlerServlet.java

http://alageospatialportal.googlecode.com/ · Java · 68 lines · 33 code · 13 blank · 22 comment · 0 complexity · f051c7cdec0fa8fbd473dc3e899f421d MD5 · raw file

  1. package au.org.emii.portal.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.RequestDispatcher;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. /**
  10. * Servlet implementation class ErrorHandlerServlet
  11. */
  12. public class ErrorHandlerServlet extends HttpServlet {
  13. private static final long serialVersionUID = 1L;
  14. private static final String ERROR_PAGE = "/WEB-INF/jsp/Error.jsp";
  15. /**
  16. * @see HttpServlet#HttpServlet()
  17. */
  18. public ErrorHandlerServlet() {
  19. super();
  20. }
  21. /**
  22. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  23. */
  24. @Override
  25. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. // get the error fields from the request - these should all be null if we've
  27. // already handled the error elsewhere
  28. Object statusCode = request.getAttribute("javax.servlet.error.status_code");
  29. Object message = request.getAttribute("javax.servlet.error.message");
  30. //Object errorType = request.getAttribute("javax.servlet.error.exception_type");
  31. Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");
  32. Object request_uri = request.getAttribute("javax.servlet.error.request_uri");
  33. // Exceptions are normally raised and logged during application or session init
  34. // if something has slipped through the net, log it, otherwise just display the
  35. // jsp error page
  36. Logger logger = Logger.getLogger(this.getClass());
  37. logger.fatal(
  38. "UNHANDLED EXCEPTION: HTTP ERROR " + statusCode + " Message: " + message +
  39. " URI: " + request_uri + " REASON " +
  40. exception.getMessage());
  41. // now we just display a static-ish JSP to the user - we don't use a
  42. // ZK page because it could well be problems with ZK that have sent
  43. // us here in the first place which would leave us unable to display
  44. // the error message
  45. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(ERROR_PAGE);
  46. dispatcher.forward(request, response);
  47. }
  48. /**
  49. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  50. */
  51. @Override
  52. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  53. doGet(request, response);
  54. }
  55. }