• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error Page Problem

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <c:out value="pageContext.exception"> shows nothing.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <c:out> in a fashion similiar to you:
<c:out 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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark, I will definately look into that!

Chris
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic