JSP templates - JavaWorld September 2000
Tutorial Details:
JSP templates
JSP templates
By: By David Geary
Use JSP templates to encapsulate Webpage layout and encourage modular design
lthough Web development tools are rapidly progressing, they still lag behind most graphical user interface (GUI) toolkits such as Swing or VisualWorks Smalltalk. For example, traditional GUI toolkits provide layout managers, in one form or another, that allow layout algorithms to be encapsulated and reused. This article explores a template mechanism for JavaServer Pages (JSP) that, like layout managers, encapsulates layout so it can be reused instead of replicated.
Because layout undergoes many changes over the course of development, it's important to encapsulate that functionality so it can be modified with minimal impact to the rest of the application. In fact, layout managers demonstrate an example of one of the tenets of object-oriented design: encapsulate the concept that varies, which is also a fundamental theme for many design patterns.
JSP does not provide direct support for encapsulating layout, so Webpages with identical formats usually replicate layout code; for example, Figure 1 shows a Webpage containing header, footer, sidebar, and main content sections.
Figure 1. Webpage layout
Click on thumbnail to view full image (22 KB)
The layout of the page shown in Figure 1 is implemented with HTML table tags:
Example 1. Including content
JSP Templates
| <%@ include file='sidebar.html'%> |
| <%@ include file='header.html'%> |
| <%@ include file='introduction.html'%> |
| <%@ include file='footer.html'%> |
|
In the example listed above, content is included with the JSP include directive, which allows the content of the page to vary -- by changing the included files -- without modifying the page itself. However, because layout is hard coded, layout changes require modifications to the page. If a Website has multiple pages with identical formats, which is common, even simple layout changes require modifications to all of the pages.
To minimize the impact of layout changes, we need a mechanism for including layout in addition to content; that way, both layout and content can vary without modifying files that use them. That mechanism is JSP templates.
Using templates
Templates are JSP files that include parameterized content. The templates discussed in this article are implemented with a set of custom tags: template:get , template:put , and template:insert . The template:get tag accesses parameterized content, as illustrated in Example 2.a, which produces Webpages with the format shown in Figure 1.
Example 2.a. A template
<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
< template:get name='title' />
|
| < template:get name='header'/> |
|
|
|
Example 2.a is nearly identical to Example 1, except we use template:get instead of the include directive. Let's examine how template:get works.
template:get retrieves a Java bean with the specified name from request scope. The bean contains the URI (Uniform Resource Identifier) of a Web component that's included by template:get . For example, in the template listed in Example 2.a, template:get obtains a URI -- header.html -- from a bean named header in request scope. Subsequently, template:get includes header.html .
template:put puts the beans in request scope that are subsequently retrieved by template:get . The template is included with template:insert . Example 2.b illustrates the use of the put and insert tags:
Example 2.b. Using the template from Example 2.a
<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
The insert start tag specifies the template to be included, in this case the template listed in Example 2.a. Each put tag stores a bean in request scope and the insert end tag includes the template. The template subsequently accesses the beans as described above.
A direct attribute can be specified for template:put ; if direct is set to true , the content associated with the tag is not included by template:get , but is printed directly to the implicit out variable. In Example 2.b, for example, the title content -- JSP Templates -- is used for the window title.
Websites containing multiple pages with identical formats have one template, such as the one listed in Example 2.a, and many JSP pages, such as Example 2.b, that use the template. If the format is modified, changes are restricted to the template.
Another benefit of templates and including content in general is modular design. For example, the JSP file listed in Example 2.b ultimately includes header.html , listed in Example 2.c.
Example 2.c. header.html
Because header.html is included content, it does not have to be replicated among pages that display a header. Also, although header.html is an HTML file, it does not contain the usual preamble of HTML tags such as or because those tags are defined by the template. That is, because the template includes header.html , those tags should not be repeated in header.html .
Note: JSP provides two ways to include content: statically, with the include directive, and dynamically, with the include action. The include directive includes the source of the target page at compile time and is equivalent to C's #include or Java's import . The include action includes the target's response generated at runtime.
Like the JSP include action, templates include content dynamically. So, although the JSP pages in Example 1 and Example 2.b are functionally identical, the former statically includes content, whereas the latter dynamically includes it.
Optional content
All template content is optional, which makes a single template useful to more Webpages. For example, Figure 2.a and Figure 2.b show two pages -- login and inventory -- that use the same template. Both pages have a header, footer, and main content. The inventory page has an edit panel (which the login page lacks) for making inventory changes.
Figure 2.a. A login form
Click on thumbnail to view full image (24 KB)
Figure 2.b. An inventory page
Click on thumbnail to view full image (42 KB)
Below, you'll find the template shared by the login and inventory pages:
<%@ taglib uri='template.tld' prefix='template' %>
...
...
The inventory page uses the template listed above and specifies content for the edit panel:
<%@ taglib uri='template.tld' prefix='template' %>
<%@ taglib uri='security.tld' prefix='security' %>
...
content=' /editPanelContent.jsp '/>
...
In contrast, the login page does not specify content for the edit panel:
<%@ taglib uri='template.tld' prefix='template' %>
content='/login.jsp'/>
Because the login page does not specify content for the edit panel, it's not included.
Role-based content
Web applications often discriminate content based on a user's role. For example, the same JSP template, which includes the edit panel only when the user's role is curator, produces the two pages shown in Figures 3.a and 3.b.
Figure 3.a. Inventory page for curators
Click on thumbnail to view full image (27 KB)
Figure 3.b. Inventory page for other users
Click on thumbnail to view full image (21 KB)
The template used in Figures 3.a and 3.b uses template:get 's role attribute:
<%@ taglib uri='template.tld' prefix='template' %>
...
...
The get tag includes content only if the user's role matches the role attribute. Let's look at how the tag handler for template:get uses the role attribute:
public class GetTag extends TagSupport {
private String name = null, role = null;
...
public void setRole(String role) { this.role = role; }
...
public int doStartTag() throws JspException {
...
if(param != null) {
if( roleIsValid() ) {
// include or print content ...
}
}
...
}
private boolean roleIsValid() {
return role == null || // valid if role isn't set
((javax.servlet.http.HttpServletRequest)
pageContext.getRequest()).isUserInRole(role);
}
}
Implementing templates
The templates discussed in this article are implemented with three custom tags:
template:insert
template:put
template:get
The insert tag includes a template, but before it does, put tags store information -- a name, URI, and Boolean value specifying whether conten
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: JSP templates - JavaWorld September 2000
View Tutorial: JSP templates - JavaWorld September 2000
Related
Tutorials:
JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages |
Boost Struts with
Boost Struts with XSLT and XML |
JSP 2.0: The New Deal, Part 3
JSP 2.0: The New Deal, Part 3
More Flexible JSP Document Format Rules
The JSP specification supports two types of JSP pages: regular JSP pages containing any type of text or markup, and JSP Documents, which are well-formed XML documents; i.e., docum |
Template-Based Code Generation with Apache Velocity, Part 1
Template-Based Code Generation with Apache Velocity, Part
I'm going to discuss template-based code generation, explain basic concepts related to templates and transformations, and demonstrate the huge benefits they can bring in code generation. |
WebOnSwing 1.0 beta
WebOnSwing 1.0 beta
WebOnSwing is a revolutionary multiple environment application framework that allows you to create web applications in the same way you develope a desktop one. You dont need to use JSP files, special tags, XML files, requests, posts |
Java Servlets: Design Issues
This article covers the principal concepts associated with servlets. This article examines some of the design issues, and offers some guidelines on the applicability of Java servlets for web based application development. |
JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages.
With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page.. |
BEA WebLogic Portal JSP Tag Libraries
The BEA WebLogic Portal includes four JSP tag libraries that are used by the portal's JSP pages. |
JSPTags.com offers JSP developers a directory of resources.
JSPTags.com offers JSP developers a directory of resources related to JavaServer Pages, Servlets and Java. As the name JSPTags.com implies, special interest is given to JSP Tag Libraries. Many developers are working with and designing new JSP Tag Librarie |
JSP Format Bean Library
JSP Format Bean Library is a collection of beans which support Java Server Pages(JSP). JSP allows the HTML developer to embed Java into a page. There are a number of common operations in a scripted page that would be tedious or complex without additional |
FreeMarker FreeMarker 2.3.1 an open-source HTML template engine.
FreeMarker provides an easy way to get data from Java servlets into Web pages, and helps you keep graphic design separate from application logic. To use it, you encapsulate HTML in templates. |
JSP Tutorial
Adding dynamic content via expressionsAs we saw in the previous section, any HTML file can be turned into a JSP file by changing its extension to .jsp. Of course, what makes JSP useful is the ability to embed Java. Put the following text in a file wit |
JSP Tags
JSP tags do not use <%, but just the < character. A JSP tag is somewhat like an HTML tag. JSP tags can have a "start tag", a "tag body" and an "end tag". The start and end tag both use the tag name, enclosed in < and > characters. The end starts with |
Advanced Features of JSP Custom Tag Libraries
In this article, the second in the JSP custom tag libraries series, we will cover advanced JSP features and how to use them. |
JSP templates. Use JSP templates to encapsulate Webpage layout and encourage modular design
Window toolkits typically provide a layout mechanism that positions widgets in a container. For example, AWT and Swing have layout managers, and VisualWorks Smalltalk has wrappers. This article presents a template mechanism for JSP that allows layout to b |
Reuse Tiles and Simplify UI
JavaServer Pages (JSP) technology supports application object reuse through includes, which allow other files (including other JSPs) to be sourced into a JSP file either at compile time or dynamically at application runtime. This is great for abstracting |
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java
Struts
Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open |

JSP Hosting |
Introduction to the JSP Java Server Pages
Introduction to the JSP Java Server Pages
Welcome to JSP Section
Introduction To JSP
Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database |
JSP Architecture
JSP Architecture
JSP ARCHITECTURE
J PS pages are high level extension of servlet and it enable the developers to embed java code in html pages. JSP files are finally compiled into a servlet by the JSP engine. Compiled servlet is used by the engine |
|
|
|