GeeCON Prague 2014*
The moose likes JSP and the fly likes Error Page Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Error Page Problem" Watch "Error Page Problem" New topic
Author

Error Page Problem

Chris Cornelius
Ranch Hand

Joined: Aug 02, 2005
Posts: 48
Hello,

I am having problems with the error page I am using. I would like to
display the error in the page, but it does not show up.

Here is the code:


If I use this instead I get null pointer exceptions:



Thanks in advance,

Chris Cornelius
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
What are you getting while trying the former approach?

Have you configured the error-page element in web.xml or errorPage attribute in your JSP page, instead.
[ December 15, 2005: Message edited by: Adeel Ansari ]
Chris Cornelius
Ranch Hand

Joined: Aug 02, 2005
Posts: 48
I currently have this code in the web.xml file.

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/SystemErrorDisplay.jsp</location>
</error-page>
<error-page>
<exception-type>org.apache.jasper.compiler.ParseException</exception-type>
<location>/SystemErrorDisplay.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/SystemErrorDisplay.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/SystemErrorDisplay.jsp</location>
</error-page>

Plus, using the <cut value="pageContext.exception"> shows nothing.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

This may answer your question.
From Chapter 9 of JavaServer Pages, 2nd Edition, by Hans Bergsten, OReilly Publishing. In the quote, "this approach" refers to the use of an error page declaration in the deployment descriptor (i.e. web.xml).

Due to an unfortunate naming mismatch between the servlet and JSP specification, there's one problem with this approach if you use a JSP page to handle the exception: the exception property of the implicit pageContext variable isn't initialized so you can't log or display the exception message as in Example 9-12. I show how you can use a servlet to work around this problem in Chapter 18.

In example 9-12 that he references in the quote, the author uses the <cut> in a fashion similiar to you:
<cut value="${pageContext.exception.message}" />

(Note the 2nd Edition of this book is based mostly on the Servlet 2.3/JSP 1.2 specification).

If you add a <%@ page errorPage="SystemErrorDisplay.jsp" @> to the page you are using to cause the error/exception, you should get the desired results. Of course the down side of using the errorPage attribute is that all types of errors/exceptions are handled via the same error page. You therefore lose the advantage of having a different page based on the type of error (one page for 404 errors, another for General Exceptions, etc). So you will need to work around the issue if you want to use the exception/error mappings in your deployment descriptor.

In chapter 18 of his book, Mr. Bergsten goes on to say:


There's one problem, though: if you use a JSP page to handle the exception, you can't log or display the exception message, because a JSP page invoked this way doesn't have access to the exception through the type of EL expression used in the error page in Chapter 9. The reason is a request-attribute name mismatch between the servlet and JSP specification. The servlet specification defines the name javax.servlet.error.exception for the request attribute holding the Throwable object that represents the exception, while the JSP specification defines the name javax.servlet.jsp.jspException. Hopefully this will be synced in future versions of the specifications, but for now you can work around the problem using a servlet to set the attribute the JSP specification requires.


He then goes on to give an example of creating a Servlet to set the attribute correctly. I do not believe it would be within fair use of copyright laws for me to publish that solution here since it goes beyond just a mere quote at that point. So you may want to look at the book yourself to get the solution (it is avaialbe online via safaribooksonline.com). Or give it some thought and engineer your own solution.

Again, this is from an edition of the book that deals with the 2.3/1.2 specification. The issue may have been fixed in 2.4/2.0, but I so not know, and I have not yet purchased the 3rd edition of his book (since the environment I work in is still a 2.3/1.2 environment) and therefore cannot look to see if he still discusses that as an issue.
[ December 16, 2005: Message edited by: Mark Vedder ]
Chris Cornelius
Ranch Hand

Joined: Aug 02, 2005
Posts: 48
Thanks Mark, I will definately look into that!

Chris
 
GeeCON Prague 2014
 
subject: Error Page Problem