Dynamically including JSP from one Web module to another
 Technote (troubleshooting)
 
Problem(Abstract)
According to JavaServer Pages™ (JSP™) Specification 1.2, JSP of one Web module can not include JSP from other Web modules using the following tag:
<jsp:include page="{relativeURL | <%= expression %>}"  flush="true| false" />
 
Cause
According to JSP Specification 1.2 for the <jsp:include> directive, relativeURL cannot contain a protocol name, port number, or domain name. It can be absolute or relative to the current JSP page. So JSP can only include JSP of its own Web module.
 
Resolving the problem
If there is a need to include JSP from one Web module to JSP of another Web module, the preceding limitation can be overcome by adding the following example code within the including JSP.


Example code
If the file, including.jsp, of TestApp1Web Web module wants to include the file, includeme.jsp, of TestApp2Web Web module, add the following piece of code in the file, including.jsp:

<%
ServletContext context = getServletContext().getContext("TestApp2Web");
RequestDispatcher dispatcher = context.getRequestDispatcher("includeme.jsp");
out.flush();
dispatcher.include(request,response);
%>


Explanation of example code
The first line obtains a ServletContext from the URI parameter passed to the getContext() method.

In the second line, a new ServletContext object gets the RequestDispatcher for the JSP to be included ("includeme.jsp").

In the third line, calling out.flush() flushes content in the JSP buffer in response. This is important to ensure that the response is in the desired sequence.

Finally, after RequestDispatcher is obtained, the include() method is called to include the target JSP (includeme.jsp).
 
 
Cross Reference information
Segment Product Component Platform Version Edition
Application Servers Runtimes for Java Technology Java SDK
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > JSP
Operating system(s): Windows
Software version: 5.1.1
Software edition:
Reference #: 1174476
IBM Group: Software Group
Modified date: Jul 16, 2004