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

From: "Nick Fankhauser" <nickf(at)ontko(dot)com>
To: "Peter Adamek" <peter(dot)adamek(at)utoronto(dot)ca>, "Jens Carlberg" <jenca(at)lysator(dot)liu(dot)se>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java
Date: 2001-12-27 13:25:06
Message-ID: NEBBLAAHGLEEPCGOBHDGAEOCEBAA.nickf@ontko.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Peter-

A couple of thoughts-

Apparently the database connection is failing. You want to know why, so I'd
suggest replacing "catch (SQLException e) {}" with "catch (SQLException e)
{e.printStackTrace();}".

Since you're running Tomcat, the stack trace won't appear on the console,
but you should be able to find it in the tomcat log files. In my case, I'm
on Debian Linux, so to look at the tomcat stdout log, I use "tail -50
/var/log/tomcat/stdout.log". The location probably varies slightly by OS.
(Get 50 lines on the tail because the stack trace will be long <grin>.)

My first guess as to what you'll find in the trace is that it's an
authentication problem. You mention that tomcat is running as su (root?).
Take a look at the pg_hba.conf file for postgresql- It's pretty standard for
it to be set up as "peer sameuser" by default for localhost connections. If
this is the case, Tomcat will be running as root, but your servlet tries to
connect as "peter" & fails the sameuser test for authentication, or even if
the auth method is "trust", root may not be a postgres user. I suppose you
could test this quickly by creating a root postgres user. (I'm not sure what
the longer-term security implications of this would be, but I assume we're
in "make it work" mode right now.)

BTW, tomcat usually runs as www-data by default in most installations (same
as apache), so In my case, I just make sure there is a www-data user in
postgresql & grant that user the privs I need it to have on each object.

I'm running Tomcat 3.2, so your mileage may vary, but it looks to me like
you've licked the Tomcat issues & you're back to postgres now.

Hope this helps-

-Nick

---------------------------------------------------------------------
Nick Fankhauser

Business:
nickf(at)doxpop(dot)com Phone 1.765.965.7363 Fax 1.765.962.9788
doxpop - Court records at your fingertips - http://www.doxpop.com/

Personal:
nick(at)fankhausers(dot)com http://www.fankhausers.com

> -----Original Message-----
> From: pgsql-jdbc-owner(at)postgresql(dot)org
> [mailto:pgsql-jdbc-owner(at)postgresql(dot)org]On Behalf Of Peter Adamek
> Sent: Thursday, December 27, 2001 2:16 AM
> To: Jens Carlberg
> Cc: pgsql-jdbc(at)postgresql(dot)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(A
> pplicationFilterChain.java:247)
>
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(Applicati
> onFilterChain.java:193)
>
> at filters.ExampleFilter.doFilter(ExampleFilter.java:149)
> at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(A
> pplicationFilterChain.java:213)
>
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(Applicati
> onFilterChain.java:193)
>
> at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapp
> erValve.java:243)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.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(StandardConte
> xtValve.java:201)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.java:566)
>
> at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authent
> icatorBase.java:472)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.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(StandardHostValv
> e.java:164)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.java:566)
>
> at
> org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispat
> cherValve.java:170)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.java:564)
>
> at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValv
> e.java:170)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.java:564)
>
> at
> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.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(StandardEngine
> Valve.java:163)
>
> at
> org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipel
> ine.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(HttpProce
> ssor.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,p
> assword);}
>
> 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(at)postgresql(dot)org)
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message tony 2001-12-27 14:57:21 Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java
Previous Message Dave Cramer 2001-12-27 13:00:02 Re: Problem with connecting to postgres using JDBC with JSP, but not JDBC with Java