• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Programmatic out-of-context includes in JSP (for shared code)

I want to be able to share global resources across web sites.  For example, foo.jsp which performs xyz, needs to be able to be included in jsp from several sites which use foo.jsp.  Copying the global resources N times is NOT an option for obvious reasons.

I know about:

<jsp:include page="/foofiles/foo.jsp"/>

and I've read about:
-------
ServletContext testContext = getServletContext();

RequestDispatcher disp = testContext.getRequestDispatcher( "/foofiles/foo.jsp" );

disp.include( request, response );
--------

but it seems that the included resources have to PHYSICALLY reside WITHIN the context of the application you're running.

I've even tried doing the above Java code from within <% jsp %> tags however this produced no output and I don't think the context was being found.  Either that or the found JSP was not being processed and output.  Do included resources create their own output streams???

I've experimented with <context> tags in Tomcat, but to no avail.  Does anyone know how I can include (programmitically using passed or read-in data) files from a cross-context resource.  If this is a limitation of JSP, I consider it a SEVERE limitation.  Doing this is ASP or JHTML on ATG Dynamo is easy and simple -- just create a virtual directory pointing TO anywhere and access it FROM anywhere.  JSP/J2EE seems to make this a burden (or impossible?!?).

Thanks for your responses and solutions.
0
jdestremps
Asked:
jdestremps
  • 6
1 Solution
 
bobbit31Commented:
>  <context> tags in Tomcat

so i'm assuming you tried setting crossContext="true"  in your context?
0
 
kennethxuCommented:
>> but it seems that the included resources have to PHYSICALLY reside WITHIN the context of the application you're running.
correct by default.
0
 
kennethxuCommented:
there are 2 solutions to your problem:

option 1 works when all your contexts are on same servlet cntainer. say you have "foo.jsp" under context "foofiles", and "bah.jsp" in context "bahapp" need to include "foo.jsp". set crossContext="true" for both foofiles and bahapp, then use:

ServletContext fooContext= ServletContext.getContext( "foofiles" );
RequestDispatcher disp = fooContext.getRequestDispatcher( /foo.jsp" );
disp.include( request, response );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kennethxuCommented:
sorry, it should be:
ServletContext fooContext= getServletContext().getContext( "/foofiles" );
RequestDispatcher disp = fooContext.getRequestDispatcher( "/foo.jsp" );
disp.include( request, response );
0
 
kennethxuCommented:
option 2 works cross servers, need to use JSTL <c:import> tag:
http://java.sun.com/products/jsp/jstl/
and here is the tutorial:
http://www.onjava.com/pub/a/pub/a/onjava/2002/05/08/jstl.html?page=2
0
 
kennethxuCommented:
let me know if you have further enquires.
0
 
jdestrempsAuthor Commented:
Thanks Kenneth.  A very timely and accurate response.  I tried it out and it works!  Awesome.
0
 
kennethxuCommented:
glad to know and my pleasure to help :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now