?
Solved

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

Posted on 2003-03-05
8
Medium Priority
?
263 Views
Last Modified: 2013-11-18
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
Comment
Question by:jdestremps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
8 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8076390
>  <context> tags in Tomcat

so i'm assuming you tried setting crossContext="true"  in your context?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076439
>> but it seems that the included resources have to PHYSICALLY reside WITHIN the context of the application you're running.
correct by default.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076447
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
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.

 
LVL 14

Accepted Solution

by:
kennethxu earned 400 total points
ID: 8076491
sorry, it should be:
ServletContext fooContext= getServletContext().getContext( "/foofiles" );
RequestDispatcher disp = fooContext.getRequestDispatcher( "/foo.jsp" );
disp.include( request, response );
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076506
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076522
let me know if you have further enquires.
0
 

Author Comment

by:jdestremps
ID: 8077100
Thanks Kenneth.  A very timely and accurate response.  I tried it out and it works!  Awesome.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8077134
glad to know and my pleasure to help :)
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question