JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages
By Dustin Marx, JavaWorld.com, 11/29/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
JavaServer Pages (JSPs) technology is an extension of Java servlet technology and combines HTML and Java code into a single
file. While Java servlet technology focuses on Java classes capable of generating HTML output with
PrintWriter.println() statements, JSP technology abstracts this concept to a higher level. With JavaServer Pages, a Web developer can write static
HTML pages and simply add Java code in those sections of the page that need to be dynamically generated. While this flexibility
enables rapid development of simple Web applications, it can be abused, resulting in unnecessarily complex applications that
are difficult to maintain, reuse, and enhance.
To avoid needlessly complex applications, follow the practices I present in this article:
- Separate HTML from Java
- Place business logic in JavaBeans
- Factor general behavior out of custom tag handler classes
- Favor HTML in Java handler classes over Java in JSPs
- Use an appropriate inclusion mechanism
- Use a JSP template mechanism
- Use stylesheets
- Use the MVC pattern
- Use available custom tag libraries
- Determine the appropriate level of XML compliance
- Use JSP comments in most cases
- Follow HTML best practices
- Utilize the JSP exception mechanism
These tips will help you write JSPs that are reusable and easy to maintain.
Separate HTML from Java
It can be tempting to throw all Java and HTML code necessary for a Webpage into a single JSP file. In simple system development,
such an approach makes it easy for someone new to the system to locate all relevant code in one place and understand how it
all interacts. However, this approach becomes burdensome and costly when the application grows more complex and more developers
become involved.
Combining HTML and Java in the same source code can make the code significantly less readable. To enhance software readability,
developers often use indentation; but mixing HTML and Java scriptlets in the same file can make useful indentation extremely
difficult to maintain.
Many Web development methodologies and architectures now emphasize the separation of HTML from Java code so different developers
can focus on their strengths. Properly separating Java and HTML, including HTML-like JSP tags and custom tags, allows Web
designers and HTML coders to work on the HTML (presentation) aspects, while Java developers work on the application's Java
(processing logic) portions. Java developers focus on business logic as they implement the behavior behind the custom tags;
Web designers then use these custom tags just as they use ordinary HTML tags.
Applications with Java properly separated from HTML are more reusable because the Java components are not tied to a Web browser
and can be used by other parts of the application. In addition, maintainability is enhanced because of the increased modularization
that comes from Java/HTML separation.
Placing business logic in JavaBeans also promotes stronger applications. I'll explain how next.
Place business logic in JavaBeans
Java code included directly inside a JSP is not as readily accessible to other JSPs as Java code contained within a JavaBean.
Common behavior and business logic placed in JavaBeans can not only be used by other JSPs but also by other portions of the
application. That is because JavaBeans are merely Java classes that satisfy some basic conventions (such as a constructor
with no arguments and public get/set methods for private data members) and can be used as any other Java class. Note that Enterprise JavaBeans (EJBs) are also
useful for storing behaviors and data common to all components of the application.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- The JavaServer Pages Syntax Reference provides a quick summary of basic JSP tags
http://java.sun.com/products/jsp/tags/11/tags11.html
- "Hans's Top Ten JSP Tips," Hans Bergsten (O'Reilly, November 2000) contains 10 useful tips for JSP development, including discussions on the two include mechanisms and when
to use forward and when to use redirect
http://java.oreilly.com/news/jsptips_1100.html
- Aurora Information Systems' "JSP Design Notes" discusses using EJBs with JavaServer Pages
http://www.aurorainfo.com/wp8/
- These excerpts from Professional JSP, Simon Brown et al. (Wrox Press, April 2001; ISBN1861004958) provide useful information on JSPs, the relationship of JSPs
to the rest of J2EE, and architectural considerations when using JSPs
http://www.enterprisedeveloper.com/wrox/profjsp/contents.htm
- The Sun J2EE Blueprints documentation presents some best practices and architectural considerations for developing J2EE applications.
JSP developers often find the "Web Tier" portion most useful
http://java.sun.com/blueprints/enterprise/
- This section of Sun's J2EE Blueprints answers common questions related to J2EE applications' Web tiers. This section also
discusses the differences between the two JSP include mechanisms and offers suggestions for deciding which mechanism to use
for a given situation
http://java.sun.com/j2ee/blueprints/web_tier/qanda/index.html#directive
- This Blueprints description of the Model-View-Controller architecture is helpful for understanding MVC
http://java.sun.com/j2ee/blueprints/design_patterns/model_view_controller/index.html
- I highly recommend using and extending Struts to facilitate MVC architectures with JSPs
http://jakarta.apache.org/struts/index.html
- Part of Struts, the template custom tag library is a subproject under Apache's Jakarta Project
http://jakarta.apache.org/struts/struts-template.html
- "Strut Your Stuff with JSP Tags," Thor Kristmundsson (JavaWorld, December 2000) describes how to use and extend the open source Struts JSP tag library
http://www.javaworld.com/javaworld/jw-12-2000/jw-1201-struts.html
- "Encapsulate Reusable Functionality in JSP Tags," Simon Brown (JavaWorld, August 2000) shows how to build your own custom JSP tags with Tomcat
http://www.javaworld.com/javaworld/jw-08-2000/jw-0811-jsptags.html
- "JSP Templates," David Geary (JavaWorld, September 2000) discusses using templates to encapsulate Web design and encourage modularization
http://www.javaworld.com/javaworld/jw-09-2000/jw-0915-jspweb.html
- "Understanding JavaServer Pages Model 2 Architecture," Govind Seshadri (JavaWorld, December 1999) discusses the Model 2 architecture and MVC
http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html
- In "E++A Pattern Language for J2EE Applications, Part 1" (JavaWorld, April 2001), Bin Yang overviews the E++ pattern language and explains how MVC and JSPs fit into the J2EE framework process
described by E++
http://www.javaworld.com/javaworld/jw-04-2001/jw-0420-eplus.html
- Core J2EE PatternsBest Practices and Design Strategies, Deepak Alur, John Crupi, and Dan Malks (Prentice Hall PTR, 2001; ISBN0130648841) discusses several design patterns related
to application presentation tiers
http://www.amazon.com/exec/obidos/ASIN/0130648841/javaworld
- Here's a good starting point for locating publicly available custom tag libraries
http://jsptags.com/
- Taglibs is an Apache subproject that focuses on JSP custom tag libraries (note that this subproject is different from Struts,
which has its own custom tag libraries)
http://jakarta.apache.org/taglibs/index.html
- The JSP specifications (currently 1.1 Final Release and 1.2 Final Release) are available for download. They are highly recommended
reading for understanding the steps needed to convert traditional shorthand JSPs to XML-compliant JSP documents
http://java.sun.com/products/jsp/download.html#specs
- Although the JavaServer Pages specifications are straightforward, Hans Bergsten provides a nice overview of 1.2's features
in this two-part series
- "JSP 1.2Great News for the JSP Community, Part 1" (OnJava.com, October 2001)
- "JSP 1.2Great News for the JSP Community, Part 2" (OnJava.com, November 2001)
How to solve this exceptionBy Anonymous on November 29, 2008, 5:05 amString index out of range : 34
Reply | Read entire comment
View all comments