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" "http://jetty.mortbay.org/configure.dtd">
<!—Configure the first WEB application -->
<Configure>
  <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>
  </New>
  <!—Configure the second environment variable  -->
  <New id=" JNDN_Test2">
    <Arg> JNDN_Test2</Arg>
    <Arg type="boolean">true</Arg>
  </New>
  <!—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>
  </New>
  <!—binding Jetty JNDI datasource to jdbc/mydatasource-->
  <New id="mydatasource">
    <Arg>jdbc/mydatasource</Arg>
    <Arg><Ref id="ds"/></Arg>
  </New>
</Configure>

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">
<html>
  <head>
    <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">
  </head>
  <body>
    <form method="post" action="aa" name="f1"><p>&nbsp;<input type="submit" value="test" name="button1"></p></form>
  </body>
</html>

(2). Here is TestServlet.java source code:

TestServlet.java
package asjava;
import java.io.IOException;
import java.io.PrintStream;
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()
    {
        super.destroy();
    }
    protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        PrintStream out = new PrintStream(response.getOutputStream());
        try
        {
            out.println(ic.lookup("JNDN_Test1"));
            out.println(ic.lookup("JNDN_Test2"));
            DataSource ds = (DataSource)ic.lookup("jdbc/mydatasource");
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            for(ResultSet rs = stmt.executeQuery("select * from echo_message"); rs.next(); out.println(rs.getString(“id”)));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void init(ServletConfig config)
        throws ServletException
    {
        super.init(config);
        try
        {
            ic = new InitialContext();
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }
    }
}

(3). Here is web.xml code.

WEB.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>TestServlet</servlet-name>
  <servlet-class>asjava.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>TestServlet</servlet-name>
  <url-pattern>/aa</url-pattern>
</servlet-mapping>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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.

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

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

*

code