How do I use a JDBC Realm with Tomcat and MySQL?
Author: Deron Eriksson
Description: This Tomcat tutorial describes how to configure Tomcat to use a JDBC Realm with MySQL for container-managed security.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20

Page: < 1 2 3 >

(Continued from page 1)

Next, I'll update my TomcatSW server.xml file to connect to the MySQLW realm databaseW that I just created. I'll comment out the UserDatabase realm section. I'll create a JDBCW realm entry containing all of the correct information, as shown here:

section of Tomcat's server.xml file

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
      <Realm  className="org.apache.catalina.realm.JDBCRealm"
         connectionName="realm_access" connectionPassword="realmpass"
              userTable="tomcat_users" userNameCol="user_name" userCredCol="password"
          userRoleTable="tomcat_users_roles" roleNameCol="role_name" />

After this, we need to put the MySQL jarW file to Tomcat's classpathW so that Tomcat can talk with the MySQL database. If we don't do this, we will receive an error message when Tomcat starts up, such as:

Example of exception that happens when MySQL jar file hasn't been added to Tomcat's classpath:

Jul 6, 2008 4:38:39 PM org.apache.catalina.realm.JDBCRealm start
SEVERE: Exception opening database connection
java.sql.SQLException: com.mysql.jdbc.Driver
        at org.apache.catalina.realm.JDBCRealm.start(

I'll add the mysql-connector-java-5.0.4-bin.jar file to my Tomcat's common/lib directory. When Tomcat starts up, it will find the jar file in common/lib. (Note, however, that I am going to run Tomcat through an EclipseSW project, so I will need to add the MySQL jar file to my project's classpath. More on this soon...)

MySQL jar file added to Tomcat's common/lib directory

Next, I'll configure an Eclipse project to run in Tomcat using Basic authentication against the JDBC realm that we just set up. I cover how to set up an Eclipse project to run Tomcat in another tutorial. I'll add a Content for my 'tomcat-demo' project to my Tomcat server.xml file.

<Context docBase="C:\projects\workspace\tomcat-demo\web" path="/tomcat-demo" reloadable="true"/>

My 'tomcat-demo' project's structure is shown below. The project consists of a single servletW.

tomcat-demo project

The TestServlet class is shown here. It outputs a simple message.

package test;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.println("This is the Test Servlet");


The project's web.xmlW file is shown here. It contains a <servlet> entry for the test servlet, and a <servlet-mapping> entry that maps requests to '/test' to the test servlet.

Notice the <security-constraint> section. This section specifies that any requests to the application ('/*' maps all requests) require that the requesting user be authenticated with a role of 'dude' in order to access the application. The <auth-method> specifies that we'll use Basic authentication.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="tomcat-demo" version="2.4" xmlns="" xmlns:xsi=""

			<web-resource-name>Wildcard means whole app requires authentication</web-resource-name>

			<!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->



I need to add the MySQL driver jar to my project's build path since I'll be running Tomcat from Eclipse. I'll add it as an external jar. Notice that the Tomcat jars have been added to the project using an Eclipse User Library. This is covered in another tutorial.

MySQL jar file added to project build path

(Continued on page 3)

Page: < 1 2 3 >