Problem Setting Servlet context initialization parameters.

Discussions

Web tier: servlets, JSP, Web frameworks: Problem Setting Servlet context initialization parameters.

  1. I am obtaining a reference to a session ejb from a servlet. I originally hardcoded the servlet context initialization parameters in my web-tier code i.e.

    Hashtable props = new Hashtable();
    props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
    props.put(Context.PROVIDER_URL,"localhost:1099");
    InitialContext ctx = new InitialContext(props);

    As you can see, I hard-coded the actual parameters "org.jnp.interfaces.NamingContextFactory" and "localhost:1099" into my servlet so that the servlet could look up a particular session bean via JNDI. No problem, it worked fine. I then learned that I could place these parameters into web.xml i.e.

    <web-app>

    ...

        <context-param>
          <param-name>java.naming.factory.initial</param-name>
    <param-value>org.jnp.interfaces.NamingContextFactory</param-value>
        </context-param>
        <context-param>
          <param-name>java.naming.provider.url</param-name>
          <param-value>localhost:1099</param-value>
        </context-param>
    ...

    </webapp>

    At least according to web.dtd I should be able to do this (see below).

    <!ELEMENT web-app (icon?, display-name?, description?, distributable?,
    context-param*, servlet*, servlet-mapping*, session-config?,
    mime-mapping*, welcome-file-list?, error-page*, taglib*,
    resource-ref*, security-constraint*, login-config?, security-role*,
    env-entry*, ejb-ref*)>

    <!-- The context-param element contains the declaration of a web
    application's servlet context initialization parameters. -->

    <!ELEMENT context-param (param-name, param-value, description?)>

    <!-- The param-name element contains the name of a parameter. -->

    <!ELEMENT param-name (#PCDATA)>

    <!-- The param-value element contains the value of a parameter. -->

    <!ELEMENT param-value (#PCDATA)>

    After editing web.xml I restarted the server. When I made a request against the application (again, which worked flawlessly prior to changing web.xml), I received the following error:

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
            at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:646)
            at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
            at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:283)
            at javax.naming.InitialContext.lookup(InitialContext.java:350)
            at edu.ufl.biostat.anakin.controller.RequestProcessor.processRequest(RequestProcessor.java:95)
            at ControllerServlet.doPost(ControllerServlet.java:50)
            at ControllerServlet.doGet(ControllerServlet.java:37)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
            at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:597)
            at org.apache.tomcat.servlets.InvokerServlet.service(InvokerServlet.java:257)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
            at org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
            at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
            at org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
            at java.lang.Thread.run(Thread.java:484)

    It's as though my context initialization parameters aren't being set at all when I try to set them via web.xml. What am I doing wrong here? Any ideas would be greatly appreciated.

    Thanks,
    Patrick Garner
  2. Patrick,

    I'm wondering if you need to cast the property back to the correct object type. something like this:

    // When attempting to connect to JNDI, we store the reference to the
       // initial JNDI context in this variable. We will use it to lookup the
       // entity bean.
       Context context = null;

       try {
          context = new InitialContext();
       } catch (Exception e) {
          exception = e;
          error = "Caught \"" + exception.getClass().getName() + "\" while " +
                  "attempting to create the initial JNDI context.";
          errorStream.println(error);
          exception.printStackTrace(errorStream);
       }

       // We have specified "ejb/test" in the web.xml file as the name by
       // which we would like to contact the home interface. We will
       // have to prepend "java:comp/env/", the root `directory' for enterprise
       // beans.
       final String location = "java:comp/env/ejb/Test";
       if (error == null) {
          try {
             // Attempt to lookup an object at the specified location in the JNDI
             // context.
             Object boundObject = context.lookup(location);

             // Try to convert it to an instance of Test, the home
             // interface for our bean.
             home = (TestHome) PortableRemoteObject.narrow(boundObject,
                                                               TestHome.class);
    ...
    ...
    ...

    Not exactly what you are doing but it might help....
  3. Hi
    Although u have specified in the web.xml, u will still need to retrieve it from the context of the web application. Not to be confused with the InitialContext.

    Here is the code snippet(in init method of servlet, where sc is the ServletConfig)

    ServletContext sCtx = sc.getServletContext();

    // attempt to retrieve from context init parameters
    String jndiFactory = sCtx.getInitParameter("java.naming.factory.initial");
    String jndiUrl = sCtx.getInitParameter("java.naming.provider.url");

    Hashtable props = new Hashtable();
    props.put(Context.INITIAL_CONTEXT_FACTORY,jndiFactory);
    props.put(Context.PROVIDER_URL,jndiUrl);
    InitialContext ctx = new InitialContext(props);

    Alternatively, u may wish to specify in servlet init parameters. The code will be somewhat similar.

    Hope this helps.
  4. Hi again

    Just to add on, if u are using jboss, the following files will need to be in your WEB-INF/lib directory or in classpath.
    jboss-client.jar,
    jbosssx-client.jar
    jnp-client.jar

    http://www.jboss.org/documentation/HTML/ch04s11.html