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
(Continued from page 1) Now let's look at test.jsp, which is located in tomcat-demo . It does 3 <c:import> statements and 3 <jsp:include> statements. test.jsp<%@ page contentType="text/html; charset=ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%> <h2>This is tomcat-demo's test.jsp</h2> <table border="1"> <tr> <td valign="top"> <div><c:import url="test-1.jsp"/></div> <c:import url="test-1.jsp" /> <br /> </td> <td valign="top"> <div><jsp:include flush="true" page="test-1.jsp"/></div> <jsp:include flush="true" page="test-1.jsp" /> <br /> </td> </tr> <tr> <td valign="top"> <div><c:import url="http://localhost:8080/tomcat-demo/test-1.jsp"/></div> <c:import url="http://localhost:8080/tomcat-demo/test-1.jsp" /> <br /> </td> <td valign="top"> <div><jsp:include flush="true" page="http://localhost:8080/tomcat-demo/test-1.jsp"/></div> <jsp:include flush="true" page="http://localhost:8080/tomcat-demo/test-1.jsp" /></td> </tr> <tr> <td valign="top"> <div><c:import url="http://localhost:8080/tomcat-demo-2/test-2.jsp"/></div> <c:import url="http://localhost:8080/tomcat-demo-2/test-2.jsp" /></td> <td valign="top"> <div><jsp:include flush="true" page="http://localhost:8080/tomcat-demo-2/test-2.jsp"/></div> <jsp:include flush="true" page="http://localhost:8080/tomcat-demo-2/test-2.jsp" /></td> </tr> </table> If we start up both tomcat-demo and tomcat-demo-2 and we hit tomcat-demo/test.jsp in our browser, we see the following: ![]() As you can see, both the c:import and jsp:include work when we try to pull content into our JSPW from a file within the same project using a relative path (ie, "test-1.jsp"). However, if we try to import content on our page using an absolute URL to a file in the same project (ie, "http://localhost:8080/tomcat-demo/test-1.jsp"), only the c:import works. If we pull content from a URL outside of the same project (ie, "http://localhost:8080/tomcat-demo-2/test-2.jsp"), once again only the c:import works. Notice, however, that for the content pulled into the test.jsp using c:import from the URL outside of the same project ("http://localhost:8080/tomcat-demo-2/test-2.jsp"), only the image with the absolute URL shows up. Why is this? Right-click the image and go to Properties. ![]() As you can see below, since the URL to the world.gif image was a relative URL, it is now relative to the tomcat-demo project instead of the tomcat-demo-2 project. Since the image doesn't exist in the tomcat-demo project, no image can be displayed. ![]() The JSTLSW c:import tag is extremely powerful, as we have seen in this demo. It's a great way to pull content into a JSP, in particular from a site outside of your web application, since this capability is not available using the jsp:include tag. However, as we saw with the example of the relative URL to a graphic, care must be taken when using c:import. |