Java EE 6 brings an easy and portable way to get the module name and application name of the current app, using
@Resource field injection or lookup:
@Resource(lookup="java:module/ModuleName")The above code can be in any Java EE component classes, such as servlet, servlet filter, web listener class, JSP tag handler class, JSF managed bean, EJB bean class, interceptor class, application client main class, and their super classes as well.
private String moduleName;
private String appName;
You can also look up the module name and app name by their reserved jndi names:
//look up inside a methodIf you only need moduleName or appName in a particular method, I feel looking them up on-demand is slightly better.
String mname = (String) initialContext.lookup("java:module/ModuleName");
String anmae = (String) initialContext.lookup("java:app/AppName");
From Java EE 6 forward, the default module name is the base name of a WAR, ejb-jar, or application client jar, that is, the jar name without the .war and .jar extension. One can customize it in descriptors (web.xml, ejb-jar.xml, application-client.xml), though this is rarely needed.
The default app name is the basename of a EAR, i.e., the EAR name without the .ear extension. One can customize it in application.xml.