Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show user-friendly error page in browser when runtime exception is thrown by servlet?

I'm developing web-application with JSF. I tested it as I was able to but from time to time runtime exceptions are thrown.

So, how to redirect user to special error page every time an exception is thrown (instead of displaying 500 Error with full tomcat logs)?

like image 450
Roman Avatar asked Apr 30 '10 23:04

Roman


People also ask

How do I display error messages in the same JSP page?

To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.

How would you handle exceptions thrown by application with another servlet?

If the web application throws either ServletException or IOException, then the web container invokes the /ErrorHandler servlet. You can define different Error Handlers to handle different type of errors or exceptions. Above example is very much generic and hope it serve the purpose to explain you the basic concept.

Can we handle runtime exception in JSP page?

You can catch and handle exceptions in a Java scriptlet within the JSP page itself, using standard Java exception-handling code. Exceptions you do not catch in the JSP page will result in forwarding of the request and uncaught exception to an error page. This is the preferred way to handle JSP errors.

What happens when runtime exception is thrown?

The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.


2 Answers

Just declare an <error-page> in web.xml wherein you can specify the page which should be displayed on a certain Throwable (or any of its subclasses) or a HTTP status code. E.g.

<error-page>     <exception-type>java.lang.Exception</exception-type>     <location>/error.jsp</location> </error-page> 

which will display the error page on any subclass of the java.lang.Exception, but thus not java.lang.Throwable or java.lang.Error. This way you can have your own error page for any kind of Throwable. E.g. java.sql.SQLException, java.io.IOException and so on.

Or,

<error-page>     <error-code>500</error-code>     <location>/error.jsp</location> </error-page> 

which will display the error page on a HTTP 500 error, but you can also specify another ones for 404 (Page Not Found), 403 (Forbidden), etcetera.

If you declare <%@page isErrorPage="true" %> in top of error.jsp, then you have access to the thrown Exception (and thus also all of its getters) by ${exception} in EL.

<p>Message: ${exception.message}</p> 

Also see the Java EE 5 tutorial on the subject.

like image 193
BalusC Avatar answered Sep 30 '22 16:09

BalusC


In your web.xml:

<error-page>   <error-code>500</error-code>   <location>/errorpages/500.jsp</location> </error-page> 

You may also catch specific exceptions or exceptions which extend Throwable:

<error-page>   <exception-type>java.lang.Throwable</exception-type>   <location>/errorpages/500.jsp</location> </error-page> 
like image 40
Pascal Thivent Avatar answered Sep 30 '22 17:09

Pascal Thivent