CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



4.4 JSP Page Design

JSP pages are unique in that they can contain both presentation logic and content. They provide a variety of options for designing applications that are easy to maintain and extend. The options available for binding content to logic include JavaBeans components, custom tags, and scriptlets. The following sections describe some of these options and recommend when to use each.


4.4.1 JavaBeans Components

JavaBeans technology is useful for building portable and reusable components that can used in conjunction with JSP technology. There are many ways to use JavaBeans components within an application.

One way to use JavaBeans components is as data-centric model objects. If these beans are created specifically to manipulate and return data, they can be used by multiple views of an application and by many different clients at one time.

In conjunction with a front component, a JavaBeans component can be used as a controller. The sample application uses a JavaBeans component to process all requests received from the front component and pass them along to the appropriate page.

Page-specific JavaBeans components provide the logic to process data and generate a result for a particular page. The disadvantage of using these types of beans is that they are more difficult to reuse.


4.4.2 Custom Tags

Custom tags are the mechanism provided by JSP technology for defining customized, declarative, and modular functionality for use by JSP pages. Custom tags are delivered as tag libraries and are imported into a JSP page using the taglib directive. Once imported, a custom tag can be used in the page using the prefix defined by the directive.

Custom tags provide the same functionality as JavaBeans components. However, unlike JavaBeans components which must first be declared and then accessed using get and set methods, custom tags work with a page by obtaining initialization information through parameters defined when the tag is created. Custom tags have access to the Web container and all the objects available to JSP pages. Custom tags can modify the generated response. Custom tags can also be nested within one another, allowing for complex interactions with a JSP page.

Custom tags are portable and reusable. They are written in the Java programming language which allows them to be used across platforms and Web containers. If you plan on reusing custom tags, you should take care to design tags that are not application-specific.

Custom tags are ideal for iterating through data and generating the HTML code needed to render a page. For example, a custom tag could take the data contained within a shopping cart JavaBeans component and generate the HTML to render the shopping cart. Proper use of custom tags can reduce, if not eliminates, the amount of Java language code used in a JSP page to generate dynamic content. Portions of a page that require logic, such as looping or state display, are also good places to use custom tags.

The template page in Code Example 4.1 provides a familiar interface to a designer or HTML authoring tools. The custom tags in the page appear as HTML tags. In contrast, Java language code can get corrupted by a tool or page designer not familiar with the Java programming language.

In addition to rendering HTML, custom tags can be used to process data. This can reduce the amount of Java language code needed within an application and make portions of an application configurable by a page designer.

Code Example 4.2 shows one such use for custom tags: as a switch statement for processing user input. The CreateTemplate custom tag creates a Template object and places it in the request containing the data necessary to render the current page. The CreateTemplate tag has nested tags that correspond to case statements in a switch statement. These nested tags include Screen tags which in turn have Parameter tags nested within them. Depending on the current screen ID obtained from the ScreenManager, the proper Template object will be created and the parameters will be set to reflect the appropriate page components. The Template object is processed by the JSP templating mechanism illustrated in Code Example 4.1. Notice that expressions within the tag parameters are used to interact with sample application data.


<j2ee:CreateTemplate template="template" 
	screen="<%=screenManager.getCurrentScreen(request)%>">
	<j2ee:Screen screen="<%=ScreenNames.MAIN_SCREEN%>">
		<j2ee:Parameter parameter=
			"HtmlTitle" value="Welcome to Java(TM) Pet Store Demo" 
				direct="true"/>
		<j2ee:Parameter parameter="HtmlBody" 
			value="/index.jsp" direct="false"/>
	</j2ee:Screen>

	<j2ee:Screen screen="<%=ScreenNames.SIGN_IN_SUCCESS_SCREEN%>">
		<j2ee:Parameter parameter="HtmlTitle" 
			value="Welcome" direct="true"/>
		<j2ee:Parameter parameter="HtmlBody" 
			value="/signinsuccess.jsp" direct="false"/>
	</j2ee:Screen>
</j2ee:CreateTemplate>
Code Example 4.2 Data-Centric Custom Tags

4.4.3 Using Scriptlets and Expressions

When designing a Web site with interactive and dynamic content, it may be necessary to use small portions of code to generate content. Scriptlets are small fragments of scripting code whose language is defined by the language parameter in the JSP page directive. Expressions are like scriptlets, except that they are played directly in the response.

To make code easier to read and maintain, we recommend that JSP pages be used mainly for presentation. While a major portion of an application could be developed in JSP technology, placing large amounts of code in JSP pages makes them more difficult to update and can be confusing to page designers.

We recommend including Java code only when necessary. JavaBeans components and custom tags provide a means of adding functionality while avoiding scriptlets. A developer can use expressions with JavaBeans components or custom tags to generate dynamic content.



CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.