0 Replies Latest reply on Aug 15, 2011 11:42 AM by Shelley Baker

    Web Context Root Lookup

    Shelley Baker Newbie

      I'm trying to lookup the web context root URL from JNDI per the Servlet 3.0 Specification:

      15.2.3 JNDI Name for Web Module Context Root URL

      The name of the pre-defined java.net.URL resource for the context root of a web application has the following syntax:

       

      java:global[/<app-name>]/<module-name>!ROOT in the global namespace
      and

      java:app/<module-name>!ROOT in the application-specific namespace.

      My code is as follows:

      @WebServlet("/name")
      public class NameServlet extends HttpServlet {
      
          @Resource(lookup = "java:module/ModuleName")
          private String moduleName;
      
          @Resource(lookup = "java:app/AppName")
          private String appName;
      
          private static final long serialVersionUID = -2578528221134310703L;
      
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
              IOException {
      
              final PrintWriter writer = resp.getWriter();
              writer.format("AppName = %s", appName);
              writer.println();
              writer.format("ModuleName = %s", moduleName);
              writer.println();
              writer.format("App ModuleURL = %s", getModuleUrl(moduleName));
              writer.println();
              writer.format("Global ModuleURL = %s", getModuleUrl(appName, moduleName));
      
          }
      
          private static URL getModuleUrl(String moduleName) {
              try {
                  final InitialContext ctx = new InitialContext();
                  // java:app/<module-name>!ROOT
                  return (URL)ctx.lookup("java:app/" + moduleName + "!ROOT");
              } catch (NamingException e) {
                  e.printStackTrace();
                  return null;
              }
          }
      
          private static URL getModuleUrl(String appName, String moduleName) {
              try {
                  final InitialContext ctx = new InitialContext();
                  // java:global[/<app-name>]/<module-name>!ROOT
                  return (URL)ctx.lookup("java:global/" + appName + "/" + moduleName + "!ROOT");
              } catch (NamingException e) {
                  e.printStackTrace();
                  return null;
              }
          }
      }
      

       

      According to the spec and the examples therein, the code appears to be correct; however, I have attempted to run it on JBoss AS 7.0.0.Final and JBoss 6.0.0.Final, but it doesn't work. Note that the module name and application name are correctly looked up, but a NamingException is thrown when it attempts to lookup either form of the context root JNDI path (the ctx.lookup() calls from each of the getModuleUrl methods). This issue seems to indicate that this should have been fixed in JBoss 6. Is my code wrong, or does JBoss not implement this correctly?