Using JSP I can use implicit object exception when my JSP page is declared with <%@ page isErrorPage="true" %> directive. But how to get this reference programmatically, for example with a servlet ?

Luigi Viggiano

It is possible to get a reference to the exception even if our page is not declared with isErrorPage="true" attribute, or inside a servlet.

The Java Servlet Specification Version 2.3 / JSP 1.2 (paragraph SRV.9.9.1 "Request Attributes"), introduced a new request attribute to get the exception object. So you can write following code with any Servlet container implementing Servlet specifications 2.3 / JSP 1.2 (i.e. Tomcat 4):

Throwable exception = (Throwable)request.getAttribute("javax.servlet.error.exception");
If you are using Servlet specifications 2.2 / JSP 1.1 (i.e. Tomcat 3.x) you can explore JSP generated code to discover that it does a similar thing to get the exception object within a JSP error page:
Throwable exception = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");
as specified in the JavaServer Pages™ Specification (paragraph 2.2.2 "Client Request Time Processing Errors" page 38, see also pag. 47).

This should work in all JSP 1.1. engines, and Servlet 2.2 containers.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.