JSP-JNDI problem(Name java:comp is not bound in this Context)
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, September 30, 2004 02:04 AM

Hi, Basically, what I've done is to use the Tomcat administration web application to create the DataSource, which looks like it populated the server.xml (see below). I then try to access the testconn.jsp, and am getting that "Name java:comp is not bound in this Context" error. I was wondering if anyone could tell me what I'm doing wrong? Thanks, Feri My Configuration: - Tomcat 5.0.19 - MySQL 4.0.18-nt - mysql-connector-java-3.0.15-ga-bin.jar -------------------------------- server.xml: -------------------------------- UserDatabaseRealm to authenticate users -->    More>>

Hi,

Basically, what I've done is to use the Tomcat administration web
application to create the DataSource, which looks like it populated the
server.xml (see below). I then try to access the testconn.jsp, and am
getting that "Name java:comp is not bound in this Context" error.

I was wondering if anyone could tell me what I'm doing wrong?

Thanks,
Feri

My Configuration:
- Tomcat 5.0.19
- MySQL 4.0.18-nt
- mysql-connector-java-3.0.15-ga-bin.jar
--------------------------------
server.xml:
--------------------------------








type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved">



factory
org.apache.catalina.users.MemoryUserDatabaseFactory


pathname
conf/tomcat-users.xml








type="javax.sql.DataSource">



factory
org.apache.commons.dbcp.BasicDataSourceFactory


username
root


password
root


driverClassName
com.mysql.jdbc.Driver


url
jdbc:mysql://localhost:3306/injury


maxIdle
30


maxActive
10


maxWait
10000








--------------------------------------------------
webappsinjuryWEB-INFweb.xml:
--------------------------------------------------

Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the server.xml file.
jdbc/injury
javax.sql.DataSource
Container


---------------------------------------------
webappsinjury estconn.jsp
---------------------------------------------
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ taglib prefix="ct" uri="/injury" %>




Connection test




<%
Statement stmt= conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("select * from user");

while (rs.next()){
%> <%=rs.getString(1)%> <%=rs.getInt(2) %>
<%
}
rs.close();
stmt.close();
%>


-----------------------------------------------------
ConnectionTag.java
------------------------------------------------------

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import javax.naming.*;
import javax.sql.*;
import javax.sql.DataSource;

public class ConnectionTag extends TagSupport implements TryCatchFinally {
private Connection conn;

// JNDI name of the connection
private String name;

public void setName(String name)
{
this.name = name;
}

public int doStartTag()
throws JspException
{
try {
Context env = (Context) new InitialContext().lookup("java:comp/env");

DataSource ds = (DataSource) env.lookup(name);

if (ds != null)
conn = ds.getConnection();
} catch (Exception e) {
throw new JspException(e);
}

if (conn == null)
throw new JspException("can't open connection " + name);

pageContext.setAttribute("conn", conn);

return EVAL_BODY_INCLUDE;
}

public void doCatch(Throwable t)
throws Throwable
{
throw t;
}

public void doFinally()
{
try {
Connection conn = this.conn;
this.conn = null;
pageContext.removeAttribute("conn");

conn.close();
} catch (Exception e) {
}
}
}
-------------------------------------------------------
Tomcat 5.0confCatalinalocalhostinjury.xml
-----------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>

cachingAllowed="true" charsetMapperClass="org.apache.catalina .util.CharsetMapper" cookies="true" crossContext="false" debug="0"
mapperClass="org.apache.catalina.core.StandardContextMapper" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper" >




   <<Less
About | Sitemap | Contact