More JSP best practices
JSP advancements ease development of standardized, highly maintainable JSP-based applications
By Dustin Marx, JavaWorld.com, 07/25/03
Since my article "JSP Best Practices" first appeared in JavaWorld in late 2001, the use of JavaServer Pages (JSP) technology has grown dramatically. Numerous tools now make Webpage development
with JSP technology easier than ever. The best practices I recommended in my previous article still apply. With new available
features, tools, and JSP development ideas, however, you may employ several additional best practices for even smoother development
of highly maintainable JSP-based applications. This article outlines some best practices that utilize the most significant
new features, tools, and ideas:
- Begin writing JSP documents
- Employ JSP code conventions
- Use the appropriate scope
- Carefully manage session scope
- Take advantage of JavaServer Pages Standard Tag Library (JSTL)
- Take advantage of servlet filters (Intercepting Filter pattern)
- Document the "APIs" of your JSP pages and documents
- Precompile your JSP pages and documents
- Organize your files and directories for easy development and deployment
- Carefully consider using proprietary, vendor-specific features
- Use Extensible Hypertext Markup Language (XHTML) syntax for HTML tags
Begin writing JSP documents rather than JSP pages
The JSP specification supports JSP pages as well as JSP documents. The main differentiator between the two is their level
of XML compliance. JSP pages use the traditional or "shorthand" syntax, while JSP documents are completely XML-compliant.
JSP documents are sometimes referred to as "JSP pages using XML syntax," but I will distinguish them here as JSP pages and
JSP documents.
I prefer JSP documents for several reasons including:
- You can easily verify JSP documents as well-formed XML/HTML.
- You can validate JSP documents against an XML Schema.
- You can readily write and parse JSP documents using standard XML tools.
- You can readily write or present JSP documents in alternate forms using XSLT (Extensible Stylesheet Language Transformations).
See the "JSP Documentation with XSLT" sidebar for an example.
- JSP uses XML-compliant include and forward actions, as well as custom tags, so making the entire document XML-compliant leads
to increased code consistency.
- JSP documents require slightly more developer discipline than JSP pages, but your reward is more readable and maintainable
documents, especially if you are new to JSP.
See "Writing JSPs in XML Using JSP 1.2" for more information on creating JSP documents and their advantages.
One significant disadvantage of JSP documents is that no XML-compliant version of JSP comments exists. A JSP document developer
can use client-side (HTML-/XML-style) comments or embed Java comments in scriptlets, but there is no JSP document equivalent
to <%-- -->. This is a disadvantage because the other two commenting styles JSP documents can use have their own drawbacks. You can see
the client-side comments in the rendered page using the browser's View Source option. The Java comments in scriptlets require
placing Java code directly in the JSP document.
Throughout the rest of this article, I refer generically to JSPs to imply both JSP pages and JSP documents, because the best
practices I discuss generally apply to both JSP types.
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
Next >
Discuss
Start a new discussion or jump into one of the threads below:
Resources
- Read the original "JSP Best Practices" article by Dustin Marx (JavaWorld, November 2001)
http://www.javaworld.com/javaworld/jw-11-2001/jw-1130-jsp.html
- "Writing JSPs in XML Using JSP 1.2" discusses writing JSP documents (as opposed to JSP pages)
http://www.onjava.com/pub/a/onjava/2001/11/28/jsp_xml.html
- PreciseJava's best practices to improve JSP performance
http://www.precisejava.com/javaperf/j2ee/JSP.htm
- "Code Conventions for the JavaServer Pages Technology Version 1.x Language" contains useful JSP coding conventions
http://developer.java.sun.com/developer/technicalArticles/javaserverpages/code_convention/
- The Jakarta Taglibs Standard Taglib Library is an open source implementation of JSTL
http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html
- JSP 2.0 Specification Proposed Final Draft 2 (JSR 152)
http://jcp.org/aboutJava/communityprocess/first/jsr152/index2.html
- "Call JavaBean Methods from JSP 2.0 Pages," Andrei Cioroinau (JavaWorld, May 2003) shows how to use JSP 2.0 and JSTL to reduce Java scripting in your JSPs
http://www.javaworld.com/javaworld/jw-05-2003/jw-0523-calltag.html
- This page describes the JSP Standard Tag Library and provides related links
http://java.sun.com/products/jsp/jstl/
- A handy JSTL syntax reference is available at
http://www.manning.com/bayern/appendixA.pdf
- "JSTL 1.0Standardizing JSP, Part 1," Hans Bergsten (ONjava.com, August 14, 2002)
http://www.onjava.com/pub/a/onjava/2002/08/14/jstl1.html
- "JSTL 1.0What JSP Applications Need, Part 2," Hans Bergsten (ONjava.com, September 11, 2002)
http://www.onjava.com/pub/a/onjava/2002/09/11/jstl2.html
- "A JSTL Primer, Part 1The Expression Language," Mark A. Kolb (IBM developerWorks, February 11, 2003) is a good introductory
article on JSTL expression language
http://www-106.ibm.com/developerworks/java/library/j-jstl0211.html
- "A JSTL Primer, Part 2Getting Down to the Core," Mark A. Kolb (IBM developerWorks, March 18, 2003) is another good JSTL introductory
article
http://www-106.ibm.com/developerworks/java/library/j-jstl0318/
- The Intercepting Filter design pattern is discussed in Sun's BluePrints documentation
http://java.sun.com/blueprints/patterns/InterceptingFilter.html
- More about the Intercepting Filter pattern on Sun's site
http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html
- JspDoc homepage (SourceForge)
http://jspdoc.sourceforge.net/
- JSPDoc homepage (OSDN's Freshmeat.net site)
http://freshmeat.net/projects/jspdoc/
- A list of Websites related to XSLT and JSP
http://www.j2eeolympus.com/J2EE/JavaXml/XSLTJSP.html
- The XSLT Standard Library provides some standard functionality for inclusion in your own XSLT code
http://xsltsl.sourceforge.net
- TagUnit is a useful tool for unit testing JSPs
http://www.tagunit.org/tagunit/index.jsp
- Apache Ant is a highly popular and useful build tool that provides the flexibility to support vendor-specific build commands
(such as JSP precompiling)
http://ant.apache.org/
- "Automate Your Build Process Using Java and Ant," Michael Cymerman (JavaWorld, October 2000)
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-ant.html
- Apache Maven is a useful tool for Java application project management
http://maven.apache.org/
- "Maven Ties Together Tools for Better Code Management," Jeff Linwood (JavaWorld, October 2002)
http://www.javaworld.com/javaworld/jw-10-2002/jw-1011-maven.html
- The Apache Struts Web Application Framework is a widely used MVC-based framework that facilitates modular JSP development
http://jakarta.apache.org/struts/index.html
- The World Wide Web Consortium's main XHTML 1.0 page
http://www.w3.org/TR/xhtml1/
- Differences between XHTML and HTML 4.0 are highlighted at
http://www.w3.org/TR/xhtml1/#diffs
- Jason Hunter's JavaWorld articles about Servlet 2.3 discuss the servlet filter model:
-
- Also read about the latest Servlet 2.4 specification in Jason Hunter's "Servlet 2.4What's in Store" (JavaWorld, March 2003)
http://www.javaworld.com/javaworld/jw-03-2003/jw-0328-servlet.html
- For more servlet-related articles, see the Servlets section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-servlets-index.shtml
- Read David Geary's Java Design Patterns columns
http://www.javaworld.com/columns/jw-java-design-patterns-index.shtml
- For more design pattern articles, visit the Design Patterns section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-patterns-index.shtml
- Browse the JavaServer Pages section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-jsp-index.shtml
- Browse the Java 2 Platform, Enterprise Edition (J2EE) section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-j2ee-index.shtml
- Browse the Java and XML section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-xml-index.shtml
- Speak out in the Enterprise Java discussion
http://www.javaworld.com/javaforums/postlist.php?Cat=&Board=Enterprisejava
- Sign up for JavaWorld's free weekly email newsletters
http://www.javaworld.com/subscribe