• 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

Accessing different WAR resources

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 623
1
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic