JSF @PostConstruct exception handling

Discussions

Web tier: servlets, JSP, Web frameworks: JSF @PostConstruct exception handling

  1. JSF @PostConstruct exception handling (1 messages)

    Hi, I'm new to JSF and I've written an example page using facelets and icefaces on glassfish v2 app server. The page contains a number of drop down lists with values stored in a database. I've written an init() method in the JSF bean class that populates the lists used in the dropdowns by calling an EJB to get the data and annotated it with the @PostConstruct annotation. The page works nicely as a quick example but now I'm trying to improve the page by adding exception handling. What I'm wondering is how to deal with failures in the @PostConstruct method, e.g. if I catch an EJBException? Any method annotated with @PostConstruct cannot throw checked exceptions so I can't do that and add an entry in the web.xml, and if I throw a RuntimeException on catching an EJBException glassfish errors when it tries to forward onto my error 500 page with the following: "getOutputStream() has already been called for this response". Is there a better way of prepopulating data in JSF beans where it's possible to forward onto an error page, is there something else I can do in the @PostConstruct method, or am I doing the right thing and this is a bug in glassfish that my error 500 page doesn't work? Many thanks in advance, Darren
  2. the forward to error page will fail if some response content has already been flushed on the socket to the web client. in other words you need to make your @PostConstruct fail before any content is flushed. here are some tips I've gathered: 1) reference your FBs high in the JSP so @PostConstruct is invoked earlier - could be a simple or 2) increase JSP response buffer size of your app server 3) add a ServletFilter around your URI which can catch the exception and do a redirect to error page