This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing different WAR resources

 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works perfectly! :-)

You are a gentleman and a scholar. Much obliged Frits :-)))
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2447
96
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic