Problem Setting Servlet context initialization parameters.


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();
    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.





    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(
            at javax.naming.InitialContext.getDefaultInitCtx(
            at javax.naming.InitialContext.getURLOrDefaultInitCtx(
            at javax.naming.InitialContext.lookup(
            at edu.ufl.biostat.anakin.controller.RequestProcessor.processRequest(
            at ControllerServlet.doPost(
            at ControllerServlet.doGet(
            at javax.servlet.http.HttpServlet.service(
            at javax.servlet.http.HttpServlet.service(
            at org.apache.tomcat.core.ServletWrapper.handleRequest(
            at org.apache.tomcat.core.ServletWrapper.handleRequest(
            at org.apache.tomcat.servlets.InvokerServlet.service(
            at javax.servlet.http.HttpServlet.service(
            at org.apache.tomcat.core.ServletWrapper.handleRequest(
            at org.apache.tomcat.core.ContextManager.service(
            at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(

    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.

    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.";

       // 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,

    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();
    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.