How do I import content from another site into my JSP?
Author: Deron Eriksson
Description: This Java tutorial describes how to use the JSTL c:import tag to import content into a JSP.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 >

The <jsp:include> tag allows you to import content onto one of your JSPsW, but the content needs to reside within your web application. However, JSTL's core library features the <c:import> tag that allows you to import content from outside of your web application.

To compare <jsp:include> and <c:import>, I'll use a 'tomcat-demo' project and a 'tomcat-demo-2' project. The tomcat-demo project features a test.jsp that is our main test page. Since it utilizes JSTLSW, I included the JSTL and standard jarW files in the project. The tomcat-demo project also features a test-1.jsp and a tools.gif graphic. The other project, tomcat-demo-2, has a test-2.jsp and a world.gif graphic.

'tomcat-demo' project

The test-1.jsp file is shown below. It displays tools.gif using a relative URL and an absolute URL with a light green background.

test-1.jsp

<table style="background-color: lightgreen">
	<tr>
		<td colspan="2">This is tomcat-demo's test-1.jsp</td>
	</tr>
	<tr>
		<td>Relative image:<img src="tools.gif" /></td>
		<td>Absolute image:<img src="http://localhost:8080/tomcat-demo/tools.gif" /></td>
	</tr>
</table>

The tools.gif graphic is shown below:

tools.gif

tools.gif

If we start up tomcat-demo and we hit test-1.jsp, we can see that both the relative and absolute URLs work:

test-1.jsp in web browser

The test-2.jsp file is shown below. It displays world.gif using a relative URL and an absolute URL with a light blue background.

test-2.jsp

<table style="background-color: lightblue">
	<tr>
		<td colspan="2">This is tomcat-demo-2's test-2.jsp</td>
	</tr>
	<tr>
		<td>Relative image:<img src="world.gif" /></td>
		<td>Absolute image:<img src="http://localhost:8080/tomcat-demo-2/world.gif" /></td>
	</tr>
</table>

The world.gif graphic is shown below:

world.gif

world.gif

If we start up tomcat-demo-2 and we hit test-2.jsp, we can see that both the relative and absolute URLs work:

test-2.jsp in web browser

(Continued on page 2)

Page:    1 2 >