A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余宏 中级黑马   /  2012-5-18 16:12  /  1980 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Java中的tomcat ,JNDI的配置方式有哪些呢?使用哪种比较好??

2 个回复

倒序浏览

tomcat中配置JNDI有全局配置和局部配置。大致的有以下两种配置方式:

第一种:全局配置。
1)在tomcat的conf文件夹下的context.xml配置文件中加入:

[html] view plaincopyprint?<Resource name="jndi/mybatis"   
            auth="Container"   
            type="javax.sql.DataSource"   
            driverClassName="com.mysql.jdbc.Driver"   
            url="jdbc:mysql://localhost:3306/appdb"   
            username="root"   
            password="123456"   
            maxActive="20"   
            maxIdle="10"   
            maxWait="10000"/>      
        <Resource name="jndi/mybatis"
                                auth="Container"
                                type="javax.sql.DataSource"
                                driverClassName="com.mysql.jdbc.Driver"
                                url="jdbc:mysql://localhost:3306/appdb"
                                username="root"
                                password="123456"
                                maxActive="20"
                                maxIdle="10"
                                maxWait="10000"/>         

2)在项目的web.xml中加入资源引用:

[html] view plaincopyprint?<resource-ref>  
  <description>JNDI DataSource</description>  
  <res-ref-name>jndi/mybatis</res-ref-name>  
  <res-ref-type>javax.sql.DataSource</res-ref-type>  
  <res-auth>Container</res-auth>  
</resource-ref>  
<resource-ref>
  <description>JNDI DataSource</description>
  <res-ref-name>jndi/mybatis</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

其中res-ref-name值要和context.xml的name值一致。

3)jndi测试方法:

[java] view plaincopyprint?public void testJNDI() throws NamingException, SQLException{  
    Context ctx = new InitialContext();  
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");  
    Connection conn = ds.getConnection();  
    System.out.println(conn.isClosed());  
  
}  
        public void testJNDI() throws NamingException, SQLException{
                Context ctx = new InitialContext();
                DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");
                Connection conn = ds.getConnection();
                System.out.println(conn.isClosed());

        }

4)在jsp中调用加载jndi方式,不可以直接用main方法测试,必须通过启动容器从jsp中调用:

[java] view plaincopyprint?TestPageAccessURL test = new TestPageAccessURL();  
test.testJNDI();  
                TestPageAccessURL test = new TestPageAccessURL();
                test.testJNDI();



第二种:局部配置。

1)在项目的META-INFO下面新建context.xml。加入:

[html] view plaincopyprint?<?xml version="1.0" encoding="UTF-8"?>  
<Context>  
    <Resource name="jndi/mybatis"   
                auth="Container"   
                type="javax.sql.DataSource"   
                driverClassName="com.mysql.jdbc.Driver"   
                url="jdbc:mysql://localhost:3306/appdb"   
                username="root"   
                password="123456"   
                maxActive="20"   
                maxIdle="10"   
                maxWait="10000"/>      
</Context>  

具体需要那种配置方式,看楼主的需求了
回复 使用道具 举报
{:soso_e179:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马