Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JSF to generate HTTP Error Code pages as well as Exception Error pages?

I understand that it is possible to serve a custom Error 404 page using configuration in web.xml like so:

<error-page>  
       <error-code>404</error-code>  
         <location>/NotFound.jsp</location>  
 </error-page>  

However I anticipate that many error-code webpages will be very similar - only the error code and text would change.

To cut down on excessive numbers of files and re-use, it would be nice to pass a parameter to a generic page which has all the usual HTML and CSS goodness.

I use Apache MyFaces, and have read about the ExceptionHandlerFactory (here) but I find that there is no provision for Error Codes.

Is it the case that the absolute only way I can customise error-code pages is to create one for each error-code that I want to customise, and wire them up using web.xml? :-(

like image 536
8bitjunkie Avatar asked Jan 17 '23 17:01

8bitjunkie


1 Answers

The error page can be just a JSF page. If you're running a Servlet 3.0 capable container, you can have a global error page:

<error-page>  
    <location>/error.jsf</location>  
</error-page> 

If you're running a Servlet 2.5 container or older, or happen to use Tomcat 7 who still doesn't have the global error page implemented as of now, then you need to specify separate error pages for every status code, but they can all point to the same error page.

<error-page>  
    <status-code>404</status-code>
    <location>/error.jsf</location>  
</error-page> 
<error-page>  
    <status-code>500</status-code>
    <location>/error.jsf</location>  
</error-page> 

The status code and exception type are in EL available as follows:

Status code: #{requestScope['javax.servlet.error.status_code']}
Exception type: #{requestScope['javax.servlet.error.exception_type']}

You can if necessary do conditional checks on this. Note that exceptions have by default always a status code of 500.

If you're using JSP's successor Facelets, templating would even be more easy.

like image 141
BalusC Avatar answered Apr 26 '23 21:04

BalusC