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

Page 3 of 6

Database access library
JSTL provides a database access custom tag library, but I seldom use it because I strongly feel that you should not implement database access directly inside JSPs. Accessing a database directly from a JSP reduces reuse because the database access code is not accessible outside the specific JSP in which the database tags are used. Direct database access in JSPs indicates strong coupling between the presentation and database tiers. Disciplined separation means more modularity, greater opportunity for reuse, and better opportunities for specialization of presentation and database experts. While I recommend using the other three JSTL tag libraries when they satisfy a JSP developer's needs, I do not recommend using the JSTL database access library outside of prototypes and the simplest Web applications.

JSTL core tag library
As its name suggests, the JSTL core tag library is the most commonly used of the four libraries. This tag library provides custom tags for iterating over collections, handling conditionals (if-else type constructs), and other familiar procedural constructs. This tag library alone saves JSP developers from writing many of their own custom tags or embedding Java directly in JSPs.

JSTL XML tag library
This tag library supplies developers with a surprisingly versatile collection of XML manipulation tags and XSLT transformation tags.

JSTL formatting tag library
For Webpages and Websites available in multiple countries and/or languages, internationalization (I18N) can be one of the more challenging aspects of Web development projects. The JSTL formatting tag library helps JSP developers meet this challenge with custom tags that support writing and reading numbers and dates appropriately for the specified locale.

Expression language
JSTL has two tag types for these custom tag libraries: traditional request time (RT) custom tags and EL tags. Because EL is more flexible, I recommend using the EL custom tag libraries rather than the RT libraries.

According to the JSP 2.0 Specification Proposed Final Draft 2, all JSP tags will support an EL. Getting to know EL now by using JSTL's EL custom tag versions will make this transition smoother.

JSTL and other custom tag libraries

Because custom tags have been supported for much longer than JSTL has been available, many individuals and organizations have developed custom tag libraries that largely overlap with JSTL. In most cases, I recommend first using JSTL when possible instead of homegrown custom tag libraries or other publicly available custom tag libraries that implement the same functionality. The standardization provided by JSTL is a significant advantage because the tag APIs can be learned once and used consistently.

Comparing Struts tag libraries and JSTL provides a good example of how to implement this best practice. Struts provides many advantages beyond the Model-View-Controller (MVC) framework with which it is commonly associated. One additional advantage is its set of custom tag libraries. Struts custom tags supported iterations and conditionals long before JSTL was available. However, I am now beginning to use JSTL iteration and conditional tags rather than the Struts equivalents. I still use Struts form tags because they are closely tied with Struts form handling, and no JSTL equivalent exists.

  • Print
  • Feedback

Resources