---
title: "深入Tomcat:JNDI在TOMCAT中的应用"
author: "jiangsha"
date: "2011-11-19T17:20:56Z"
tags: []
categories: ["互联网"]
slug: "tomcat-jndi-tomcat"
draft: false
lastmod: "2012-07-03T14:59:35Z"
id: 1267211
url: "https://jiangsha.iteye.com/blog/1267211"
---

JNDI在TOMCAT中的应用 

在J2EE应用中，JNDI可以简化开发者的工作。而TOMCAT则使用户可以很方便的使用JNDI来开发自己的应用。 

**一．JNDI的配置** 

用户只需要配置TOMCAT的$CATALINA\_HOME/CONF/SERVER.XML和/WEB-INF/WEB.XML这两个文件就可以轻松的使用JNDI来进行bean的调用和数据库资源的使用了。 

首先，需要在/WEB-INF/WEB.XML中通过以下几个元素来声明自己的引用： 

1．&lt;env-entry&gt;：该值用来指定应用运行的环境入口； 

2．&lt;resource-ref&gt;：该值用来指定所引用的资源，这些资源必须是在TOMCAT中指定的，比如：JDBC datasource,JmailSession等等； 

3．&lt;resource-env-ref&gt;基本同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．&lt;environment&gt;：声明通过JNDI上下文引用的资源的名，必须要在web.xml中声明的一致；   
2．&lt;Resource&gt;：配置应用程序能够正常引用到的资源的名称和数据类型；   
3．&lt;ResourceParams&gt;：配置了所使用的资源工厂运行的类的名称或者用来配置那个资源工厂的JAVABEAN的属性；   
4．&lt;ResourceLink&gt;：增加一个在全局JNDI上下文中定义的资源的连接。   
上面几个属性的任何一个必须在&lt;Context&gt;或&lt;DefaultContext&gt;标签中定义。   
另外，需要配置在web.xml中的所有&lt;env-entry&gt;元素的名字和值最好出现在初始化上下文中，这样当环境允许时（override 属性为”true”时就会覆盖server.xml中相应的值。

**二．TOMCAT中的标准资源工厂**   
TOMCAT中提供了一系列标准的资源工厂来想应用系统提供服务，同时提供给你配置上的弹性。   
1． Javabean资源   
以下程序生成一个JAVABEAN 

 

```
package com.mycompany; 
public class MyBean { 
        private String foo = "Default Foo"; 
        private int bar = 0; 
		
        public String getFoo() { 
                return (this.foo); 
        } 

        public void setFoo(String foo) { 
               this.foo = foo; 
        } 
        
        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的名称，如下：   
&lt;resource-ref&gt;   
&lt;description&gt;   
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.   
&lt;/description&gt;   
&lt;res-ref-name&gt;   
mail/Session   
&lt;/res-ref-name&gt;   
&lt;res-type&gt;   
javax.mail.Session   
&lt;/res-type&gt;   
&lt;res-auth&gt;   
Container   
&lt;/res-auth&gt;   
&lt;/resource-ref&gt;   
以下是在程序中调用该资源的代码：   
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的资源工厂：   
&lt;Context ...&gt;   
...   
&lt;Resource name="mail/Session" auth="Container" type="javax.mail.Session"/&gt;   
&lt;ResourceParams name="mail/Session"&gt;   
&lt;parameter&gt;   
&lt;name&gt;mail.smtp.host&lt;/name&gt;   
&lt;value&gt;localhost&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;/ResourceParams&gt;   
...   
&lt;/Context&gt;

3． JDBC   
许多应用都会通过JDBC使用数据库来实现一些功能。J2EE规范要求J2EE应用去实现一个DataSource接口来实现这个目的。TOMCAT4也同样遵循这个标准，这样你在上面开发的数据库应用就可以不用修改的在其他j2ee平台上使用。   
首先看看在web.xml中的配置：   
&lt;resource-ref&gt;   
&lt;description&gt;   
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.   
&lt;/description&gt;   
&lt;res-ref-name&gt;   
jdbc/EmployeDB   
&lt;/res-ref-name&gt;   
&lt;res-type&gt;   
javax.sql.DataSource   
&lt;/res-type&gt;   
&lt;res-auth&gt;   
Container   
&lt;/res-auth&gt;   
&lt;/resource-ref&gt;

下面是对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中的配置：   
&lt;Context ...&gt;   
...   
&lt;Resource name="jdbc/EmployeeDB" auth="Container"   
type="javax.sql.DataSource"/&gt;   
&lt;ResourceParams name="jdbc/EmployeeDB"&gt;   
&lt;parameter&gt;   
&lt;name&gt;username&lt;/name&gt;   
&lt;value&gt;dbusername&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;parameter&gt;   
&lt;name&gt;password&lt;/name&gt;   
&lt;value&gt;dbpassword&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;parameter&gt;   
&lt;name&gt;driverClassName&lt;/name&gt;   
&lt;value&gt;org.hsql.jdbcDriver&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;parameter&gt;   
&lt;name&gt;url&lt;/name&gt;   
&lt;value&gt;jdbc:HypersonicSQL:database&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;parameter&gt;   
&lt;name&gt;maxActive&lt;/name&gt;   
&lt;value&gt;8&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;parameter&gt;   
&lt;name&gt;maxIdle&lt;/name&gt;   
&lt;value&gt;4&lt;/value&gt;   
&lt;/parameter&gt;   
&lt;/ResourceParams&gt;   
...   
&lt;/Context&gt;

参数说明：   
driveClassName：JDBC驱动类的完整的名称；   
maxActive：同时能够从连接池中被分配的可用实例的最大数；   
maxIdle：可以同时闲置在连接池中的连接的最大数；   
maxWait：最大超时时间，以毫秒计；   
password：用户密码；   
url：到JDBC的URL连接；   
user：用户名称；   
validationQuery：用来查询池中空闲的连接。