tags:

views:

63

answers:

1

I'm trying to construct a java web app along modular principles, with some common resources (JSPs, mainly) in one WAR, and some custom resources in another. This means JSPs scattered across different WARs.

Now JavaEE frowns upon this sort of shenanigans, and wants you to put everything in one place. My current workaround to this is to have an Eclipse-triggered Ant script which copies the content of one WAR into the other, but this is not a pleasant solution (it's fragile and too IDE-dependent).

Ideally, what I'd like to be able to do is for a servlet to forward to a JSP located in a different WAR to one in which it is itself deployed. This would allow greater freedom in how I assemble my WARs. However, the RequestDispatcher does not seem to support such things.

Another possibility is to use <c:import>, which does allow resources to be imported from a different WAR (with some caveats). This would probably allow me to have a "hook" JSP in one WAR, which then drags in the required JSP from another. This is a bit clunky, though, and the fact that <c:import> permits it shows that the underlying servlet API does also. But how do I access that functionality via the RequestDispatcher in a servlet?

+2  A: 

You can, with the following steps:

  • obtain the foreign context using ServletContext.getContext(contextPath)

  • get the RequestDispatcher of the foreign ServletCotnext.

  • in META-INF/context.xml set crossContext="true" (perhaps tomcat-specific)

Bozho
Gack! I hadn't seen the `getContext` method, that's the fella!
skaffman
Isn't this only for Tomcat?
JoseK
the final setting - yes, perhaps. But other containers should have similar settings.
Bozho