Question

I am trying to get the server URL (eg. http://www.mywebapp.com/myapp) from the ServletContext when the application starts up, I am doing this by invoking a bean method on startup (using @Startup) and getting the servlet context,

@Startup
@Name("startupActions")
@Scope(ScopeType.APPLICATION)
public class StartupActionsBean implements StartupActions,
Serializable {

@Logger private Log log;

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Create
@Override
public void create(){
    ServletContext sc = org.jboss.seam.contexts.ServletLifecycle.getServletContext();
    String context = sc.getContextPath();
    String serverInfo = sc.getServerInfo();
    log.debug("__________________START__________________");
    log.debug("Context Path: "+context);
    log.debug("Server Info: "+serverInfo);
}

// Cleanup methods
@Remove
@BypassInterceptors
@Override
public void cleanUp(){}
}

This work ok, however the ServletContext path is blank, see console output below..

18:52:54,165 DEBUG [uk.co.app.actions.startup.StartupActionsBean] __________________START__________________
18:52:54,165 DEBUG [uk.co.app.actions.startup.StartupActionsBean] Context Path: 
18:52:54,165 DEBUG [uk.co.app.actions.startup.StartupActionsBean] Server Info: JBoss Web/3.0.0-CR1

Does anyone know how to get the contextpath through this, or other means?

ps. using SEAM 2.2.2, Jboss AS6 Final, Richfaces 3.3.3

Was it helpful?

Solution

Don't use @Startup, your component startup code gets called before contexts are fully setup and some other factories could not be yet initialized.

Observe the org.jboss.seam.postInitialization event and use the same ServletLifecycle.getCurrentServletContext() to get hold of the data needed. A quick example:

@Name("contextPath")
public class ContextPathInit implements Serializable {
    private String contextPath;

    @Observer("org.jboss.seam.postInitialization")
    public void init() {
        contextPath = ServletLifecycle.getCurrentServletContext().getContextPath();
    }
}

OTHER TIPS

Have you tried getting it from ExternalContext using the getContextName method?

FacesContext.getCurrentInstance().getExternalContext().getContextName()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top