You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Przemyslaw Kowalczyk <pr...@engine.com.pl> on 2002/07/02 12:08:48 UTC

Tomcat 4.0.4, jndi, jdbc and postgresql [long]

Hi

I've just started to write servlets. Servlets that only do processing, without 
connecting to database work fine, but I have some problems to get the 
connection with postgresql via jdbc work.

Tomcat: 4.0.4	(on linux)
Postgresql: 7.2.1 (on linux)
Jdbc: pgjdbc2.jar (from jdbc.postgresql.org)

The servlet is quite simple:

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InfoServlet extends HttpServlet {

        Connection con;
        private boolean conFree = true;

        public void init() throws ServletException {
                try  {
                        Context initCtx = new InitialContext();
                        Context envCtx = 
(Context)initCtx.lookup("java:comp/env");
                        DataSource ds = (DataSource)
                        envCtx.lookup("jdbc/BookDB");
                        Connection con = ds.getConnection();
                } catch (Exception ex) {
                        throw new ServletException("Couldn't open connection 
to database: " + ex.getMessage());
                }
        }

        public void destroy() {
                try {
                        con.close();
                } catch (SQLException ex) {
                        System.out.println(ex.getMessage());
                }
        }

        public void doGet (HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {

                HttpSession session = request.getSession();

                // set content-type header before accessing the Writer
                response.setContentType("text/html");
                response.setBufferSize(8192);
                PrintWriter out = response.getWriter();

                // then write the data of the response
                out.println("<html>" +
                "<head><title>Duke's Bookstore</title></head>");
        }
}

in web/WEB-INF/web.xml I defined:


<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
        <display-name>Currency Converter Application</display-name>
        <description>
                Test servlet
        </description>
        <servlet>
                <servlet-name>info</servlet-name>
                <display-name>info</display-name>
                <description>no description</description>
                <servlet-class>InfoServlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>info</servlet-name>
                <url-pattern>/info</url-pattern>
        </servlet-mapping>
        <session-config>
                <session-timeout>30</session-timeout>
        </session-config>
        <resource-ref>
                <res-ref-name>jdbc/BookDB</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Containter</res-auth>
        </resource-ref>
</web-app>

And in server.xml in the contex of the application:

       <Context path="/bookstore" docBase="bookstore" debug="0"
                 reloadable="true" crossContext="true">
          <Logger className="org.apache.catalina.logger.FileLogger"
                     prefix="localhost_bookstore_log." suffix=".txt"
                  timestamp="true"/>
          <Resource name="jdbc/BookDB" auth="Container"
            type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/BookDB">
            <parameter>
              <name>user</name>
              <value>java</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>java</value>
            </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>org.postgresql.Driver</value>
            </parameter>
            <parameter>
              <name>driverName</name>
              <value>jdbc:postgresql:public</value>
            </parameter>
          </ResourceParams>
        </Context>

I've tried a few different driverName parameters: (restarting Tomcat after 
each change)
jdbc:postgresql://localhost/public
jdbc:postgresql://full.server.name/public
jdbc:postgresql:public
jdbc:postgresql://localhost/

But, despite the parameters, I always get the following error:

exception 
javax.servlet.ServletException: Couldn't open connection to database: 
Exception creating DataSource: org.hsql.jdbcDriver
        at InfoServlet.init(Unknown Source)
        at javax.servlet.GenericServlet.init(GenericServlet.java:258)
        at 
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:918)
....

{cut}

I have no idea why Tomcat wants to connect using hsql driver. I grepped 
through tomcat directory and found only one reference to this driver (in 
examples section). I removed event the sample section, restarted Tomcat but 
it didn't help. 

Does anyone know what is wrong? 

regards
Przem


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


tc 4.0.4 + apache 2.0.36: mod_jk vs mod_proxy

Posted by Ekkehard Gentz <ek...@gentz-software.de>.
hi,
we have a question ?

when should we use mod_proxy and when mod_jk ?

we have a webapp with jsp's, which works best with
tc 4.0.4 and apache 2.0.36 as tandem,
because apache serves the static content much better
(many gifs for toolbars, many pdf-files etc)
at this time we have configured this using mod_jk2

last day we made some experiences using apache as proxy
using mod_proxy
we did the same as before: apache serves all static content
from the webapp and sends the othe requests to tomcat
we configured proxy-params in the tomcat server.xml
and did some first tests:
seems to be equal to the access-times using mod_jk

so now we have the questions:
what are the pros and cons of these two methods to
let apache serve static and tomcat the rest of the
web-application ?

our webapp uses jsp's, java-script, only one applet for
styled text - entry, no tag-libs, no JDBC (we use
the OODB FastObjects)

apache sets the http header so the gifs we need to
display toolbars in the windows will not always asked for
to the server

thanks for infos

regards

ekkehard

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Tomcat 4.0.4, jndi, jdbc and postgresql [long]

Posted by Arshad Mahmood <ar...@compuvision.co.uk>.
Hi,

I notice that you are doing the JDBC lookup in the "init" function. This
doesn't appear to work properly (at least on 4.1.6), move the initialisation
code to the "doGet/doPost" and see if that helps. This cured my JNDI/JDBC
problems.

Regards.

----- Original Message -----
From: "Przemyslaw Kowalczyk" <pr...@engine.com.pl>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Tuesday, July 02, 2002 11:08 AM
Subject: Tomcat 4.0.4, jndi, jdbc and postgresql [long]


> Hi
>
> I've just started to write servlets. Servlets that only do processing,
without
> connecting to database work fine, but I have some problems to get the
> connection with postgresql via jdbc work.
>
> Tomcat: 4.0.4 (on linux)
> Postgresql: 7.2.1 (on linux)
> Jdbc: pgjdbc2.jar (from jdbc.postgresql.org)
>
> The servlet is quite simple:
>
> import java.io.*;
> import java.util.*;
> import java.sql.*;
> import javax.sql.*;
> import javax.naming.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class InfoServlet extends HttpServlet {
>
>         Connection con;
>         private boolean conFree = true;
>
>         public void init() throws ServletException {
>                 try  {
>                         Context initCtx = new InitialContext();
>                         Context envCtx =
> (Context)initCtx.lookup("java:comp/env");
>                         DataSource ds = (DataSource)
>                         envCtx.lookup("jdbc/BookDB");
>                         Connection con = ds.getConnection();
>                 } catch (Exception ex) {
>                         throw new ServletException("Couldn't open
connection
> to database: " + ex.getMessage());
>                 }
>         }
>
>         public void destroy() {
>                 try {
>                         con.close();
>                 } catch (SQLException ex) {
>                         System.out.println(ex.getMessage());
>                 }
>         }
>
>         public void doGet (HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
>                 HttpSession session = request.getSession();
>
>                 // set content-type header before accessing the Writer
>                 response.setContentType("text/html");
>                 response.setBufferSize(8192);
>                 PrintWriter out = response.getWriter();
>
>                 // then write the data of the response
>                 out.println("<html>" +
>                 "<head><title>Duke's Bookstore</title></head>");
>         }
> }
>
> in web/WEB-INF/web.xml I defined:
>
>
> <!DOCTYPE web-app
>     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
>     "http://java.sun.com/dtd/web-app_2_3.dtd">
> <web-app>
>         <display-name>Currency Converter Application</display-name>
>         <description>
>                 Test servlet
>         </description>
>         <servlet>
>                 <servlet-name>info</servlet-name>
>                 <display-name>info</display-name>
>                 <description>no description</description>
>                 <servlet-class>InfoServlet</servlet-class>
>         </servlet>
>         <servlet-mapping>
>                 <servlet-name>info</servlet-name>
>                 <url-pattern>/info</url-pattern>
>         </servlet-mapping>
>         <session-config>
>                 <session-timeout>30</session-timeout>
>         </session-config>
>         <resource-ref>
>                 <res-ref-name>jdbc/BookDB</res-ref-name>
>                 <res-type>javax.sql.DataSource</res-type>
>                 <res-auth>Containter</res-auth>
>         </resource-ref>
> </web-app>
>
> And in server.xml in the contex of the application:
>
>        <Context path="/bookstore" docBase="bookstore" debug="0"
>                  reloadable="true" crossContext="true">
>           <Logger className="org.apache.catalina.logger.FileLogger"
>                      prefix="localhost_bookstore_log." suffix=".txt"
>                   timestamp="true"/>
>           <Resource name="jdbc/BookDB" auth="Container"
>             type="javax.sql.DataSource"/>
>           <ResourceParams name="jdbc/BookDB">
>             <parameter>
>               <name>user</name>
>               <value>java</value>
>             </parameter>
>             <parameter>
>               <name>password</name>
>               <value>java</value>
>             </parameter>
>             <parameter>
>               <name>driverClassName</name>
>               <value>org.postgresql.Driver</value>
>             </parameter>
>             <parameter>
>               <name>driverName</name>
>               <value>jdbc:postgresql:public</value>
>             </parameter>
>           </ResourceParams>
>         </Context>
>
> I've tried a few different driverName parameters: (restarting Tomcat after
> each change)
> jdbc:postgresql://localhost/public
> jdbc:postgresql://full.server.name/public
> jdbc:postgresql:public
> jdbc:postgresql://localhost/
>
> But, despite the parameters, I always get the following error:
>
> exception
> javax.servlet.ServletException: Couldn't open connection to database:
> Exception creating DataSource: org.hsql.jdbcDriver
>         at InfoServlet.init(Unknown Source)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at
>
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:91
8)
> ....
>
> {cut}
>
> I have no idea why Tomcat wants to connect using hsql driver. I grepped
> through tomcat directory and found only one reference to this driver (in
> examples section). I removed event the sample section, restarted Tomcat
but
> it didn't help.
>
> Does anyone know what is wrong?
>
> regards
> Przem
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>