0

I have JSP page with some EL inside. Unfortunately, EL expressions throws exception. As result user sees half of the page while system logs exception and sends my email.

Is it possible to configure container (tomcat) to process jsp page first and THEN display it to user and display 500 error page in case of any exception?

Error page is much better than partially rendered page.

Yes, I know that template engine should process viewmodel which nothing but accessors/mutators and no exception should be thrown, but for now I have to access some business (domain model) objects in EL.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user996142
  • 2,753
  • 3
  • 29
  • 48

3 Answers3

1

EL expression evaluations are not supposed to throw exceptions.

If this happens, then that can only mean that you've a bug in your preprocessing code or in the way how your getter methods are designed.

The preprocessing code (read: the front controller servlet) must make absolutely sure that any of the data which the page needs to access/present is already in the scope. The getter methods which the EL expressions refer must not do anything else than just returning an already-prepared/loaded property.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You could use JSTL

<c:catch> 

tag. An example:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
 <c:catch var="exp">
             ${.}
 </c:catch>
 <c:if test="${exp != null}" >
      Expection was thrown in EL
 </c:if>
rickz
  • 4,324
  • 2
  • 19
  • 30
  • That won't help anything. OP's concrete problem is that the response is already committed at the point the exception was been thrown. Otherwise the servletcontainer should already have shown the default error page in its entirety. – BalusC Mar 29 '12 at 23:32
  • @BalusC You are right. He should cleanup up his code. That makes the most sense. But, he seemed desperate to get something working. I edited above here to show the code I was suggesting. – rickz Mar 29 '12 at 23:57
  • Another crazy idea: increase output buffer size. Maybe that way the response would not commit until the whole page was ready to go( or throw an exception first). – rickz Mar 30 '12 at 00:01
  • Again, this won't help anything. Even when you caught the exception with ``, you won't be able to dipslay the standard HTTP 500 error page as required by the OP, simply because the response is already committed. That's the whole problem of the OP. If the response wasn't committed at that point, the servletcontainer would automatically have shown the error page. But this is not the case for the OP. – BalusC Mar 30 '12 at 00:33
  • Wouldn't increasing the size of the page buffer delay commitment ? – rickz Mar 30 '12 at 00:47
0

You can configure a 500 error page for your application with web.xml.

<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/error-pages/500.jsp</location>
</error-page>

Put this inside <web-app>...</web-app> of web.xml file. For any server-sided exception, it will redirect the user to 500.jsp page

Bart
  • 19,692
  • 7
  • 68
  • 77
tusar
  • 3,364
  • 6
  • 37
  • 60
  • I think you did not understand OP's concrete problem like as rickz. I can however understand your misunderstanding as OP is practicing a bad practice of doing exception-sensitive business job in getter methods behind EL expressions, which is something not everyone would expect/do. – BalusC Mar 31 '12 at 03:37
  • Ah ! what my luck today ! Good morning @BalusC sir ! yes I read the question correctly by this time. what I feel now, my answer is not to the point of question but it suggest a solution for `Server-sided-error`. thank you for clearing. Have a nice day :) – tusar Mar 31 '12 at 04:00
  • OP **already** has an error page (even when he didn't have one, the servletcontainer has still a default one). The concrete problem is that it's not been displayed because the response is already committed. Re-read the first paragraph of the question in its entirety. – BalusC Mar 31 '12 at 04:01
  • okey I understand ! so required part of the answer is written in italics... :) – tusar Mar 31 '12 at 04:05
  • That part is only noise. Just post a comment. – BalusC Mar 31 '12 at 04:07
  • for curiosity, does JSF can handle this kind of exception ? I have never-ever used JSF. zero-knowledge. – tusar Mar 31 '12 at 04:09
  • It's not different. The OP's problem is caused by a major design mistake in his own code. JSF does not magically fix bad code. – BalusC Mar 31 '12 at 04:11
  • Okey. BTW I wont delete the answer even-if it receives minus-vote. will keep the link as my first interaction with the great BalusC. Thank you sir, for giving so much to the community. – tusar Mar 31 '12 at 04:19
  • @BalusC sir, I have a problem. could you please have a look at this ? http://stackoverflow.com/q/8937158/778687 – tusar Apr 03 '12 at 05:08