深入Tomcat:JNDI在TOMCAT中的应用

 说句实话,在J2EE应用中很少用Tomcat得,一般都用Weblogic,Jboss,不过在网上发现了这篇文章,对于想对Tomcat做深入研究的朋友的确不错!
JNDI在TOMCAT中的应用
在J2EE应用中,JNDI可以简化开发者的工作。而TOMCAT则使用户可以很方便的使用JNDI来开发自己的应用。
一.JNDI的配置
用户只需要配置TOMCAT的$CATALINA_HOME/CONF/SERVER.XML和/WEB-INF/WEB.XML这两个文件就可以轻松的使用JNDI来进行bean的调用和数据库资源的使用了。
首先,需要在/WEB-INF/WEB.XML中通过以下几个元素来声明自己的引用:
1.<env-entry>:该值用来指定应用运行的环境入口;
2.<resource-ref>:该值用来指定所引用的资源,这些资源必须是在TOMCAT中指定的,比如:JDBC datasource,JmailSession等等;
3.<resource-env-ref>基本同resource-ref,但不需要增加一个权限的控制参数。
然后,用户的应用配置完成以后,将被发布到TOMCAT,此时,所有指定的资源都被放JNDI名字空间的java:comp/env位置,用户可以在自己的应用程序中通过以下的代码取得:
// 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();
最后,用户需要在server.xml中配置JNDI的资源信息,该配置通过对以下几个参数的赋值来实现:
1.<environment>:声明通过JNDI上下文引用的资源的名,必须要在web.xml中声明的一致;
2.<Resource>:配置应用程序能够正常引用到的资源的名称和数据类型;
3.<ResourceParams>:配置了所使用的资源工厂运行的类的名称或者用来配置那个资源工厂的JAVABEAN的属性;
4.<ResourceLink>:增加一个在全局JNDI上下文中定义的资源的连接。
上面几个属性的任何一个必须在<Context>或<DefaultContext>标签中定义。
另外,需要配置在web.xml中的所有<env-entry>元素的名字和值最好出现在初始化上下文中,这样当环境允许时(override 属性为”true”时就会覆盖server.xml中相应的值。

二.TOMCAT中的标准资源工厂
TOMCAT中提供了一系列标准的资源工厂来想应用系统提供服务,同时提供给你配置上的弹性。
1. Javabean资源
以下程序生成一个JAVABEAN
package com.mycompany;
public class MyBean {
private String foo = "Default Foo";
public String getFoo() {
return (this.foo);
}
public void setFoo(String foo) {
this.foo = foo;
}
private int bar = 0;
public int getBar() {
return (this.bar);
}
public void setBar(int bar) {
this.bar = bar;
}
}
以下是在web.xml中的配置:
<resource-env-ref>
<description>
Object factory for MyBean instances.
</description>
<resource-env-ref-name>
bean/MyBeanFactory
</resource-env-ref-name>
<resource-env-ref-type>
com.mycompany.MyBean
</resource-env-ref-type>
</resource-env-ref>
以下是在应用系统中调用这个资源的代码:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
writer.println("foo = " + bean.getFoo() + ", bar = " + bean.getBar());
以下是配置TOMCAT的资源工厂:
<Context ...>
...
<Resource name="bean/MyBeanFactory" auth="Container"
type="com.mycompany.MyBean"/>
<ResourceParams name="bean/MyBeanFactory">
<parameter>
<name>factory</name>
<value>org.apache.naming.factory.BeanFactory</value>
</parameter>
<parameter>
<name>bar</name>
<value>23</value>
</parameter>
</ResourceParams>
...
</Context>

2. JavaMail Sessions
在WEB应用中,发送电子邮件和消息是系统功能的不好或却的一部分,而JavaMail就让这一部分的开发变的直截了当,但却需要很多细节上的配置,来告知系统一些必要的参数(比如:SMTP等)。
TOMCAT包括了一个可以生成javax.mail.Session实例的标准资源工厂,并且被生成的实例已经和一个配置在server.xml中的SMTP相连接。这样,应用就可以完全和邮件服务器脱离关系,只是简单的接收,发送和配置消息。
首先在需要在web.xml声明你需要预配置的JNDI的名称,如下:
<resource-ref>
<description>
Resource reference to a factory for javax.mail.Session
instances that may be used for sending electronic mail
messages, preconfigured to connect to the appropriate
SMTP server.
</description>
<res-ref-name>
mail/Session
</res-ref-name>
<res-type>
javax.mail.Session
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
以下是在程序中调用该资源的代码:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getParameter("from"));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(request.getParameter("to"));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(request.getParameter("subject"));
message.setContent(request.getParameter("content"), "text/plain");
Transport.send(message);

以下就是在server.xml中配置TOMCAT的资源工厂:
<Context ...>
...
<Resource name="mail/Session" auth="Container" type="javax.mail.Session"/>
<ResourceParams name="mail/Session">
<parameter>
<name>mail.smtp.host</name>
<value>localhost</value>
</parameter>
</ResourceParams>
...
</Context>

3. JDBC
许多应用都会通过JDBC使用数据库来实现一些功能。J2EE规范要求J2EE应用去实现一个DataSource接口来实现这个目的。TOMCAT4也同样遵循这个标准,这样你在上面开发的数据库应用就可以不用修改的在其他j2ee平台上使用。
首先看看在web.xml中的配置:
<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>
jdbc/EmployeDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>

下面是对JNDI的应用的代码:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

在server.xml中的配置:
<Context ...>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/EmployeeDB">
<parameter>
<name>username</name>
<value>dbusername</value>
</parameter>
<parameter>
<name>password</name>
<value>dbpassword</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.hsql.jdbcDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:HypersonicSQL:database</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>8</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>4</value>
</parameter>
</ResourceParams>
...
</Context>

参数说明:
driveClassName:JDBC驱动类的完整的名称;
maxActive:同时能够从连接池中被分配的可用实例的最大数;
maxIdle:可以同时闲置在连接池中的连接的最大数;
maxWait:最大超时时间,以毫秒计;
password:用户密码;
url:到JDBC的URL连接;
user:用户名称;
validationQuery:用来查询池中空闲的连接。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值