Skip site navigation (1) Skip section navigation (2)

Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java

From: Peter Adamek <peter(dot)adamek(at)utoronto(dot)ca>
To: Jens Carlberg <jenca(at)lysator(dot)liu(dot)se>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java
Date: 2001-12-27 07:16:22
Message-ID: 3C2ACAC6.FBBF9714@utoronto.ca (view raw or flat)
Thread:
Lists: pgsql-jdbc
First off, many thanks to Rene Pijlman, Jens Carlberg and K.Ari Kruprikov
for your previous emails regarding the subcect Problem with connecting to
postgres using jdbc7.0-1.2.jar.  Your suggestions have been very
informative.

I must confess, I am somewhat new to JSP and Java as a whole.  My background
is in ASP development and I am dying to fully convert over.  I am getting
the hang of things pretty well.  I have a full arsenal of Oreiley and Wrox
books I've gone out to purchase at my disposal on Java 2, JSP 2nd eddition,
Databases with PostgreSQL, etc.  I'm also running Red Hat Linux 7.1.
Switched over from NT 4.0.  So please bear with me.

I have used your suggestion Rene and tried connecting to my postgresql
database using the "HelloPostgreSQL.java" pure JAVA (non-JSP) program
located at http://www.fankhausers.com/postgresql/jdbc/.  Running this
enabled me to connect to the database, create tables, insert data, delete
data and drop tables.

Subsequent to this, I tried to modify my JSP code to also connect to the
database (using the same connection string and all), but I kept on getting
the following run time error in my browser:

Apache Tomcat/4.0.1 - HTTP Status 500 - Internal Server Error
type Exception report
message Internal Server Error
description The server encountered an internal error (Internal Server
Error) that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
        at TestServlet.service(TestServlet.java:40)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)

        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

        at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
        at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)

        at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)

        at
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)

        at java.lang.Thread.run(Thread.java:484)

At this point, I followed Jens suggestion to modify my code to the
following.  In the following code, I don't run any SQL statements against
the database, but after attempting to make the database connection, I check
to see if it (the connection) is equal to null and sure enough it is.  This
is what was generating the above error.  So at this point, I'm not sure of
what to do to.  I think my problem is in the way the drivers or classes are
being initialized, but I'm not sure.  I have folllowed the standard way to
connect to the database as has been suggested in the books and on numerous
sites, but nothing seems to work.  I know the connection string works fine,
becuase the same one works in my "HelloPostgreSQL.java" pure JAVA (non-JSP)
program.

One thing that I am thinking is that it may be the way I am running Tomcat.
I have downloaded the binary, untared it as su and when I run the startup
script, I run it as su (standalone without having it be integrated with
Apache).  Could this present some problem?  What context would you suggest
running Tomcat as (there seems to be very limited documentation on
this)?  If I should run it as another user, I supposes I have to change the
ownership of all files in the $TOMCAT_HOME tree to that user context
otherwise I may not get permission to certain files.  I am also in the
middle of trying to integrate my Tomcat with Apache to see if maybe that
type of setup will do the trick.  Any suggestions once again would be
greatly appreciated (and don't be afraid to say the obvious - it may not be
obvious to me).  Many thanks again to Rene, Jens and Ari.

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

public class TestServlet extends HttpServlet {

    Connection db;
    String database = "peter";
    String username = "myLogin";
    String password = "myPassword";

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

        /* Load the driver */
        try {Class.forName("org.postgresql.Driver");}
        catch (ClassNotFoundException e) {}

        /* Connect to the database */
        try {db =
DriverManager.getConnection("jdbc:postgresql:"+database,username,password);}

        catch (SQLException e) {}
    }

    public void service (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException {
        // set content type and other response header fields first
        response.setContentType("text/html");

        // get the communication channel with the requesting client
        PrintWriter out = response.getWriter();

        /* Standard top bit */
        out.println("<HTML>\n"
                    + "<HEAD>\n"
                    + " <TITLE>Test</TITLE>\n"
                    + "</HEAD>\n"
                    + "<BODY BGCOLOR=\"#FFFFFF\">\n");

        if (db == null) {
           out.println("Database is not being opened.<br>");

        }

        /* Standard bottom bit */
        out.println("</BODY>\n"
                    + "</HTML>\n");
        out.close();
    }

    public void destroy() {
        try {db.close();}
        catch (SQLException e) {}
    }
}



In response to

Responses

pgsql-jdbc by date

Next:From: Jens CarlbergDate: 2001-12-27 09:20:13
Subject: Re: Problem with connecting to postgres using JDBC with JSP, but not
Previous:From: Barry LindDate: 2001-12-27 02:44:35
Subject: Re: [HACKERS] Failure in timestamptz of JDBC of 7.2b4

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group