Setting-up Tomcat
To set-up Tomcat you usually have to edit the main configuration file, server.xml. This file is located in <tomcat-home>/conf.
To use Tomcat 4 database components with Mimer SQL you have to make sure that Tomcat can find the Mimer JDBC driver. To do this without editing any files, simply copy the JDBC driver to <tomcat-home>/common/lib.
Authentication
Tomcat supports authentication as described in the Java Servlet 2.3 and JavaServer Pages 1.2 specifications and it provides a couple of techniques to store and validate user names, passwords and group belongings.
Storing User Information
JDBCRealm is a technique for storing user information in a database. The first step to enable storing user information is to create the necessary tables.
In Mimer SQL, you can use the following SQL statements to create the necessary tables:
create table USERS(
USER_NAME CHARACTER VARYING(15),
USER_PASS CHARACTER VARYING(60),
primary key(USER_NAME)
);
create table USER_ROLES(
USER_NAME CHARACTER VARYING(15),
ROLE_NAME CHARACTER VARYING(15),
primary key(USER_NAME,ROLE_NAME)
);
Configuring Tomcat
When you have created the tables, the next step is to configure Tomcat. By default, MemoryRealm is enabled. Start by removing or commenting out MemoryRealm, for example:<!--
<Realm className="org.apache.catalina.realm.MemoryRealm"/>
-->
Add the JDBCRealm, for example:<Realm className="org.apache.catalina.realm.JDBCRealm"
debug="99"
digest="MD5"
driverName="com.mimer.jdbc.Driver"
connectionURL="jdbc:mimer://system:system@localhost/SystemDB"
userTable="users"
userNameCol="user_name"
userCredCol="user_pass"
userRoleTable="user_roles"
roleNameCol="role_name"/>
As you can see from the example above, you can define both the table names and the column names in server.xml.
The debug="99" tells the system to use debug, and digest="MD5" says that the passwords in the database should be encrypted using the MD5 algorithm. If you remove the digest attribute, the passwords can be stored in clear text.
Encrypting Passwords
There is a convenience utility to help you encrypt the passwords so you can insert them in the database. To use this, issue the command:
java -cp catalina.jar org.apache.catalina.realm.RealmBase -a MD5 password
This will print the encrypted password. Possible values for digest are MD5, SHA and MD2.
This is all that is needed to use standard security in your Web application.
Connection Pooling
The J2EE standard requires J2EE Application Servers to have a data source implementation, and Tomcat 4 conforms to this.
By default, Tomcat 4 uses a connection pool called Tyrex, but you can change that.
Setting-up a Connection Pool
To setup a connection pool using the default mechanism is quite simple. All configuration is done in the server.xml configuration file.
The following is an example of a pool:<Resource name="jdbc/MimerPool" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/MimerPool">
<parameter>
<name>user</name>
<value>HOTELADM</value>
</parameter>
<parameter>
<name>password</name>
<value>HOTELADM</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mimer.jdbc.Driver</value>
</parameter>
<parameter>
<name>driverName</name>
<value jdbc:mimer://localhost/oneWindowDB</value>
</parameter>
</ResourceParams>
Enter the above code in the web application’s context in server.xml.
Making the Data Source Available
To make the data source available to your application, you have to specify it in web.xml, the standard configuration file for your web application.
You do this as follows:<resource-ref>
<resource-ref-name>jdbc/MimerPool</resource-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Including the Pool in Your Code
When you want to use the configured pool from within your Java code you fetch a data source via JNDI and then fetch a connection from that data source. This includes three steps:- Get an initial context, for example:
Context = new InitialContext(); - Get a data source, for example:
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/MimerPool"); - Get a connection, for example:
Connection con = ds.getConnection();