<%--
/*
* @(#)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.
401/CustomErrorPage.jsp
403/CustomErrorPage.jsp
404/CustomErrorPage.jsp
500/CustomErrorPage.jsp
503/CustomErrorPage.jsp
*
* 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
* =====================================================================================
Custom Error Test App
Custom Error Test Application
401/CustomErrorPage.jsp
403/CustomErrorPage.jsp
404/CustomErrorPage.jsp
500/CustomErrorPage.jsp
503/CustomErrorPage.jsp
* 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" %>
<%
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 tag
if(e.getMessage() != null && e.getMessage().indexOf("Exception in JSP") != -1)
errorMessage = "An error occurred in a JSP file ...\n\n" + e.getMessage() + "
";
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 += "
You have followed a broken link on our website.
We apologise for the inconvenience.";
} else {
// Broken link on another site (ask user to contact admin of that site)
message += "
You have followed a broken link on another website.
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...
%>
An error occurred!!!
Error: <%= errorMessage %>
<%
} 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());
}
%>