-1

In my application I receive username/password via a servlet which calls a SessionScoped ServiceBean which initializes a SSH connection in a @PostConstruct method. If the SSH host is not available I want to redirect the user to "/applicationerror.html". Therefore I throw a RuntimeException in this method.

Neither a JSF ExceptionHandler catches my exception nor can I redirect the user via FacesContext.getInstance().getExternalContext().redirect() as the FacesContext is null in my PostConstruct method.

What can I do to avoid the http 500?

public class LoginServlet extends HttpServlet {

   @Inject
   private AnyBean anyBean;

   doPost() {
      anyBean.loginUser(requestparamusercode, requestparampassword)
   }
}

@Named
@SessionScoped
public class AnyBean implements Serializable  {

   @Inject
   private SshService sshService;

   public boolean loginUser(String usercode, String password) {
      sshService.login(usercode, password);
   }

@SessionScoped
public class CommandServiceImpl  {

   public boolean login(usercode, password)  {
      ....
   }

   @PostConstruct
   private void initSshConnection() {
      try {
      } catch(IOException e) {
     // what to do here
   }
}
}
Ginkgochris
  • 455
  • 4
  • 25

1 Answers1

0

Sounds like you're going to have to catch the exception and use a RequestDispatcher in the catch block.

LightGuard
  • 5,298
  • 19
  • 19
  • WITHIN the @PostConstruct method I can catch the exception. But what then? Where from do I get a ServletContext to create a RequestDispatcher? – Ginkgochris Jun 18 '13 at 07:41
  • 1
    You probably need to do the exception handling for the redirect in the servlet. – LightGuard Jun 18 '13 at 16:05