<%-- /* * @(#)CustomErrorPage.jsp (Metawerx customer example classes) * * Copyright (c) 1998-2007 Metawerx. All Rights Reserved. * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * * Synopsis * ===================================================================================== * This code demonstrates a custom error page that can be used to handle anything from * 404 errors to Java exceptions. * * This page includes examples on: * - display of your own (branded) error page in JSP * - accessing the ErrorData object provided by Tomcat * - handling common problems such as PageContext being null * - working out if a 404 error is from the current website or an external website * - custom error message logic * * To instruct Tomcat to use this JSP file instead of the default Tomcat error page, * add the following to your web.xml file. <!-- =========================================================== Error Handler This forwards all errors to CustomErrorPage.jsp Note that CustomErrorPage.jsp MUST NOT throw an error itself, or it causes a second call and is hard to debug. Comment this section out when testing changes to your error page. ============================================================ --> <error-page> <error-code>401</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>403</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>404</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>500</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>503</error-code><location>/CustomErrorPage.jsp</location> </error-page> * * Complete file structure for Tomcat for this sample * ===================================================================================== JAKARTA-WEBAPPS JAKARTA-WEBAPPS/CustomErrorApp JAKARTA-WEBAPPS/CustomErrorApp/CustomErrorPage.jsp JAKARTA-WEBAPPS/CustomErrorApp/WEB-INF JAKARTA-WEBAPPS/CustomErrorApp/WEB-INF/web.xml * * Complete, minimal web.xml file for this sample * ===================================================================================== <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Custom Error Test App</display-name> <description>Custom Error Test Application</description> <!-- =========================================================== Error Handler This forwards all errors to CustomErrorPage.jsp Note that CustomErrorPage.jsp MUST NOT throw an error itself, or it causes a second call and is hard to debug. Comment this section out when testing changes to your error page. ============================================================ --> <error-page> <error-code>401</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>403</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>404</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>500</error-code><location>/CustomErrorPage.jsp</location> </error-page> <error-page> <error-code>503</error-code><location>/CustomErrorPage.jsp</location> </error-page> </web-app> * History * ===================================================================================== * 2006/04/14 17:43 Neale Rudd * Added this example * */ --%> <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %> <%@ page isErrorPage="true" %> <!-- Unless this text is here, if your page is less than 513 bytes, Internet Explorer will display it's "Friendly Error Page", and your custom error will never be displayed. This text is just used as filler. This is a useless buffer to fill the page to 512 bytes to avoid display of Friendly Error Pages in Internet Explorer This is a useless buffer to fill the page to 512 bytes to avoid display of Friendly Error Pages in Internet Explorer This is a useless buffer to fill the page to 512 bytes to avoid display of Friendly Error Pages in Internet Explorer --> <% try { String errorMessage = "[The Custom Error Page was accessed directly]"; // Make sure we have a pageContext. if(pageContext != null) { // Get error data ErrorData ed = null; try { ed = pageContext.getErrorData(); } catch(NullPointerException ne) { // Sometimes this call causes a NullPointerException (PageContext.java:514) // Catch and ignore it.. it effectively means we can't use the ErrorData // Times this happens: // - error.jsp is accessed directly } // Prepare error report if(ed != null) { String url = ed.getRequestURI(); int errorCode = ed.getStatusCode(); Throwable e = ed.getThrowable(); if(e != null) { // Handle JSP exceptions differently, show the lines in a <pre> tag if(e.getMessage() != null && e.getMessage().indexOf("Exception in JSP") != -1) errorMessage = "An error occurred in a JSP file ...\n\n<pre>" + e.getMessage() + "</pre>"; else errorMessage = e.getMessage(); } else { // HTTP Error (404 or similar) String message = errorCode + " - Page not found: "+url; // If referer available, report it if(errorCode == 404) { if(request.getHeader("Referer") != null) { // Check if the referer contains our server name if(request.getHeader("Referer").indexOf(request.getServerName()) != -1) { // Broken link on this site message += "<br />You have followed a broken link on our website.<br />We apologise for the inconvenience."; } else { // Broken link on another site (ask user to contact admin of that site) message += "<br />You have followed a broken link on another website.<br />Please contact the administrator of that site, and let them know the link is not correct."; } } } errorMessage = message; } } } // A suitable error message is now stored in the errorMessage string variable. // You can send this to your support team by email using JavaMail or other similar tools. // Show the error message, with some HTML around it // This is the area you can customise to add your company logo etc... %> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <html> <body> <h1>An error occurred!!!</h1> <hr> <b>Error: <%= errorMessage %></b> </body> </html> <% } catch(Throwable e2) { // Error in error handler out.println("An error has occurred, and could not be reported automatically.\n\n"); out.println("Please copy the details below into an email and send it to support.\n"); out.println(e2.toString()); } %>