• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Error Pages in JSP 2.0

February 24, 2009 //  by Krishna Srinivasan//  Leave a Comment

We can configure error pages in jsp and servlets to direct the control to a custom error page, showing a friendly error message to the user when an exception is thrown in the page. But tracking or logging the exception information is not very easy in JSP 1.2.

also read:

  • Java EE Tutorials
  • Servlets Interview Questions
  • New Features in Servlets 3.0
  • Asynchronous Servlets in Servlets 3

JSP 2.0 fixes this problem by switching to the servlet specification: javax.servlet.error.exception. In addition, a new property of the implicit EL pageContext variable, named errorData, exposes other information about the problem. The errorData property is an instance of the javax.servlet.jsp.ErrorData class that can be used as a bean with the following properties:

<table border="1" width="55%" style="background-color:#EFEFEF">
	<tr align="center">
		<td width="10%"><b>Property</b></td>
		<td width="12%"><b>Java Type</b></td>
		<td><b>Description</b></td>
	</tr>
	<tr align="left">
		<td>requestURI</td>
		<td>String</td>
		<td>The URI for the request that failed.</td>
	</tr>
	<tr>
		<td>servletName</td>
		<td>String</td>
		<td>The name of the servlet or JSP page that failed.</td>
	</tr>
	<tr>
		<td>statusCode</td>
		<td>int</td>
		<td>The failure status code. </td>
	</tr>
	<tr>
		<td>throwable</td>
		<td>Throwable</td>
		<td>The exception that caused the error page to be invoked.</td>
	</tr>
<table>

1) File Name : firstPage.jsp

<%@page errorPage="/jsp/errorpage.jsp" contentType="text/html" %>
<html>
<head>
	<title>Error Pages in JSP 2.0</title>
</head>
	<body>
	<%
		String s = null;
		out.println(s.trim());
	%>
	</body>
</html>

2) File Name : errorpage.jsp

<%@page isErrorPage="true" contentType="text/html" %>
<html>
	<body>
		Request that failed: ${pageContext.errorData.requestURI}
		
		Status code: ${pageContext.errorData.statusCode}
		
		Exception: ${pageContext.errorData.throwable}
		
		${pageContext.errorData.servletName}
	</body>
</html>

3) Output

Request that failed: /TestWebApp/jsp/firstPage.jsp
Status code: 500
Exception: java.lang.NullPointerException
jsp

Explantion:

In the first page I declare a null string and I call a function on it. The JSP engine will encounter faulty code and will throw a NullPointerException. As I have configured an error page in the page directive the control is taken to the errorpage.jsp.

In the error page I use properties of errorData to display useful information about the exception. This information is useful for the developer to track down the problem quickly and efficiently as he will know exactly which page threw the exception and what was the type of exception.

Category: Java EETag: JSP, JSP 2.0

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « New Features in Java 7.0 – Part 1
Next Post: Custom Tag Libraries and Tag Files in JSP 2.0 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact