Tomcat: what is the init context params to use for making an external client connection to Tomcat 5.5 JNDI tree?

后端 未结 2 1802
灰色年华
灰色年华 2021-01-20 20:11

Currently I am using this for JBoss, but I need something also for an external Tomcat:

Properties props = new Properties();
props.put(Context.PROVIDER_URL, \         


        
2条回答
  •  生来不讨喜
    2021-01-20 20:56

    As far as I know, tomcat doe not support remote access to its JNDI tree, so you can access it only from the tomcat process. Because of that, The tomcat sets all the initialization params for the default InitialConext, and you can use it like this:

    // Obtain our environment naming context
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    
    // Look up our data source
    DataSource ds = (DataSource)
      envCtx.lookup("jdbc/EmployeeDB");
    
    // Allocate and use a connection from the pool
    Connection conn = ds.getConnection();
    ... use this connection to access the database ...
    conn.close();
    

    You can also learn more of the JNDI in tomcat in this link

提交回复
热议问题