Servlet Cant Connect to PostgreSQL DB

I have written the code for a Servlet to connect to the PostgreSQL DB.. its showing a naming error:

import javax.sql.*;

import javax.naming.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

import java.util.logging.*;

publicclass VerifyLoginextends HttpServlet

{

privatefinalstatic Logger log = Logger.getLogger(VerifyLogin.class.getName());

privatefinalstatic String DATASOURCE_NAME="jdbc:/PostgresDS";

private DataSource _dataSource;

publicvoid setDataSource(DataSource dataSource)

{

_dataSource= dataSource;

}

public DataSource getDataSource()

{

return _dataSource;

}

publicvoid init()throws ServletException

{

if( _dataSource ==null )

{

try

{

Context env = (Context)new InitialContext().lookup("java:comp/env");

_dataSource = (DataSource)env.lookup(DATASOURCE_NAME);

if (_dataSource ==null )

thrownew ServletException ("'"+DATASOURCE_NAME+"'is an unknown DataSource");

}

catch (NamingException e)

{

thrownew ServletException (e);

}

}

}

publicvoid 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>");

Connection conn=null;

try

{

conn = getDataSource().getConnection();

out.println("<head>");

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

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

}

catch (SQLException se)

{

thrownew ServletException(se);

}

finally

{

try

{

if(conn!=null)

conn.close();

}

catch (SQLException e)

{

log.log(Level.WARNING, e.toString(), e);

}

}

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

}

}

here is the web.xml that i had edited:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<database jndi-name="jdbc:/PostgreDS">

<driver>

<type>org.postgresql.Driver</type>

<url>jdbc:postgres://localhost:5432/bookstore</url>

<user>postgres</user>

<password>password</password>

</driver>

</database>

<display-name>project</display-name>

<servlet>

<display-name>VerifyLogin</display-name>

<servlet-name>VerifyLogin</servlet-name>

<servlet-class>VerifyLogin</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>VerifyLogin</servlet-name>

<url-pattern>/verifylogin</url-pattern>

</servlet-mapping>

</web-app>

and this is the error that I am getting:

type Exception report

message

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

exception

javax.servlet.ServletException: No object bound to name java:comp/env/jdbc:/PostgresDS

VerifyLogin.init(VerifyLogin.java:56)

javax.servlet.GenericServlet.init(GenericServlet.java:261)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

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)

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

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:189)

com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:604)

com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:475)

com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:371)

com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:264)

com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:281)

com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:83)

root cause

javax.naming.NameNotFoundException: No object bound to name java:comp/env/jdbc:/PostgresDS

com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:668)

com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:132)

VerifyLogin.init(VerifyLogin.java:45)

javax.servlet.GenericServlet.init(GenericServlet.java:261)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

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)

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

org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:189)

com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:604)

com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:475)

com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:371)

com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:264)

com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:281)

com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:83)

By the way, just to test the PostgreSQL DB i developed a small desktop application..and it worked fine.. so there is nothing wrong with the CLASSPATH settings.

Where is the Servelt experienceing the problems. and How can it be solved?

Thanks

[9916 byte] By [arijit_dattaa] at [2007-10-3 1:57:50]
# 1

> By the way, just to test the PostgreSQL DB i

> developed a small desktop application..and it worked

> fine.. so there is nothing wrong with the CLASSPATH

> settings.

When working with servlets the CLASSPATH of your system is not used.

Just check if you have the jdbc driver in the common/lib directory in case

you are using tomcat. or you can put it in the lib directory of your application.

regards

Pravin

PMJaina at 2007-7-14 18:56:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi Pravin... Thanks for all the help. The servlet could connect to the PostgreSQL DB without a hitch. I am using the Sun AppServer and put the postgre-jdbc driver in the Sun>AppServer>lib directory.. and it worked superbly!! :)Thanks once again.
arijit_dattaa at 2007-7-14 18:56:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...