Hi, Mark,

Moving on, I see.  Great!  Here is a bunch of junk to look at and maybe
learn from.  (I don't mind ending sentences prepositions with.)  First is
this little way to get a connection from a PostgreSQL driver using
DataSource.  I assume you will be using DataSource and not the kiddies
version of jdbc, i.e. you will be using the jdbc extension, I assume.  If
not, you should.  I don't want to bother talking about the kiddy version,
which is well-covered in Sun tutorials.  If I seem to talk about the kiddy
version with disdain, that is the correct impression. ///;-)

Once you have the industrial strength jdbc set up with DataSource, the rest
should be somewhat obvious.  There is nothing really tricky or special about
Tomcat in this regard.

What I definitely like to do is to get all the database "parameters" as you
say established with start up in Tomcat, so I put it all in a StartUp
servlet that has an init() method.  Before I go on, I will stop and ask if
you know what an init() method with a servlet does?


NOTICE THAT THE jdbcURL, etc. used in the start up servlet come from the
web.xml specification that is for the start up servlet and ServletConfig,
i.e. are for init() parameters in web.xml.

Also note the resource reference in the web.xml at the bottom.  You are
beginning to get into a bit more complicated area.  You have to include your
jar or classes for a driver just like you do any classes or jars.  Jars are
jars and classes are classes and they all are found in the same way.  I also
use Tomcat in the context of an application server, running in the same JVM,
so I have not shown a lot I do in relation to JMX specifications and MBeans
and MLET classes which pop all I show here into a JNDI context.  But, that
should not be a problem.

Please do not fail to stop and learn the DataSource and jdbc extension
stuff.  You have to get an advanced driver when you do this.  The kiddy
drivers do not work with or have DataSource implementations.  You need to do
some scouting and reading on this, if you are not already up to speed.  When
I find out how much you know about this, then I can point you better.

Micael

Below are:

   1.  A little made up class to get a DataSource connection or Connection
object.

    2.   A more developed web.xml than I have shown you before.

    3.  A StartUp servlet which is fairly sophisticated.

Have fun and let me know how it goes, if you would.  I have an interest in
seeing how you progress with all this, because we all have to train people
new at things.

Micael, again

///;-)



///////////////////////////////////////////////////

public class PostgresqlConnection {
    private Connection conn;
    private String     user;
    private String     password;

    public Connection getConnection(String user, String password) throws
ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        PostgresqlDataSource dataSource = new PostgresqlDataSource();
  conn = dataSource.getConnection(user, password);
        return conn;
    }
}

//////////////////////////////////////////////////

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";>

<web-app>
  <servlet>
   <servlet-name>authenticate</servlet-name>
   <servlet-class>AppAuthenticateServlet</servlet-class>
  </servlet>

  <servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>ActionServlet</servlet-class>
   <init-param>
     <param-name>action-mappings</param-name>
     <param-value>actions</param-value>
   </init-param>
  </servlet>

  <servlet>
   <servlet-name>setup</servlet-name>
   <servlet-class>SetupServlet</servlet-class>
   <init-param>
     <param-name>jdbcDriver</param-name>
    <param-value>org.postgresql.Driver</param-value>
   </init-param>

   <init-param>
    <param-name>jdbcURL</param-name>
    <param-value>
     jdbc:postgresql://localhost:5432/db
    </param-value>
   </init-param>

   <init-param>
    <param-name>jdbcUser</param-name>
    <param-value>db</param-value>
   </init-param>

   <init-param>
    <param-name>jdbcPwd</param-name>
    <param-value>yobyor</param-value>
   </init-param>

   <load-on-startup/>
     </servlet>

     <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
     </servlet-mapping>

     <servlet-mapping>
            <servlet-name>authenticate</servlet-name>
            <url-pattern>/authenticate</url-pattern>
     </servlet-mapping>

    <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <taglib>
        <taglib-uri>utilities</taglib-uri>
        <taglib-location>/WEB-INF/tlds/utilities.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>application</taglib-uri>
        <taglib-location>/WEB-INF/tlds/app.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>i18n</taglib-uri>
        <taglib-location>/WEB-INF/tlds/i18n.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>security</taglib-uri>
        <taglib-location>/WEB-INF/tlds/security.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>database</taglib-uri>
        <taglib-location>/WEB-INF/tlds/database.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>html</taglib-uri>
        <taglib-location>/WEB-INF/tlds/html.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>logic</taglib-uri>
        <taglib-location>/WEB-INF/tlds/logic.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>dom</taglib-uri>
        <taglib-location>/WEB-INF/tlds/dom.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>xslt</taglib-uri>
        <taglib-location>/WEB-INF/tlds/xslt.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>regions</taglib-uri>
        <taglib-location>/WEB-INF/tlds/regions.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>xpath</taglib-uri>
        <taglib-location>/WEB-INF/tlds/xpath.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>tokens</taglib-uri>
        <taglib-location>/WEB-INF/tlds/tokens.tld</taglib-location>
    </taglib>

    <resource-ref>
     <description>Primary database</description>
  <res-ref-name>PostgresDS</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>CONTAINER</res-auth>
     </resource-ref>

     <security-constraint>
      <web-resource-collection>
       <web-resource-name>Credit Card Page</web-resource-name>
       <url-pattern>/WEB-INF/jsp/createAccount/content.jsp</url-pattern>
      </web-resource-collection>
      <user-data-constraint>
       <transport-guarantee>CONFIDENTIAL</transport-guarantee>
      </user-data-constraint>
     </security-constraint>

</web-app>

////////////////////////////////////////////////////////

SETUP SERVLET
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import beans.app.User;
import beans.app.Users;
import beans.jdbc.DbConnectionPool;

public class SetupServlet extends HttpServlet implements
beans.app.Constants, tags.jdbc.Constants {
   private DbConnectionPool pool;

   public void init(ServletConfig config) throws ServletException{
      super.init(config);

      ServletContext ctx = config.getServletContext();
      createDbConnectionPool(config, ctx);

      try {
         ctx.setAttribute(USERS_KEY, loadUsers(ctx));
      } catch(SQLException ex) {
         throw new ServletException(ex);
      }
   }

   public void destroy() {
      ServletContext ctx = getServletConfig().getServletContext();
      ctx.removeAttribute(DBPOOL_KEY);
      ctx.removeAttribute(USERS_KEY);

      pool.shutdown();
      pool = null;
      super.destroy();
   }

   private void createDbConnectionPool(ServletConfig config, ServletContext
ctx) {
      pool = new DbConnectionPool(
                  config.getInitParameter("jdbcDriver"),
                  config.getInitParameter("jdbcURL"),
                  config.getInitParameter("jdbcUser"),
                  config.getInitParameter("jdbcPwd"));
      ctx.setAttribute(DBPOOL_KEY, pool);
   }

   private Users loadUsers(ServletContext ctx) throws SQLException {
      Connection conn = null;

      if(pool != null) {
         try {
            // wait for a maximum of 10 seconds for a connection
            // if pool is full
               conn = (Connection)pool.getConnection(10000);
         } catch(Exception ex) {
            throw new SQLException(ex.getMessage());
         }
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * FROM USERS");
         Users users = new Users(rs);

         pool.recycleConnection(conn);
         return users;
      }
      return null;
   }
}


-----Original Message-----
From: Mark <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Thursday, November 29, 2001 8:58 AM
Subject: Tomcat 4.0/JDBC driver configuration?


>Can someone point me in the right direction or provide an example of
>configuring an application to use JDBC under Tomcat 4.0?  I was able to use
>both thin and OCI Oracle JDCB drover connections by copying the
>classes12.zip to $CATALINA_HOME/common/lib directory and renaming it to
>classes12.jar, but that's not a good long term solution.  I found having
>the driver in the CLASSPATH doesn't work.
>
>I suspect web.xml and/or server.xml is the place to configure the
>deployment details.  Any examples or references to examples would be
>greatly appreciated.
>
>Mark
>
>
>--
>To unsubscribe:   <mailto:[EMAIL PROTECTED]>
>For additional commands: <mailto:[EMAIL PROTECTED]>
>Troubles with the list: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to