This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I have a curious problem. More academic at this point than anything else. I am writing a login servlet. We are required by our client to not allow the user / customer to see any exceptions at all. Users need simply be redirected to a 'temporary difficulties' html page. So, in addition to other checks, we've put in a method-wide try - catch in the doPost method that logs the error and redirects the user in case something untoward occurs. Now, in updating and tweaking the code, I made an error in logic that was caught only in runtime. I was trying to call methods for an object before instantiation. No problem. It is now fixed. But the question is, why was the runtime NullPointerException not caught? Why did it spit up to the browser level immediately? I even tried catching the NPE explicitly, to no effect. Any thoughts on this? I can post code snippets if anyone thinks them relevant. Sanjay
As a workaround -- you could define an <error-page> in your web.xml so that every 500 Internal Servlet Error or every NullPointerException; it automatically sends your client to a "Temporary troubles" page.
From the Servlet 2.3 spec: <!--The error-page element contains a mapping between an error code or exception type to the path of a resource in the web application --> <!ELEMENT error-page ((error-code | exception-type), location) > <!--The error-code contains an HTTP error code, ex: 404 --> <!ELEMENT error-code (#PCDATA) > <!--The exception-type contains a fully qualified class name of a Java exception type. --> <!ELEMENT exception-type (#PCDATA) > <!--The location element contains the location of the resource in the web application. --> <!ELEMENT location (#PCDATA)>
Thanks Jessica for a good suggestion. It does not solve my academic question and it is also not usable because my servlet container, Macromedia's JRun, does not support the specs required for the error page functionality. And, "L" (a name not in keeping with the JavaRanch custom by the way). Here's a code snippet. I hope it formats correctly and is readable.
No, I have not checked with Macromedia. I will do that. Thanks for the suggestion. I am heartened to know that there isn't a bug in my code at any rate. Sanjay
Joined: Jul 05, 2001
Hi I have worked on JRun 4. Sometimes it gives exceptions like this and also ClassCastException for no bug in the code. You need to shut down the servers and even the admin one and restart then through Windows Services. Then it will work fine. I think its a bug in JRun container. Same querries are unanswered in JRun user forum.
Hi Jessica, I would also like to know if there is any similar solution possible for EJB's. I have methods defined in EJB and after caching the exception i just need to show the users a generic error message instead of the actual one. Thanks, pete