*** URGENT HELP REQUIRED: Servlet cant access DB over JDBC

Hi Guys, This may sound silly.. but i had problems when my Servlet tried to access the Db using JDBC.

I am using PostgreSQL 8 as DB.

The JDBC code that has been used accessing the DB, was also implemented in a desktop application and worked fine. but when it came to servlets there were problems.

Here is the code:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

publicclass VerifyLoginextends HttpServlet

{

publicvoid doGet (HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException

{

String user_id= req.getParameter ("uid");

String pass= req.getParameter ("pwd");

try

{

Class.forName("org.postgresql.Driver");

}

catch (ClassNotFoundException cnfe)

{

res.sendRedirect("/project/Error1");

}

System.out.println("Registered the driver ok, so let's make a connection.");

Connection c =null;

try

{

c = DriverManager.getConnection("jdbc:postgresql://localhost/bookstore","postgres","password");

}

catch (SQLException se)

{

res.sendRedirect("/project/Error2");

}

try

{

PreparedStatement login = c.prepareStatement("SELECt password FROM login WHERE username=?");

login.setString(1,user_id);

ResultSet rs=login.executeQuery();

while (rs.next())

{

if ( pass.equals(rs.getString(1)))

{

res.sendRedirect("/project/Success");

}

else

{

res.sendRedirect("/project/Login");}

}

}

catch ( SQLException e)

{

}

}

}

And this is the error I got:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfillingthis request.

exception

java.lang.IllegalStateException

org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:418)

VerifyLogin.doGet(VerifyLogin.java:46)

javax.servlet.http.HttpServlet.service(HttpServlet.java:747)

javax.servlet.http.HttpServlet.service(HttpServlet.java:860)

sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

java.lang.reflect.Method.invoke(Method.java:585)

org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)

java.security.AccessController.doPrivileged(Native Method)

javax.security.auth.Subject.doAsPrivileged(Subject.java:517)

org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)

org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)

note The full stack trace of the root cause is available in the Sun-Java-System/Application-Server logs.

Where is the problem? Please help me out.. I am at my wits end.

[4575 byte] By [arijit_dattaa] at [2007-10-3 1:47:46]
# 1

From the HttpServletResponse#sendRedirect(String) API docs:

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Normally, I see this when I call getOutputStream() or getWriter(), output some content, and then attempt to do a redirect. Or if you attempt to do a redirect after a stream is closed. However, I do not see you doing either of these in this method.

Just for kicks and giggles, does doPost() work? Also, you need to close your JDBC resources and not 'eat' the SQL exception, it might be telling you something.

- Saish

Saisha at 2007-7-14 18:46:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Here's one thing that could be happening:

1. Your JDBC driver isn't in the classpath, so you redirect to /project/Error1.

2. Next, DriverManager.getConnection fails, so you redirect to /project/Error2.

Redirecting twice in the same request is illegal, so you get that exception thrown.

Proper error logging would show you whether it's happening. Just because it's allegedly urgent doesn't mean you can leave out important code.

DrClapa at 2007-7-14 18:46:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi.. Thanks for your advice .. I did get some results: but on the whole was not sucessful:

I tweaked the code a bit . Here is the new code

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class VerifyLogin extends HttpServlet

{

public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

{

String user_id= req.getParameter ("uid");

String pass= req.getParameter ("pwd");

res.setContentType( "text/html");

PrintWriter out = res.getWriter();

out.println("<html>");

try

{

Class.forName("org.postgresql.Driver");

}

catch (ClassNotFoundException cnfe)

{

out.println(cnfe.getMessage());

}

Connection c = null;

try

{

c = DriverManager.getConnection("jdbc:postgresql://localhost/bookstore","postgres", "password");

out.println("<head>");

out.println("<title>Successful</title>");

out.println("</head>");

c.close();

}

catch (SQLException se)

{

out.println(se.getMessage());

}

out.println("</html>");

}

}

And I got the error :

org.postgresql.Driver No suitable driver

I checked my CLASSPATH

SET CLASSPATH=C:\Sun\AppServer\lib\j2ee.jar;C:\Sun\AppServer\PostgreSQL\postgresql-8.2dev-503.jdbc3.jar;%CLASSPATH%,.

I double checked it to see what if the classpath really reflects where the PostgreSQL driver is kept ...but everything seemed to be in order.

Here is the catch. Just to test the driver I developed a simple desktop application accessing the same PostgreSQL DB with the same driver .. It worked fine.. it connected the DB smoothly without any hiccups!

I am stumped! Help!!

arijit_dattaa at 2007-7-14 18:46:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

As to the first servlet failing:

Put return; statements after your redirects.

The code DOES not stop executing after a sendRedirect(), so your exception handler will do a redirect and then the code below it will execute, doing another redirect. This too will cause the response has already been committed error (as drClap had already noted).

gimbal2a at 2007-7-14 18:46:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hey the problem has been solved: The servlet could connect to the postgre db without a hitch. Have a look at the the this thread: http://forum.java.sun.com/thread.jspa?messageID=4328752
arijit_dattaa at 2007-7-14 18:46:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...