Tomcat
The following will demonstrate how to create resources on the server so that the distributed war file can use JNDI lookups. This allows a customer to specify their own settings and allows the developer to distribute new war files without extra configuration. Tested and working in Tomcat 5.5 and SDK 1.5
JDBC
In server.xml (located at TOMCAT_DIR/conf), add the following within the <GlobalNamingResources> tag. Note that the example below is for a MySQL resource, but you can change the highlighted parameters to apply it to any kind of database server.
<!-- JDBC resource -->
<Resource name="jdbc/database" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="15" maxWait="10000" removeAbandoned="true" removeAbandonedTimeout="300"
logAbandoned="true" driverClassName="com.mysql.jdbc.Driver"
username="username" password="password"
url="jdbc:mysql://localhost:3306/database?autoReconnect=true" />
In your application web.xml, add the following within the <web-app> tag.
<resource-ref> <description>JDBC Connection</description> <res-ref-name>jdbc/database</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
In your application context.xml, add the following within the <Context> tag.
<ResourceLink name="jdbc/database" global="jdbc/database" type="javax.sql.DataSource"/>
In your java code, add something along the lines of (looking up the context everytime you want a connection is far from optimal, but it will give you a working start point)
public Connection getConnection() throws NamingException, SQLException {
InitialContext context = new InitialContext();
DataSource ds = (DataSource)context.lookup( "java:comp/env/jdbc/database" );
Connection connection = ds.getConnection();
return connection;
}
For all of this to work the server needs to have access to the required jar files to create the connection (MySQL in this case) so you will have to add an equivalent JDBC jar file to the common libraries directory, TOMCAT_DIR/common/lib. For this example I used mysql-connector-java-3.1.11-bin.jar
JavaMail
In server.xml (located at TOMCAT_DIR/conf), add the following within the <GlobalNamingResources> tag..
<!-- javamail session --> <Resource name="mail/session" auth="Container" type="javax.mail.Session" mail.smtp.host="localhost" mail.smtp.port="25" />
It is sometimes necessary to add mail.smtp.localhost="localhost" into the above entry if your hostname is not configured correctly on the host machine.
In your application web.xml, add the following within the <web-app> tag.
<resource-ref> <description>JavaMail Session</description> <res-ref-name>mail/session</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> </resource-ref>
In your application context.xml, add the following within the <Context> tag.
<ResourceLink name="mail/session" global="mail/session" type="javax.mail.Session"/>
In your java code, add something along the lines of (looking up the context everytime you want a session is far from optimal, but it will give you a working start point)
public Session getMailSession() throws NamingException {
InitialContext context = new InitialContext();
Session mailSession = (Session)context.lookup( "java:comp/env/mail/session" );
return mailSession;
}
public void sendMail( String toAddress, String fromAddress, String subject, String message )
throws NamingException, MessagingException {
Session mailSession = getMailSession();
MimeMessage msg = new MimeMessage( mailSession );
msg.setRecipients( Message.RecipientType.TO,
InternetAddress.parse( toAddress ) );
msg.setFrom( new InternetAddress( fromAddress ) );
msg.setSubject( subject );
msg.setText( message );
Transport.send( msg );
}
For all of this to work the server needs to have access to the required jar files to create the session, so you will have to add the following to the common libraries directory, TOMCAT_DIR/common/lib - activation.jar and mail.jar