2.4 The Phases of a JSP
Each JSP goes through two distinct phases. The first phase, when the translator turns the file into a servlet, is called translation time. This translation from JSP to servlet happens whenever the JSP file is modified.
The second phase, when the resulting servlet is run to generate the page, is called request time. This happens whenever a browser requests the page. Different things happen in each phase, and the distinction is important.
The handling of JSP comment tags happens at translation time. The translator simply omits any text within comment tags, so the servlet will not even need to deal with it.
Conversely, the jsp:include tag is handled at request time. For every request that comes in for the index.jsp URL, the content.jsp servlet will be run when the jsp:include tag is encountered. In principle, this could have been done at translation time; the chunk of HTML from the content.jsp file could have been dropped right into the index.jsp servlet. This would be much less powerful, however. By processing includes at request time, the contents of the included file can change independently of the main file.
Another advantage to processing includes at request time is that doing so ensures that no JSP is fundamentally special or different. Every JSP is a little program that can be run by requesting the corresponding URL through a browser; there is no distinction between "top-level" and "included" JSPs. As mentioned, this means that a browser can directly request an included file, such as content.jsp, which is often useful when testing page components.
This also guarantees that all JSP elements will work the same way in all pages. This means that JSP comments will be stripped out of included files and that the jsp:include tag will work inside included files! Files can include files that can include files, and so on, potentially to infinity.
For the sake of completeness, it is worth mentioning the translation-time version of the jsp:include tag, called the include directive. This tag looks like this:
<%@include file="contents.jsp" %>
This tag is called a directive because it directs the page compiler to take some action. In this case, when it sees the directive, the page compiler will embed the contents of the contents.jsp file directly into the servlet that it is building for index.jsp. Subsequently, if the contents.jsp file is edited, index.jsp will not change. Browsers will continue to get the old message until the page compiler is forced to rebuild the index.jsp servlet.
Note that the include directive specifies what is to be included with file=, whereas the jsp:include tag specifies page=. This nicely encapsulates the differences between the two: The directive includes a file as it is building the servlet at request time, and the tag includes another page at request time.
In some obscure instances, the include directive and the jsp:include tag are not equivalent. For the most part, though, the two are functionally identical, and as the jsp:include tag is more convenient, it will be used in preference to the directive throughout this book.
 |