May 12, 2021 JSP
When writing JSP programs, programmers may miss bugs that can appear anywhere in the program. There are usually several types of exceptions in JSP code:
This section will give you a few simple and elegant ways to handle runtime exceptions and errors.
The exception object is an instance of the Trowable sub-class and is only available on the error page. The following table lists some important methods in the Trowable class:
Serial number | Method . . . |
---|---|
1 |
public String getMessage()
Returns information about the exception. This information is initialized in the Trowable constructor |
2 |
public ThrowablegetCause()
Returns the cause of the exception, type Throwable object |
3 |
public String toString()
Returns the class name |
4 |
public void printStackTrace()
Output the exception stack track to System.err |
5 |
public StackTraceElement [] getStackTrace()
Returns an exception stack trace as an array of stack trace elements |
6 |
public ThrowablefillInStackTrace()
Fill the Throwable object with the current stack track |
JSP provides the option to specify error pages for each JSP page. Whenever a page throws an exception, the JSP container automatically calls the error page.
The next example .jsp an error page for the main page. Specify an error page @page using the errorPage "XXXXX"% directive.
<%@ page errorPage="ShowError.jsp" %>
<html>
<head>
<title>Error Handling Example</title>
</head>
<body>
<% // Throw an exception to invoke the error page int x = 1; if (x == 1) { throw new RuntimeException("Error condition!!!"); } %>
</body>
</html>
Now, write the ShowError .jsp as follows:
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<pre>
<% exception.printStackTrace(response.getWriter()); %>
Note that the ShowError.jsp file uses the @page "true" instruction, which tells the JSP compiler that it needs to produce an exception instance variable.
Now try visiting the main .jsp page and it will produce the following results:
java.lang.RuntimeException: Error condition!!!
......
Opps...
Sorry, an error occurred.
Here is the exception stack trace:
You can use the JSTL tag to write the error page ShowError .jsp. The code in this example is almost the same as the logic of the code in the example above, but the code in this example is better structured and provides more information:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<table width="100%" border="1">
<tr valign="top">
<td width="40%"><b>Error:</b></td>
<td>${pageContext.exception}</td>
</tr>
<tr valign="top">
<td><b>URI:</b></td>
<td>${pageContext.errorData.requestURI}</td>
</tr>
<tr valign="top">
<td><b>Status code:</b></td>
<td>${pageContext.errorData.statusCode}</td>
</tr>
<tr valign="top">
<td><b>Stack trace:</b></td>
<td>
<c:forEach var="trace" items="${pageContext.exception.stackTrace}">
<p>${trace}</p>
</c:forEach>
</td>
</tr>
</table>
</body>
</html>
The results are as follows:
If you want to put exception handling on one page and handle different exceptions differently, you need to use try... Catch block.
The next example shows how to use try... catch block, put these codes in the .jsp:
<html>
<head>
<title>Try...Catch Example</title>
</head>
<body>
<% try{ int i = 1; i = i / 0; out.println("The answer is " + i); } catch (Exception e){ out.println("An exception occurred: " + e.getMessage()); } %>
</body>
</html>
Try accessing the main .jsp, which will produce the following results:
An exception occurred: / by zero