necessary HTML there is not troublesome.Sun's Java 2 Platform, Enterprise Edition(J2EE) Blueprints documentationdiscusses this issue further.There are exceptions to this standard: if including one or two lines of Java code asscriptlets in the JSP solves the same problem that would require many more lines of HTML in the Java handler class, allowing Java code to exist in the JSP page might beprudent.
Use an appropriate inclusion mechanism
It is rarely good design to reproduce code commonly used by different applicationpieces each time another piece of that application needs that functionality. Factoringcommon JSP or HTML code out of multiple pages and into a single file improvesmaintainability (you need to make changes in only one location) and reusability.Two JSP
mechanisms reduce code redundancy and promote reusability; toensure that you use the appropriate include mechanism, it is important to know thedifferences between the two. Generally, I use the include
unless I can justify a need for the include
Question 7 in the Blueprints' "Web Tier" sectionprovides a good resource for understanding the differences between the two includemechanisms and determining which to use in a particular situation.
A JSP's include directive includes the content of a specified file in that JSP. Use theinclude mechanism for situations when text, such as ASCII or HTML, needs to beincluded in multiple JSPs. For example, I commonly use the include directive toinclude footer information, such as company name or copyright date, on every JSP ina company's application.Since you include the content of any file specified by the include directive in thecalling JSP before it compiles, variables and other values specified in the calling JSPcan also be utilized in the included content. However, I try not to rely on variablesdefined in the calling JSP, since this dependency reduces the included file'sreusability.
The include action executes the specified JSP first and then places the generatedresponse in the calling JSP. Because the include action includes the generatedresponse rather than the source content itself, variables and other values specified inthe calling JSP are not available to the page included with the include action.One disadvantage of the include action as currently implemented by the JSPimplementations with which I am familiar relates to the
attribute. In theJSP implementations I have used, this attribute is required and must be set to
value indicates that the buffer will always flush before a target pagespecified by the include action executes. This can prove problematic if the forwardmechanism is invoked either explicitly or implicitly later in the JSP. In the recentlyreleased JSP specification (1.2), however, the include action's
attribute can beset to
. Tomcat 4.0 provides a reference implementation of this specification andsupports this new include action argument.
Use a JSP template mechanism