Reputation: 40327
In some legacy code we had, we had a process that grab our "base" JSP files, and stick a header, footer, etc. and anything else we wanted on all the JSP files throughout the app. One thing this process added to all the JSPs was a try-catch block around the whole JSP in scriptlets. So, in the end, all our JSPs would look something like this:
<%
try
{
%>
.... all the rest of the JSP .....
<%
}
catch(Exception e)
{
Log.error(e);
}
%>
We recently got rid of this process and moved our JSPs over to use JSTL and not have any scriptlets. We created a tag that we basically wrap around each JSP do the the header, footer, etc. So now our JSPs look something like this:
<foo:page>
.... all the rest of the JSP .....
</foo:page>
In making this switch, we lost the ability to catch any exceptions that happen while loading the page. I did some looking around and found the <c:catch>
tag. I've tried to put this into our page tag, but I can't quite get it to work the way we want. I found that if I put the <c:catch>
tag just around the <jsp:doBody/>
tag in the page tag, it would catch the exception and I could do something with it. However, this won't catch any exceptions that are thrown in other parts of this outer page tag. Ideally I would enclose our entire page tag with the <c:catch>
tag, but when I do that, it doesn't seem to catch the exception. The page just stops rendering at the point the exception was thrown.
I have the same
<c:if test="${!(empty pageException)}">
ERROR!
</c:if>
after the <c:catch>
tag in both cases, but I only actually see "ERROR!" in the source when the <c:catch>
tag is immediately around the <jsp:doBody/>
Any information about this would be greatly appreciated.
Upvotes: 1
Views: 7372
Reputation: 51
The <c:catch>
should work. As a backup, you can have your <foo:page>
class implement TryCatchFinally and handle the exception there.
Upvotes: 1
Reputation: 5533
You may use errorPage
attribute in <%@ page %>
directive instead of wraping of try catch block or <c:catch></c:catch>
blocks.
1.Create a single error JSP page which handles the errors that occur across all the other JSP pages in the application. To specify a JSP page as an errorHandler page, use this JSP page directive:
<%@ page isErrorPage="true" %>`
In the errorHandler JSP page, use exception
implicit object to retrieve exception details.
2.Include the errorHandler JSP page in other JSP pages, by using this JSP directive to specify that if there are exceptions occurring on the current page, forward the request to errorHandler.jsp:
<%@ page errorPage="/errorHandler.jsp" %>
Upvotes: 2