Thursday, April 28, 2011

jsp:include


<%@ include %> and <jsp:include />

The include directive is used to include text and/or code at translation time of a
JSP. The include directive always follows the same syntax, <%@ include
file="relativeURL" %>, where the value of relativeURL is replaced with the file
to be inserted. Files included must be part of a Web Application. Since include
directives take place at translation time, they are the equivalent of directly
including the source code in the JSP before compilation and do not result in per-
formance loss at runtime.
Server-side includes are a commonly used feature of JSP. Includes allow the
same repetitious bit of code to be broken out of multiple pages and have one
instance of it included with them all. A good example to use is including a
common header and footer with multiple pages of content. Usually this tech-
nique is used to keep a site’s navigation and copyright information correct and
maintainable for all individual pages on the site.As an example take the following
two files, header.jsp and footer.jsp.
The header.jsp file (fig 1) includes information that is to appear at the
top of a page. It includes site navigation and other miscellaneous information.
This header also tracks how many people have visited the site since the last time
the Web Application was reloaded by reusing code from PageCounter.jsp.
fig 1 header.jsp
<%! int pageCount = 0;
void addCount() {
pageCount++;
}
%>
<% addCount(); %>
<html>
<head>
<title>Header/Footer Example</title>
</head>
<body>
<center>
<h2>Servlets and JSP the J2EE Web Tier</h2>
<a href="http://www.jspbook.com">Book Support Site</a> - 
<a href="ShowSource">Sites Source code</a><br>
This site has been visited <%= pageCount %> times.
</center>
<br><br>


The footer.jsp file (fig 2) includes information that is to appear at
the very bottom of a page. It includes copyright information, disclaimers, and
any other miscellaneous information.
fig 2 footer.jsp
<br><br>
<center>Copyright &copy; 2003
</center>
</body>
</html>
By themselves header.jsp and footer.jsp do not do much, but when com-
bined with some content, a full page can be generated. For this example the
content does not matter. Arbitrarily make up a JSP, but be sure to include
header.jsp at the top of the page and footer.jsp at the bottom. fig 3 pro-
vides an example of such a page.
fig 3 content.jsp
<%@ include file="header.jsp" %>
Only the content of a page is unique. The same header and footer 
are reused from header.jsp and footer.jsp by means of the include 
directive
<%@ include file="footer.jsp" %>


Save all three files, header.jsp, footer.jsp, and  content.jsp, in the base
directory of the jspbook Web Application and browse to http://127.
0.0.1/jspbook/content.jsp. All three files are mashed together at translation
time to produce a Servlet that includes the contents of header.jsp, content.jsp,
and footer.jsp in the appropriate order. Figure shows a browser rendering
of the output.




So what is the advantage of this approach over combining header.jsp,
content.jsp, and footer.jsp all in one file in the first place? As the files are set
up now, many more content pages can be created that reuse the header and
footer. The header and footer can also be changed at any given time, and the
changes are easily reflected across all of the JSP

No comments:

Post a Comment