views:

273

answers:

1

Is it possible to replace the standard error pages from JavaServer Faces such as

500 Internal Server Error

?

These include information such as the stack trace, etc. But for the users, when the page is in Production Mode, I want to show a friendlier page saying that the request did not succeed.

+3  A: 

The development stack trace is probably coming from your JSF implementation. The code for disabling it will be specific to the impementation. For MyFaces in Servlets, use this init parameter in your web.xml:

  <context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>false</param-value>
  </context-param>

If you're using the Sun implementation (Mojarra), there may be some com.sun.faces... keyed parameter.

You may also want to check the value of Facelets init parameter facelets.DEVELOPMENT (make sure you haven't set it to true).

To specify an error page, you can use the usual container mechanisms. For Servlets, this would be by specifying a error pages in web.xml, keyed to either exception types or error codes. To catch all throwables...

  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errorPage.faces</location>
  </error-page>

You might find additional vendor-specific support for error handling in the JSF implementations - you'd have to check their documentation.

McDowell
Thanks, great answer! One follow up question though, would it be possible to use the error message on the catching page?(errorPage.faces)
Filip Ekberg
Oh and by the way, where do i find a list of all the com.sun.faces... keyed parameters? I am not using Myfaces, i am using JSF 1.2 with Facelets.
Filip Ekberg
To get the error message, try the expression `#{requestScope['javax.servlet.error.message']}`. To get the exception, use `javax.servlet.error.exception` - see the Servlet spec for a full list error values set to the request map (if you want the error code, etc). You can Mojarra context params in the Glassfish FAQ: http://wiki.glassfish.java.net/Wiki.jsp?page=JavaServerFacesRI#section-JavaServerFacesRI-WhatContextParametersAreAvailableAndWhatDoTheyDo
McDowell
Having the following: <error-page> <error-code>500</error-code> <location>/error.jsf</location> </error-page>doesn't seem to work, the page is just blank, when i add /error.jsf to my URL it works but not when i get an error, any ideas on that?
Filip Ekberg
If you redirect to an error page by error code, I don't think the exception is set (so any exceptions/messages will be null). Probably because an exception is not the only way to trigger an error code. Try redirecting based on the exception type. Otherwise, all I can say is that it works on my machine...
McDowell
I see, well I changed the XML-file so that it looks like this now: <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/nullerror.xhtml</location> </error-page> however that displays the xhtml and not the correct jsf-encoded stuff. and when i change it to nullerror.jsf it's just blank, but when i navigate to it, it displays the content. Really weird, you getting any ideas on what could be wrong?
Filip Ekberg
It may be that the error state your app enters prevents the JSF lifecycle from being able to function. If you get really stuck, you can always switch to a non-JSF JSP to use for the error page. You may be able to use a servlet filter to do some error handling/redirection too if necessary.
McDowell