JNDI常见配置方式

<span style="font-family:SimHei;font-size:14px;"><span style="line-height: 18px;"> JNDI常见配置方式 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得我们可以用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  tomcat配置jndi有全局配置和局部配置。 </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">第一种:全局配置	</span><br style="line-height: 18px;" /><span style="line-height: 18px;">1)在tomcat的conf文件夹下的context.xml配置文件中加入: </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><Resource name="sqlconn" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">auth="Container" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  url="jdbc:sqlserver://localhost:1433;databasename=j1201" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">username="sa" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">password="sa123" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxActive="20" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxIdle="10" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxWait="10000"/> </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">2)在项目的web.xml中加入资源引用: </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><resource-ref> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  <description>JNDI DataSource</description> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  <res-ref-name>sqlconn</res-ref-name> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  <res-ref-type>javax.sql.DataSource</res-ref-type> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">  <res-auth>Container</res-auth> </span><br style="line-height: 18px;" /><span style="line-height: 18px;"></resource-ref> </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">3)jndi测试方法: </span><br style="line-height: 18px;" /><span style="line-height: 18px;">public void testJNDI() throws NamingException, SQLException{ </span><br style="line-height: 18px;" /><span style="line-height: 18px;">Context ctx = new InitialContext(); </span><br style="line-height: 18px;" /><span style="line-height: 18px;">DataSource ds = (DataSource) ctx.lookup("java:comp/env/sqlconn"); </span><br style="line-height: 18px;" /><span style="line-height: 18px;">Connection conn = ds.getConnection(); </span><br style="line-height: 18px;" /><span style="line-height: 18px;">System.out.println(conn.isClosed()); </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">} </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">4)在jsp中调用加载jndi方式,不可以直接用main方法测试,必须通过启动容器从jsp中调用: </span><br style="line-height: 18px;" /><span style="line-height: 18px;">TestPageAccessURL test = new TestPageAccessURL(); </span><br style="line-height: 18px;" /><span style="line-height: 18px;">test.testJNDI(); </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">第二种:局部配置(不推荐)。 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">1)在tomcat的server.xml的<host>标签内,添加: </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><Context path="/slesson3" docBase="/slesson3"> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">   <Resource </span><br style="line-height: 18px;" /><span style="line-height: 18px;">name="sqlconn" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">type="javax.sql.DataSource" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxIdle="2" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxWait="5000" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">username="sa" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">password="sa123" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">url="jdbc:sqlserver://localhost:1433;databasename=j1201" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxActive="4"/> </span><br style="line-height: 18px;" /><span style="line-height: 18px;"></Context> </span><br style="line-height: 18px;" /><span style="line-height: 18px;">其他配置同第一种方式。 </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">第三种:局部配置。 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">1)在项目的META-INFO下面新建context.xml。加入: </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><?xml version="1.0" encoding="UTF-8"?> </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><Context> </span><br style="line-height: 18px;" /><span style="line-height: 18px;"><Resource name="sqlconn" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">auth="Container" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">type="javax.sql.DataSource" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">url="jdbc:sqlserver://localhost:1433;databasename=j1201" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">username="sa" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">password="sa123" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxActive="20" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxIdle="10" </span><br style="line-height: 18px;" /><span style="line-height: 18px;">maxWait="10000"/>	</span><br style="line-height: 18px;" /><span style="line-height: 18px;"></Context> </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">其他配置同第一种方式。 </span><br style="line-height: 18px;" /><br style="line-height: 18px;" /><br style="line-height: 18px;" /><span style="line-height: 18px;">总结:如果要配置局部的话,推荐使用第三种方式,这样不依赖tomcat了。但是还是推荐使用第一种方式好,虽然依赖tomat,但是是全局的,而且可以配置 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">多个。对于以后切换使用方便。 </span><br style="line-height: 18px;" /><span style="line-height: 18px;">在项目的web.xml中添加的资源引用可有可无。 </span></span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值