File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes getContext(uri) in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getContext(uri) in Tomcat" Watch "getContext(uri) in Tomcat" New topic
Author

getContext(uri) in Tomcat

Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
I need to access a servlet that resides in another webapp in the same virtual host as my web app.
My code, minus all the details, boils down to this:
myContext = getServletContext();
otherAppContext = myContext.getServletContext("/TheOtherApp");
I have meticulously and reapeadly checked and double checked spelling, case sensitivity, and everything alse that I can think of in my code and my web.xml file. I am pretty sure that by code is correct.
I am 99% convinced that the problem is that Tomcat has some configuration parameter that enables or prohibits cross-application context sharing. I am speculating that the default setting prohibits context sharing. But, I can't find any documentation on this issue. Can anybody help?
Thanks,
Dave Segal
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
you cant access the context of a different web app, they are protected from each other. If you need a servlet in another context you can either
a) access the info over HTTP
or
b) copy the code over to your own context.
but you cant go outside the boundaries of you web application


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Chanoch,
It would be most valuable to me if you could comment on the javadoc that I have pasted on the end of this reply. Either I am misinterpreting, or the Tomcat implementation or installation is problematic.
according to the javadoc, the javax.servlet.ServletCongfig.getContext(String uripath) method should work the way I am trying to use it. But, there is a brief reference to cases where in a "security conscious environment, the servlet container may return null.". This appears to be what is happening to me - I am getting a null.
Is there a limitation in Tomcat that makes it always "security concious"? It seems that one would want web apps running in the SAME tomcat virtual host to be able to easily share objects, and that this shouldn't pose much of a security threat, since you have control over the webapps on your server.
I can't imagine how a large installation would be feasible if wabapps couldn't ~easily~ share data/objects via their ServletContexts.
I'll try to get my test app running on ServletExec, and let you konw if I get better results.

Many thanks for your help.
Dave

javadoc follows:
------------------------------------------------
getContext public ServletContext getContext(java.lang.String uripath) Returns a ServletContext object that corresponds to a specified URL on the server. This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container. In a security conscious environment, the servlet container may return null for a given URL.Parameters:uripath - a String specifying the context path of another web application in the container.Returns:the ServletContext object that corresponds to the named URL, or null if either none exists or the container wishes to restrict this access.See Also: RequestDispatcher
[ October 01, 2002: Message edited by: Dave Segal ]
David Carew
Greenhorn

Joined: Jul 08, 2002
Posts: 5
Web containers aren't required by the J2EE specs to allow context sharing across different web apps. In Tomcat this is disallowed by default. You can turn it on by using the crossContext attribute of the context element in server.xml
e.g.
<Context path="/myAppContext" crossContext="true" .....
Note this would be for the context of the webapp that is making the call to getContext(...)
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
David - yes, I added that parameter and my code suddenly works like it should. Thanks. Dave
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
You can also set crossContext="true" as the default for all web apps, but I haven't ben able to get this to work. Anybody have any experience with this? Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getContext(uri) in Tomcat