Search code examples
jsfjsf-2rmi

JSF2: How to initiate services at deployment


For university project I am developing a webapplication with JSF. My excercise is to do the frontend. A fellow studend is supposed to do backend stuff. Both parts are designed to be seerate applications. Both communicate through RMI. I want to open the connection once at deployment.

I am at the point to settle up the connection now. I tried to do that with a @ApplicationScoped ManagedBean:

//Constructor of ApplicationScoped ManagedBean  
public Communication() {
    this.connect();
}

Is that way possible? I tried it but the managedBean seems not to be called..

Can you advice a Best Practice?


@Brian: Unfortunately I don't use EJB at all -.-

@BalusC's pot: I created a communicationbean:

@ManagedBean(name="communication")
@ApplicationScoped
public class Communication {

    public static FrontendCommInterface server;

    public Communication() {
        this.connect();
    }

Then I created the LoginBean:

@ManagedBean
@ViewScoped
public class Login {

@ManagedProperty(value="#{communication}")
private Communication communicationBean;

public FrontendCommInterface server;

private String username;
private String password;

public Login() {
    server = communicationBean.getConnection();
}

public String login(){
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String sessionId = session.getId();

    try {
        server.login(getUsername(), getPassword(), sessionId);
        return "start.xhtml";

    } catch (RemoteException e) {
        e.printStackTrace();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Anmeldung nicht erfolgreich: ", getUsername()+", "+getPassword()+", "+sessionId));
        return "login.xhtml";
    }
}

But unfortunately it throws exceptions:

com.sun.faces.mgbean.ManagedBeanCreationException: Klasse org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login can not be instanciated.

java.lang.NullPointerException
    org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login.<init>(Login.java:28)

After debuging I found out that my ManagedProperty is Null ! It hasn't been created! How to do that? I thought referencing via managedproperty would create it -.-


Solution

  • The managed bean is only auto-created whenever it's been referenced by EL #{managedBeanName}, which can happen by either accessing as-is in view, or by being injected as managed property of another bean, or being manually EL-resolved by e.g. Application#evaluateExpressionGet().

    In your particular case, you actually want to intialize some stuff during webapp's startup. You rather want to use ServletContextListener for this.

    @WebListener
    public class Config implements ServletContextListener {
    
        public void contextInitialized(ServletContextEvent event) {
            // Do stuff during webapp's startup.
        }
    
        public void contextDestroyed(ServletContextEvent event) {
            // Do stuff during webapp's shutdown.
        }
    
    }
    

    You could even pre-create an application scoped managed bean there whenever necessary (if your intent is to be able to access it from other beans by @ManagedProperty).

        public void contextInitialized(ServletContextEvent event) {
            event.getServletContext().setAttribute("bean", new Bean());
        }
    

    JSF stores application scoped beans as an attribute of the ServletContext and JSF won't auto-create another one when one is already present, so the one and the same as created by the above code example will be used by JSF as well.