Setting up Jetty JNDI Datasource Binding

This tutorial is to set up Jetty JNDI datasource for DBCP(Connection pooling), at beginning, please copy Mysql driver library(we used MYSQL as  database) and required DBCP jars to the directory “lib” of the jetty rootDir. 3 main DBCP jars are required as below.

  • Commons-dbcp.jar
  • Commons-pool.jar
  • Commons-collections.jar

1. Create JNDI.xml under directory “contexts” of Jetty root directory.

In order to increase readability you may use name of JNDI.xml as the same as your project, Here is JNDI.xml contents:

<?xml version="1.0"  encoding="GB2312"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<!—Configure the first WEB application -->
  <Set name="contextPath">/JNDI</Set>
  <Set name="resourceBase">E:/StartPortableApps/jspTest</Set>
  <!—Configure the first environment variable  -->
  <New id="JNDN_Test1">
    <Arg> JNDN_Test1</Arg>
    <Arg type="java.lang.Integer">4000</Arg>
  <!—Configure the second environment variable  -->
  <New id=" JNDN_Test2">
    <Arg> JNDN_Test2</Arg>
    <Arg type="boolean">true</Arg>
  <!—setting up the Jetty JNDI datasource -->
  <New id="ds">
    <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
    <Set name="url">jdbc:mysql://localhost:3306/test</Set>
    <Set name="username">root</Set>
    <Set name="password"> </Set>
    <Set name="maxActive" type="int">100</Set>
    <Set name="maxIdle" type="int">30</Set>
    <Set name="maxWait" type="int">1000</Set>
    <Set name="defaultAutoCommit" type="boolean">true</Set>
    <Set name="removeAbandoned" type="boolean">true</Set>
    <Set name="removeAbandonedTimeout" type="int">60</Set>
    <Set name="logAbandoned" type="boolean">true</Set>
  <!—binding Jetty JNDI datasource to jdbc/mydatasource-->
  <New id="mydatasource">
    <Arg><Ref id="ds"/></Arg>

In above of setting up the Jetty JNDI datasource segment, which are used to DBCP(Connection pooling) support and a list parameters you need to configure out. The driverClassName specifics the database driver class name, the url specifics the connected database url, which are consist of hostname, port and database name and url pattern. The maxActive is specifics how many seconds the connection should keep active without new connected.

2. Here is jsp and servletcode for testing jetty JNDI.

(1). please create index.jsp under E:/StartPortableApps/jspTest(JNDI.xml has specified the absolute path)

<%@ page language="java" pageEncoding="GB2312"%>
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <form method="post" action="aa" name="f1"><p>&nbsp;<input type="submit" value="test" name="button1"></p></form>

(2). Here is source code:
package asjava;
import java.sql.*;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.sql.DataSource;
public class TestServlet extends HttpServlet
    InitialContext ic;
    public TestServlet()
    public void destroy()
    protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
        PrintStream out = new PrintStream(response.getOutputStream());
            DataSource ds = (DataSource)ic.lookup("jdbc/mydatasource");
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            for(ResultSet rs = stmt.executeQuery("select * from echo_message");; out.println(rs.getString(“id”)));
        catch(Exception e)
    public void init(ServletConfig config)
        throws ServletException
            ic = new InitialContext();
        catch(Exception e)
            throw new ServletException(e);

(3). Here is web.xml code.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>

Jetty is an open source and available for commercial and free use, which provides an HTTP server, HTTP client.  These instructions apply to releases of jetty-6.1.12rc3 and above, and of jetty-7.0.0.pre4 and above, We have completed this Set up Jetty JNDI datasources binding tutorial and example, and all source is tested pass.

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *