打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
深入Tomcat:JNDI在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";  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的名称,如下: <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:用来查询池中空闲的连接。 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JSP中tomcat的SQL Server2000数据库连接池的配置
jndi配置属性的含义
Java程序中连接池、及参数绑定实现
tomcat部署与Context
Tomcat7配置数据源
Installing JIRA on Tomcat 5.5
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服