Creating JSP Layouts with Page Tags

For some time now I’ve been following a new approach when creating the basic layout structure for JSP based web applications. The new approach is based on JSP 2.0 tag files and changes the way that the single pages are build up from reusable parts.

The most common way to do so is to use included files with common blocks:

<%@include “”%>

<b>page specific markup</b>

<%@include “”%>

Here the individual pages contain two or more includes which define the page start and end. Often navigation or sidebars or whatever are included in their own files. Changing the output of the title in the header e.g. requires passing data through the page scope or so.

My new approach defines the basic layout as JSP 2.0 tag file. Let’s take a look at an example:

<%@tag description=”page layout” %>
<%@taglib prefix=”c” uri=””%>
<%@attribute name=”head” fragment=”true”%>
<%@attribute name=”title” required=”true” type=”java.lang.String”%>
<%@attribute name=”script” required=”false” type=”java.lang.String”%>
<%@attribute name=”stylesheet” required=”false” type=”java.lang.String”%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”>
<title>${title} – Page Tag Example</title>
<style type=”text/css”>
@import url(“<c:url value=”/style/style.css”/>“);
<c:forTokens var=”item” items=”${stylesheet}” delims=”,”>
@import url(“<c:url value=”/style/”/>${item}“);

<c:forTokens var=”item” items=”${script}” delims=”,”>
<script type=”text/javascript” src=”<c:url value=”/script/”/>${item}“>
<jsp:invoke fragment=”head” />
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<meta http-equiv=”Pragma” content=”no-cache”>
<h1>Page Tag Example</h1>
<ul id=”nav”>
<li><a href=”index.jsp”>Index</a></li>
<li><a href=”page1.jsp”>Page 1</a></li>
<li><a href=”page2.jsp”>Page 2</a></li>
<div id=”content”>
<jsp:doBody />
<div id=”footer”>
<a href=””><img
src=”″ border=”0″
alt=”Valid HTML 4.01 Transitional” height=”31″ width=”8″></a>

The tag defines the example page layout with a page header, a navigation and a footer. There are several attributes to control the page tag from inside the JSP page it is used in:

  • head – Fragment attribute used to add additional HTML markup to the head section of the page
  • title – Required attribute which sets the page specific part of the page title
  • script – Comma separated lists of script files to include from the /script directory
  • stylesheet – Comma separated lists of style sheets to import from the /style directory

The head fragment attribute demonstrates how to setup a fragment attribute which makes it possible to set HTML code as an attribute. To use such a fragment attribute inside a page you need to use the <jsp:attribute> and <jsp:body> tags:

<%@taglib tagdir=”/WEB-INF/tags” prefix=”example”%>
<example:page title=”Page 1″>
<jsp:attribute name=”head”>
<meta name=”author” content=”Sven Helmberger”>
Page 1 – demonstrates the use of the head fragment attribute

If you only use normal (non-fragment) attributes you can leave out the <jsp:attribute> and <jsp:body> tags and specify the rest of the attributes like the title attribute above. Fragment attributes can also be useful to define common code blocks besides the tag body like a page-specific area in a sidebar or so.

You can see that, even with fragment attributes complicating things, the page tag leads to a simple JSP code with high reuse and clear passing of data from the special page code to the common code.


  1. Great example! Who needs facelets and JSF when JSP can do the same!

    • strictly speaking this is more a replacement for Tiles than a replacement for facelets/JSF, but yeah.. I always thought this to be a great improvement in terms of cleanliness without the additional complications.

      Especially since it’s all just there without any need to install aynthing.

1 Pingback

  1. Pingback: Java for PHP Developers | Netcrash

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024

Theme by Anders NorénUp ↑