Servlet DataSource Resource Injection in Tomcat

20 August 2012 By Nithya Vasudevan 14,214 views 6 Comments
9 Flares Twitter 1 Facebook 1 Google+ 7 9 Flares ×

Project Description

  • In the previous Servlet example, we configured datasource in Apache Tomcat and used JNDI look up.
  • This Servlet example explains how to configure datasource in Apache Tomcat and inject it in Servlet using @Resource annotation.
  • For simplicity, all the JDBC codes and HTML responses are coded in the Servlet.
  • Refer this example which explains how to use Datasource in Servlet using MVC, Post-Redirect-Get (PRG), DAO, TO, Singleton patterns and creating user-defined application exception used by DAO methods

Environment Used

  • JDK 6 (Java SE 6) (To install JDK – Windows , Ubuntu)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1) (To install Eclipse, refer this page)
  • Apache Tomcat 6.x (To install Tomcat refer this page)
  • MySQL 5.5 (To install MySQL refer this page)
  • MySQL Connector/J 5.1 JAR file
  • Java EE 5 API (Servlet 2.5)
  • Java Database Connectivity (JDBC) API (java.sql.*, javax.sql.*)
  • [Optional] For monitoring and analyzing HTTP headers between the browser and web servers, you can use one of these add-ons of Firefox
    • Live HTTP Headers
    • HttpFox


Create Database and table in MySQL

This example uses one table ‘Employee‘ and the description of the table is shown below.

emp_idintPrimary Keyauto_increment
  • Open command prompt (Windows) or Terminal(Linux) and type
    mysql –u [your-username] –p
    and press enter and type the password.
  • If you are using Windows, you can also use MySQL command line client which will be available in All programs menu.
  • For creating a new database, refer this page. In this example, the database name is ‘exampledb‘.
  • After creating the database type the command “use <database_name>;”
  • For creating a new table, refer this page. In this example, the table name is ‘employee
  • Insert some records in the table. Refer this page for inserting rows using MySQL.

Create Dynamic Web Project

  • Open Eclipse IDE
  • For writing Servlet and JSP, we need to create a new Dynamic Web project. Create this project and name it as “ServletDSResInjection“.

Download MySQL connector

  • The connector can be downloaded from:
  • This tutorial uses 5.1 version. Unzip the connector to a safe location on your computer which contains MySQL Connector J JAR.
  • Copy the MySQL Connector J JAR and paste it inside project’s ‘lib’ folder.

Configure Context

  • Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context in context.xml.
  • This xml file contains context information and can be placed in one of the location as explained below,
    • In the /conf/context.xml file
    • In the /conf/[enginename]/[hostname]/context.xml.
    • META-INF/context.xml inside the application.

We place this file in appliction’s META-INF folder.
Create a new XML file in META-INF folder and copy the following content.

<?xml version="1.0" encoding="UTF-8"?>
 <!-- The contents of this file will be loaded for each web application -->
  <Context crossContext="true">

	<!-- Default set of monitored resources -->
   <Resource name="jdbc/testDB" auth="Container" 
		maxActive="100" maxIdle="30" maxWait="10000"

Fill the username and password for your database and enter your database name in the url string.

Create Servlet

Create a new Servlet “” in the package “com.theopentutorials.servlets” and copy the following code.

Here, instead of doing JNDI lookup for the JDBC datasource, we use resource injection using @Resource annotaion and store it in DataSource object.

package com.theopentutorials.servlets;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Resource(name = "jdbc/testDB")
	DataSource ds;

	public EmployeeServlet() {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Connection con = ds.getConnection();

			Statement stmt = con.createStatement();
			String query = "select * from employee";
			ResultSet rs = stmt.executeQuery(query);

			PrintWriter out = response.getWriter();
			out.print("<center><h1>Employee Details</h1></center>");
			out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
			out.print("<tr><th>Employee ID</th>");
			out.print("<th>Employee Name</th>");

			while ( {
				out.print("<td>" + rs.getInt("emp_id") + "</td>");
				out.print("<td>" + rs.getString("emp_name") + "</td>");
				out.print("<td>" + rs.getDouble("salary") + "</td>");
				out.print("<td>" + rs.getString("dept_name") + "</td>");
		} catch (SQLException e) {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
  • When the application is deployed in the tomcat container, it looks up the JDBC resource and injects into the Servlet.

Configure web.xml

A resource reference is an element in a deployment descriptor that identifies the component’s coded name for the resource. More specifically, the coded name references a connection factory for the resource. In the example in the following section, the resource reference name is “jdbc/testDB“.
Now create a WEB-INF/web.xml for this application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
	/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <description>DB Connection</description>


Deploy the project in Server and start/restart the server.

Use Ctrl + F11 in Eclipse to run the Servlet. The URL for requesting the Servlet is

Folder Structure

The complete folder structure of this example is shown below.

Program control flow

  • When the application is deployed in the container, it looks up JDBC resource (jdbc/testDB) in global JNDI namespace and injects into Servlet’s DataSource object (ds).
  • Web client (browser) sends requests to Servlet through request URL. Since the request is sent directly to Servlet it is a HTTP GET Request.
  • Servlet in doGet() method, creates Connection from injected Datasource object and executes SQL statement and responds with actual HTML content.

The content of HTTP request and response headers can be monitored using browser add-ons such as Live HTTP Headers, HttpFox, etc

Tags: , , , , , , , , , ,

  • Swaroop


    Thank you for the wonderful tutorial.
    The previous example as mentioned above runs fine.
    But where as the above example is giving me the following error.
    Am using Oracle Database.I believe there is some problem while injecting the resource.Do we need to re-initialize the context again.Please suggest.

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class ‘oracle.jdbc.driver.OracleDriver’
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(
    at com.tutorial.dsinjection.EmployeeServlet2.doGet(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.coyote.http11.Http11Processor.process(
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

    • Praveen Macherla

      Include oracle jdbc jar file in web-inf/lib folder.

      • Swaroop


        Thank you for the response.
        I have included the ojdbc jar in WEB-INF/lib.
        The only change is I have renamed the file as EmployeeServlet2(in web.xml also).
        The first example is running fine with ojdbc jar included.
        Please suggest.

  • Swaroop


    The example worked by copying the ojdbc jars in TOMCAT/WEB-INF/lib.

  • Frank

    I would suggest to close the Connection in a finally Block

  • Pingback: Java | ravisankar1