aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Accessing different WAR resources Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Accessing different WAR resources" Watch "Accessing different WAR resources" New topic
Author

Accessing different WAR resources

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

[Talking about Servlets 3.0.]

Just want to make it straight: I cannot access any resources from another WAR within the same servlet container, right?

If I want to access a resource within a web-app but NOT within a JAR file inside /WEB-INF/lib, I can use the ServletContext's methods: getResource(-)/getResourceAsStream(-).
If I want to access a resource within a web-app which CAN be located within a JAR file inside /WEB-INF/lib, I can use ClassLoader's methods: getResource(-)/getResourceAsStream(-).

But there is no way I can access a resource of another webapplication deployed on the same web-container.

Is this correct?


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

Hi Pedro,

Just want to make it straight: I cannot access any resources from another WAR within the same servlet container, right?

I think you can.

First of all you can get to a ServletContext of another web-application (in the same servlet container) by using the method:
ServletContext getContext(String uripath)
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.

After that you can call the getResource() method on the foreign ServletContext to obtain a foreign resource.
URL getResource(String path) throws MalformedURLException
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.


Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks Frits!

Shame on me for not reading the API in details...

I've tried to execute it, and I've got two web applications in container:

/ocmjd
/test


Now in /ocmjd servlet I execute this code:



And I get:
1.org.apache.catalina.core.ApplicationContextFacade@1f217ec
2.org.apache.catalina.core.ApplicationContextFacade@1f217ec
3.null


The same if I execute the same code (but change /ocmjd with /test) from the /test application. It's always null...

Do I have to set something in the tomcat itself? I'm wondering about this part:

In a security conscious environment, the servlet container may return null for a given URL.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

Don't worry: sometimes reading the API's can be quite abstract

Do I have to set something in the tomcat itself?

Yes, set the crossContext=true in the context.xml file

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

That works perfectly! :-)

You are a gentleman and a scholar. Much obliged Frits :-)))
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing different WAR resources
 
Similar Threads
whose bug?weblogic or mine,i think it's weblogic's?
getResource() n getResourceAsStream()
Path to the deployed war class folder
reading a file located on the server
Where to place non java files in a WAR archive?