Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Tomcat and the fly likes using the StandardContext.getDocBase() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "using the StandardContext.getDocBase()" Watch "using the StandardContext.getDocBase()" New topic
Author

using the StandardContext.getDocBase()

bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Hello there.

I'm trying to use the StandardContext in the org.apache.catalina.core.* package to retreive the docBase from the context. I have experience using the java.naming.Context and the java.naming.InitialContext classes, but can't seem to get the StandardContext to work. I've included the catalina 5.5.9 jar in my classpath and things seem to compile correctly, but I get runtime errors.

In all honesty, I can't even get this to work:

StandardContext tomcatContext = new StandardContext();

When I load the page I get this error:

ERROR - Servlet.service() for servlet action threw exception java.lang.NoClassDefFoundError: org/apache/tomcat/util/http/mapper/Mapper at org.apache.catalina.core.StandardContext.<init>(StandardContext.java:377

Has anyone tried to do this before? All I really want to be able to do is StandardContext.getDocBase(). That's all.

Thanks for your time!
bryan


"...and the Truth will set you free."
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
The missing class (Mapper) is in CATALINA_HOME/server/lib/tomcat-util.jar
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Oh. Well that solved that problem. (So why didn't the compiler catch that that class wasn't available?)

Unfortunately I'm still not quite there yet. I have this so far:



This is actually returning "null" right now. Which makes sense because if I just created a new context, the docBase will be null for it. So my question is, how do I get the current context (perhaps the javax.naming.Context?) to populate my new StandardContext? Everytime I try to do something like this:



i receive a class cast exception. any ideas?

thanks for your help so far!
bryan
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Check what's being returned with
System.out.println( initialContext.lookup("java:comp/env").getClass() );
and go from there.
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Well, that class is this:

org.apache.naming.NamingContext

So it seems like I need to get from an org.apache.catalina.core.StandardContext to an org.apache.naming.NamingContext. Only I have no idea how to do this. I can't find any methods in either of those classes to attempt such a thing when looking through the API Docs.

What's also weird is that this compiles just fine:

tomcatContext = (StandardContext) initialContext.lookup("java:comp/env");

however, it throws a classCastException at runtime.

Any ideas on how to get my NamingContext to a StandardContext?

Thank you!
bryan
Joseph Marques
Greenhorn

Joined: May 01, 2005
Posts: 12
Well of course

tomcatContext = (StandardContext) initialContext.lookup("java:comp/env");

compiles just fine. The lookup returns an object, and you're down-casting to some other type. A compiler does not konw what subtypes of objects were put into the context, and it can't possible know what the string "java:comp/env" will be mapped to in a target environment anyway.

Then, at runtime, the "java:comp/env" is mapped to one type of context object, but you're trying to cast it into another - ClassCastException.

My two cents,
-joe
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Fair enough.

So how DO I get a NamingContext to a StandardContext?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

That's a really good question, because i need this also.

How could you get the StandardContext-object where you can call getters like getDocBase, findMimeMapping,...


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Unfortunately I still haven't figured this out either. There has to be a way...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want your webapps to have access to objects residing outside of it's own context, you have to set the privileged attribute to "true" in your Context entry.

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

I haven't done what you're trying to do so I can't help you beyond that.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using the StandardContext.getDocBase()
 
Similar Threads
JAX Custom Exception Mapping to SOAP Fault
text editors
Error running JSP file
SimpleDateFormat -->2 Year Dates
Stuck on HelloWorldServlet