display errors on custom JSP page

404

 Sometimes customization of the 500 and 400 error pages might be desired.  To do this, configuring the  web.xml file to send these errors to the customized page(s) would help achieve the task. 

 

 

Adding the below snippet to web.xml would send all errors not handled by the application to the error page.

	<error-page>
		<location>/WEB-INF/error.jsp</location>
	</error-page>

 

What if the developer wants to display the error line on the customized page? The PageContext object gives the developer access to the exception and its meta data. 

 

The code snippet below shows an example of displaying the exception on a cutomized jsp page.

<c:if test="${not empty pageContext.exception}"> 
 <tr>
  <td align=center>
	<table align=center border=0 width=100%>
	 <tr>
	  <td> 
        <h3 style="color:#cc0000;">
          Error: ${pageContext.exception}
        </h3> <br/>
	    Exception stack trace:<br/>
	    <c:forEach var="trace" 
          items="${pageContext.exception.stackTrace}">
		  ${trace}<br/>
	    </c:forEach>
	  </td>
	</tr> 
	</table>
  </td>
 </tr>
</c:if>