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:用來查詢池中空閑的連接。
|
|