Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java - Mailing list pgsql-jdbc

From Dave Cramer
Subject Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java
Date
Msg-id 018101c18ed6$6641fa30$8201a8c0@inspiron
Whole thread Raw
In response to Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java  (Peter Adamek <peter.adamek@utoronto.ca>)
List pgsql-jdbc
Peter,

As an aside, if you want to make the complete transition (IMO) have a
look at velocity (http://jakarta.apache.org/velocity), or
(www.webmacro.org) webmacro , and get rid of JSP too!

I see you are at U of T, I am in Orangeville!

Cheers,

Dave

-----Original Message-----
From: pgsql-jdbc-owner@postgresql.org
[mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Peter Adamek
Sent: Thursday, December 27, 2001 2:16 AM
To: Jens Carlberg
Cc: pgsql-jdbc@postgresql.org
Subject: [JDBC] Problem with connecting to postgres using JDBC with JSP,
but not JDBC with Java


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(Applica
tionFilterChain.java:247)

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

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

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

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

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

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
72)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
e.java:201)

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

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

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

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
72)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:234
4)
        at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
:164)

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

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

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

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

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

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

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
72)
        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.ja
va:566)

        at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
72)
        at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.j
ava: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,passwor
d);}

        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) {}
    }
}



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)



pgsql-jdbc by date:

Previous
From: Justin Clift
Date:
Subject: Does the 7.2 JDBC driver work properly with DbVisualizer yet?
Next
From: tony
Date:
Subject: Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java