wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question about error pages and JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question about error pages and JSTL" Watch "Question about error pages and JSTL" New topic
Author

Question about error pages and JSTL

Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
Hi,

first time I post here and I am currently preparing the SCWCD exam using the HF Servlets and JSP book (a great one ...!).

here is my question:
I noticed that when configuring the error page only in the DD file, I am not able to get the exception object from the pageContext in my errorPage.

I added in the DD:


my error page:


my test page:


when calling the test page, I see the errorPage (display: you generated an error , but there is nothing about the exception.

but ... when adding in the test page, I see the complete error message:

You generate an error:
java.lang.ArithmeticException: / by zero


I thought that when you set a page as an errorPage (with the page directive), you always can access the pageContext.exception object.

Did I something wrong?

regards
Pierre
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
Nobody can help me?
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
Don't hate me for suggesting this.

What happens when you call your page something other than "errorPage.jsp"? Name it myWhatEverPage.jsp?
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
That was a good sugestion ... but I tried that before ... and still the same problem.

At this time, I am really confused by this stuf (see my other post for more details).

thanks again to spent some time on that
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
What happens when you replace ${pageContext.exception} with ${pageContext.errorData.throwable.cause} or ${pageContext.errorData.throwable.rootCause}
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
${pageContext.errorData.throwable.cause} returns nothing ...
and
${pageContext.errorData.throwable.rootCause} generates an error: rootCause not available.
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
But there is something more strange ...

I noticed that I worked with an older version of J2EE. To have access to the new features from J2EE 1.4 and JSP2, I updated my environment with the last version provided by SUN (eg. 2005Q1 UR1). And now start the troubles ...

There is no way to access an error page while setting error management only in my web.xml (with <error-page>) . The application always fails with an HTTP 500 - internal server error!

While redirecting error with the "errorPage" attribute from the page directive, I can access my custom error page ... but only if this page is not set as an error page (using the "isErrorPage" attribute):
If isErrorPage is set to "false" or not specified, the error page is accessible and I can access the exception implicit object

If isErrorPage is set to "true", the application failed with an HTTP 500 - internal server error

I am really confused with that, because I follow the JSP2.0 specs, and that doesn't work at it MUST.



all helps are welcomed !

I tested that with the previous J2EE version (from june 2004), and still have the problem.
[ March 18, 2005: Message edited by: Pierre Schuffenecker ]
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
What do you get when you use getMajorVersion() and getMinorVersion() in this updated container of yours? Additionally, which DTD(the first few lines of your web.xml) are you using?
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
Try updating your container version.

And one more thing, try to access exception object in your error page using scriptlets. If that returns your exception message, the error resides somewhere in the JSTL part.

I am sure that your exception must be propagated to your error page.

Try using this page directive in your test page:


Secondly try this in your error page:


If the above works, you may check it using JSTL.
I hope this helps you debug.


BS<br />SCJP 1.4 88%<br />SCWCD 1.4 86%
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
Originally posted by Nitish Bahadur:
What do you get when you use getMajorVersion() and getMinorVersion() in this updated container of yours? Additionally, which DTD(the first few lines of your web.xml) are you using?


I mean, you wanted to talk about major and minor version available from the application object ...
in that case, I saw the following:
MajorVersion = 2
MinorVersion = 4

Don't know what exactly it means.
regards
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
-> Bassam Zahid

I tried that you proposed ... and I always have an error.
The strange thing is that using

in my error page, I saw a NullPointerException on the the exception object!
I checked the translated code from my error page, and everything seems to be fine.

while testing with EL ( ${pageContext.exception} ), I always have an HTTP 500 error and nothing in the logs.

I tried with the 2 last available version from J2EE.
I will maybe retry with an older version.
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
this is just to clarify the situation.

I completly removed all java and tomcat stuffs, then started from a clean environment.
I run now:
- j2se 1.5 (the latest one)
- tomcat 5.5.7 (that supports servlet 2.4 and jsp 2.0 specs)

I recreated a (very) small web app to check the error management (the same as before) and included (just to be sure) the JSTL libraries in my WEB-INF/lib folder.
tested ...
and I always get the same issues:
- while setting the error pages with the DD, the container doesn't redirect to the error page.
- while setting the error page with the page directive, I am correctly redirect to the "error page", but only if this page is not set as an error page (isErrorPage = true).

Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
I tried your code and design and I am afraid, all the times I FAILED to produce the error.

Don't know why it is so.
It is working fine on my system.
What's wrong with yours ???
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
Please also check that you use only one method at a time. Make sure that you use one of the following:

1) Declare the error page through DD

OR

2) Program the error page in JSP

--------------------------------------

First try not to declare the error page in DD. Use the JSP mechanism to see if it works. May be you are getting error due to conflict between the two.
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
Declaring an error page in the Deployment Descriptor does not guarantee you that you will have a reference to the exception object in the Error Page.
But if you use a page directive to indicate your error page in your JSP you are guaranteed to have a reference to the exception object in your error page so long as your error page declares that it is an error page.( The JSP specification REQUIRES this)

If the page directive indicating an error page is absent in a JSP then the JSP specification REQUIRES that the container's STANDARD exception handling mechanism take over.

I'm of the opinion that may be, TOMCAT sticks to this rule and does not bother passing a reference to the exception object to your error page.

(Refer to JSP 2.0 SPEC page 2-32 for details)

I hope someone who has command over this will clarify this.
Pierre Schuffenecker
Greenhorn

Joined: Mar 11, 2005
Posts: 13
Thanks to all ...

I really mean that there is a problem with my J2EE environment on my PC.
I tried yesterday evening with another computer ... and everything worked as expected.

I don't understand why it doesn't work properly.
After 3 full clean-up and re-install, I still have this issue.

As you said, maybe someone involved in Tomcat can explain this problem ...

Thanks for your help

Pierre
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
There is another guy in the same forum who faced the same prob and it was due to his OS.

Try on XP. It works fine.
siva raju
Ranch Hand

Joined: Oct 10, 2003
Posts: 37
I had similar problems,

Problem 1: Internal server error.

If there is an non-null exception object in the error handler page, the error handler sets the response status as
SC_INTERNAL_SERVER_ERROR (5XX)
I looked at the generated jsp source code by Tomcat 5 to know this. I verified this by seeing the HTTP headers.
In older operating systems, the browser might not have handled this(a 5XX error) correctly. When it sees a 5XX, the browser might be throwing up.
When you set the isErrorPage="false", there is no "exception" implicit object.Hence, You dont see this message.


Problem 2. ${pageContext.exception} is empty while <%= exception %> works.

Looking at the Tomcat 5 jasper source code, this value is equal to the attribute, "javax.servlet.jsp.jspException"
Again from the code, I found that this attribute is set only if the jsp that throws the exception, has its

errorPage="xxxx" value set.
<%= exception %> works bcos it takes it source from another request attribute "javax.servlet.error.exception". This

attribute is always set irrespective of the errorPage value.


Siva<br />-----<br />SCWCD 1.4, SCJP 1.4, SCBCD(Preparing), CCNA
Michael Witherspoon
Greenhorn

Joined: Apr 02, 2005
Posts: 3
Starting with your original post, there's something you don't know and probably should. In spite of what we all assume to be true about the availability of the implicit exception object when using JSPs as error pages, that object is ALWAYS null if the JSP loads due to being mapped to an error-page element in web.xml.

What I mean is, we assume that if you write
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/html404.jsp</location>
</error-page>
in your web.xml, then when a 404 error happens and the html404.jsp JSP loads, the implicit exception object is useful. It's not. It's null, is always null, and will never be anything other than null.

I wish it were otherwise. I also wish the documentation I find at places like java.sun.com where experts pontificate on how one can write the following code into that same html404.jsp and get something useful from the implicit exception object were actually true. It's not. Take this example, for example:

In the html404.jsp file, some people say that you can write this at the top:
<%@page isErrorPage="true" %>
and then automatically gain access to the implicit exception object and so something like this:
<%= exception.getClass().getName() %>

You can, but the only thing you get is the web container exploding all over itself complaining about the exception object being null. Why? Because it is ALWAYS null if the JSP is loaded due to a mapping in the web.xml to an HTTP error code number.

So, why do people write stuff like this? They think but don't test and spew forth "knowledge" without testing. They should test first. If they did they would find out that the only time the implicit exception object is NOT null is when the JSP error page is loaded due to another JSP containing a page directive declaring the error page like this:
<%@ page errorPage="/WEB-INF/errorpages/CustUpdateErrors.jsp" %>

For example, if JSP a.jsp has a page directive like:
<%@ page errorPage="/b.jsp" %>

and b.jsp has a directive like:
<%@page isErrorPage="true" %>

then and only then will the implicit exception object be available with a value other than null.

So, we assume that every JSP error page has a non-null implicit exception object but we are very, very wrong. Only those loaded by another JSP in the manner shown above have one. JSP error pages loaded due to a mapping in the DDs NEVER have a non-null implicit exception object. Oh, yes, the object is there but it's null. If you try to declare a variable for it like:
<% Exception exception %>
you get an error about exception already being declared, so it's definitely there and it's definitely null, and it's definitely useless, and I can't go on anymore about it.

Spoon
Michael Witherspoon
Greenhorn

Joined: Apr 02, 2005
Posts: 3
After more testing, I've determined that mapping a Java exception to a JSP error page in the deployment descriptors works, too, but mapping and HTML error code still does not.

For example, if the DDs says this:
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>

the error.jsp can access the implicit exception object. I wish the HTTP error code thing would work, however.
Michael Witherspoon
Greenhorn

Joined: Apr 02, 2005
Posts: 3
Experimentation is the key. It turns out that I'm wrong, too. There's more than meets the eye as always seems to be the case in J2EE stuff.

My rant that the implicit exception object is ALWAYS null when the error JSP is loaded due to a mapping in the deployment descriptors is only mostly correct. It is ALWAYS null for HTTP error 404. It just so happens that error 404 is the one most people want to trap for. HTTP error 500 sometimes, maybe even most of the time, has a non-null implicit exception object. The tricky part is that sometimes its null and sometimes it's not.

Trapping for HTTP error 500 in this manner also seems to interfere with error handling for JSPs that have their own JSP error pages declared. It will take much more experimentation to figure out exactly what's wrong and the circumstances under which the 500 error has a null exception object and when it doesn't.

Suffice it to say, if you really want to know how the error handling stuff in JSPs works, do a few dozen hours of experimentation because it's nowhere near as clean as people make it out to be.

Finally, sorry for the earlier rant but I'm trying to learn this stuff and I'm getting lots and lots of half-right to mostly wrong answers. It's very frustrating. When those answers come from "experts" I think I'd rather have no answer than wrong ones.

Spoon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about error pages and JSTL
 
Similar Threads
problem while declaring error page
error pages in dd
error-page tag facing problem
pageContext.exception is not executing
Exception: c:catch doubt