Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tomcat: ServletException: is not bound in this Context

Status
Not open for further replies.

dustfinger

Technical User
Aug 12, 2005
3
0
0
CA
I am working on a simple java servlet and I am getting the following error from tomcat.

javax.servlet.ServletException: Name ImageServerDS is not bound in this Context
com.javasrc.imageserver.web.ImageServerServlet.init(ImageServerServlet.java:38)
javax.servlet.GenericServlet.init(GenericServlet.java:211)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)

root cause

javax.naming.NameNotFoundException: Name ImageServerDS is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:768)
org.apache.naming.NamingContext.lookup(NamingContext.java:151)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
javax.naming.InitialContext.lookup(InitialContext.java:347)
com.javasrc.imageserver.web.ImageServerServlet.init(ImageServerServlet.java:34)
javax.servlet.GenericServlet.init(GenericServlet.java:211)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
java.lang.Thread.run(Thread.java:534)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.

# jar tf ../imageserver.war
META-INF/
META-INF/MANIFEST.MF
META-INF/context.xml
WEB-INF/
WEB-INF/web.xml
WEB-INF/classes/
WEB-INF/classes/com/
WEB-INF/classes/com/javasrc/
WEB-INF/classes/com/javasrc/imageserver/
WEB-INF/classes/com/javasrc/imageserver/web/
WEB-INF/classes/com/javasrc/imageserver/web/ImageServerServlet.class

# cat WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' '
<web-app>
<servlet>
<servlet-name>ImageServerServlet</servlet-name>
<servlet-class>com.javasrc.imageserver.web.ImageServerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ImageServerServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>

# cat META-INF/context.xml
<!-- Added imageserver context -->
<Context path="/imageserver" docBase="imageserver" debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_imageserver_log." suffix=".txt" timestamp="true"/>

<Resource name="jdbc/ImageServerDS" auth="Container" type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/ImageServerDS">

<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/ImageServerDS?autoReconnect=true</value>
</parameter>

<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>

<parameter>
<name>username</name>
<value>dustfinger</value>
</parameter>

<parameter>
<name>password</name>
<value>***</value>
</parameter>

<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>

<parameter>
<name>removeAbandonedTimeout</name>
<value>300</value>
</parameter>

<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>

</ResourceParams>
</Context>

Taken from ImageServerServlet.java
Code:
 public void init() throws ServletException
  {
    try
    {
      InitialContext ic = new InitialContext();
      this.datasource = ( DataSource )ic.lookup( "java:/ImageServerDS" );
    }
    catch( Exception e )
    {
      throw new ServletException( e );
    }
  }
Also at the top of the file I have
Code:
package com.javasrc.imageserver.web;
I placed my war file in /opt/tomcat5/webapps/ and I checked that it was properly uncompressed by tomcat. I believe that I am doing everything correctly, so I am hoping that it is just a type'o that I am unable to see. I have been looking at this for a long while now. Any help would be appreciated.

One more thing that I took note of: /opt/tomcat5/logs/catalina.out tells me that
SEVERE: Error deploying configuration descriptor imageserver.xml
java.io.IOException: java.io.FileNotFoundException: /etc/tomcat5/Catalina/localhost/imageserver.xml (Is a directory)
So my temporary solution is to cp context.xml file to /etc/tomcat5/Catalina/localhost/imageserver.xml. This does seem to solve that problem, however I wonder why imageserver.xml was being generated as a directory in the first place?

dustfinger.
 
Solved it. All I had to do was get the datasource like this..
Code:
 Context initCtx = new InitialContext();
	 Context envCtx = (Context) initCtx.lookup("java:comp/env");
	 if(envCtx == null )
	     throw new Exception("Boom - No Environment Context");
	 
	this.datasource =
	     (DataSource) envCtx.lookup("jdbc/ImageServerDS");

dustfinger.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top