How do I import another web page into my JSP and display its source code?
Author: Deron Eriksson
Description: This Java tutorial describes how to use the c:import and c:out JSTL tags to import another web page into a JSP and display it and its source code.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20


This tutorial will utilize JSTLSW tags. The JSTL libraries can be downloaded at http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi. I placed the jstl-1.1.2.jar and standard-1.1.2.jar files in my WEB-INF/lib directory and added them to my project build path.

'tomcat-demo' project

The index.jsp page imports the test.html file twice using the c:import tag. The first time, it imports test.html and names it with the variable testHtml. It outputs the content of testHtml, using the c:out tag with escapeXml set to false. The second time, it imports test.html and names it with the variable testHtmlSource. It outputs testHtmlSource using the c:out tag with escapeXml set to true. The first display is placed in a div tag, while the second display is placed in a pre tag.

index.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" %>

<h3>test.html</h3>
<div style="border: 2px solid black;">
<c:import var="testHtml" url="/test.html" />
<c:out value="${testHtml}" escapeXml="false" />
</div>

<h3>test.html source code</h3>
<pre style="border: 2px solid black;">
<c:import var="testHtmlSource" url="/test.html" />
<c:out value="${testHtmlSource}" escapeXml="true" />
</pre>


The test.html file is shown here:

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
</head>
<body>
This is a test page.
</body>
</html>

If we fire up the project and hit index.jsp, we see that first the test.html file is displayed normally since the c:out escapeXml attribute is set to false. Next, we see the test.html source code displayed, since escapeXml is set to true.

index.jsp page