• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using the StandardContext.getDocBase()

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The missing class (Mapper) is in CATALINA_HOME/server/lib/tomcat-util.jar
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check what's being returned with
System.out.println( initialContext.lookup("java:comp/env").getClass() );
and go from there.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough.

So how DO I get a NamingContext to a StandardContext?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,...
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately I still haven't figured this out either. There has to be a way...
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic