aspose file tools*
The moose likes Struts and the fly likes Error - Name jdbc is not bound in this Context    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error - Name jdbc is not bound in this Context    " Watch "Error - Name jdbc is not bound in this Context    " New topic
Author

Error - Name jdbc is not bound in this Context

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 396
Hi,

i wanted to use Datasource in JDBC to make connection with mysql
suport and want to excute Jsp page but not able to execte some error is
getting.

1)Jsp Page: data_source.jsp
----------------------------------------------

<%@ page session="true" import="java.sql.*,javax.sql.*,javax.naming.*" %>

<HTML>
<body bgcolor="blue">
<H1 align = "center" >

Welcome to User and can see the DataSource Connection
<%
InitialContext ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("jdbc/test");

Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from emp");

if(rs.next())
{
%>

<table width="100%" border="1" >
<tr align="left">
<th>Name</th>
<th>Dept</th>
</tr>

<%
do
{
%>
<TD> <%= rs.getString("Name") %> </TD>
<TD> <%= rs.getString("Dept") %> </TD>
<%
}while(rs.next());

%>

</table>
<%
}else {
%>
No Results from Query:
<%
}
rs.close();
stmt.close();
conn.close();
ctx.close();
%>

</body>
</html>

2) web.xml file content
-----------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<description>MySQL Test App</description>
<resource-ref>
<description>Mysql DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>first</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

</web-app>

3) myhealth.xml or server.xml
----------------------------------------------

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="C:/Program Files/Apache Software Foundation/Tomcat
5.0/myhealth" path="/myhealth" reloadable="true" privileged="true">

<Resource name="jdbc/test"
scope="Shareable" type="javax.sql.DataSource"
auth="Container" description="hOME.."/>

<ResourceParams name="jdbc/test">
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>

<value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
</ResourceParams>

</Context>

4) Error which i got during execution of data_Source.jsp page.


HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

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

exception

javax.servlet.ServletException: Name jdbc is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.Data_005fSource_jsp._jspService(Data_005fSource_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

root cause

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:815)
org.apache.naming.NamingContext.lookup(NamingContext.java:198)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
javax.naming.InitialContext.lookup(InitialContext.java:351)
org.apache.jsp.Data_005fSource_jsp._jspService(Data_005fSource_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

As i made datasource Context using Tomcat apache. But i still get same error.

I tried so much from Google search, please give me proper solution.

Coz this is the common problem most of us face initally which i saw in seaching from google. so, what is this and clear me.

Thanks,
Prabhat
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Your server.xml does not look right to me. Specifically, you're missing a factory parameter. This link has a MySQL datasource example for Tomcat 5.0.

Watch the log file when the server initializes. There should be an entry indicating the datasource was bound properly or else an error message if it wasn't.

P.S. If this is just a demo application, that's fine, but if this is a real production application, database access code in a JSP is a big violation of MVC principles.
[ October 04, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error - Name jdbc is not bound in this Context