0

Initial Problem

Hi,

I use a @WebListener Class to initiate a RMI connection at application deployment. That connects my JSF frontend with a backend.

That works fine!

Next I want to hand the connection to a ManagedBean because I want to use the connection to e.g. save something out of a bean, since weblistener is not accessible out of xhtml pages.

I tried to put a managedProperty into that class but I think that's not allowed. So how to do that?

@WebListener
public class Config implements ServletContextListener {

public static final String SERVER_NAMING = "xxx";
public static final String SERVER_HOST = "xxx"; 

public static FrontendCommInterface server;


public void contextInitialized(ServletContextEvent event) {
    try {

        server = (FrontendCommInterface) Naming.lookup("rmi://" + SERVER_HOST + "/" + SERVER_NAMING); 
            System.out.println("Connection successfull!");
//HERE THE SERVER SHOULD HANDED TO ANOTHER MANAGEDBEAN !!! BUT HOW TO DO THAT??? 

        } catch (MalformedURLException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (RemoteException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (NotBoundException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    }
}

public void contextDestroyed(ServletContextEvent event) {
    // Do stuff during webapp's shutdown.
} 

1 Answer 1

2

You need to create the bean and put in application scope yourself.

event.getServletContext().setAttribute("communication", new Communication(server));
3
  • I can use ManagedProperties in WebListners?
    – Sven
    Jan 9, 2011 at 12:17
  • No, definitely not. Note that I updated the answer since I misunderstood the initial problem due to lack of coffee this early morning ;)
    – BalusC
    Jan 9, 2011 at 12:23
  • No problem :-D. But in the meantime I tried to add your other advice that included postConstruct and it works :-) Thank you
    – Sven
    Jan 9, 2011 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.