Subscribe to feed:
Or get posts via email:









Add to Technorati Favorites



« XHTML Friends Network | Main | Representational State Transfer (REST) »

Use Tag Files For Modularity in JSP

Have you ever come across JSP code peppered with <%@ include %> directives? Back when we were limited to JSP 1.1, include directives were a decent means to build modularity and re-use code. The biggest problem with the include directive, however, is that included code does not get its own stack. Variables used by the included fragment are the same variables used by the JSP that included it. For a large scale project, the complexity of documenting the inputs of fragments and the manual enforcement of those inputs become a burdon upon productivity. In practice, the include directives are a source of very difficult-to-track errors.

Don't worry though, because you can use Tag files to address all of those issues. JSP 2.0 Tag files offer huge modularity advantages over include page fragments. Tag files are almost identical to regular JSP files, except they have the <%@ tag %> directive at the top instead of <%@ page %>. You will probably also want to make use of some <%@ attribute %> directives.

Use tag files when you need:

  • re-usable, modular JSP code
  • required attributes
  • attributes type checking
  • a stack (for recursion)

Here is a great step-by-step guide by Andrei Cioroianu that will take you through the process of converting included jsp fragments into .tag files.

Post a comment