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
}
}
}