Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

More JSP best practices

JSP advancements ease development of standardized, highly maintainable JSP-based applications

  • Print
  • Feedback

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.

  • Print
  • Feedback

Resources