|
This question
can be used to explore a candidate's understanding of basic
JSP tags. It is appropriate for all levels of experience in
JSP.
JSP
supports two different ways of merging the content of one
file into another - the include directive and the include
action. How do these differ and why would you choose one over
the other?
JSP provides two
distinct ways to include the contents of a file. A beginning
JSP developer should be familiar with at least the first one,
but a developer with some good JSP experience should understand
the use of each of the tags.
The first tag is
< jsp:include >,
the JSP include action. The include action will insert the
rendered output of the referenced file at request time, after
the included page has been processed by the JSP container.
Thus if the page specified in the include tag is another JSP,
it will be passed the current request, evaluated on its own,
and the resulting output merged into the source page at the
point of the include action tag. The contents of the included
file must be stand-alone, because it will be compiled and
rendered by the container on its own. This type of include
is called a request-time include.
The second type
of include is ,
the include directive. The include directive is known as a
translation-time include. Its work is done during the container's
initial parsing of the page and merges in the actual source
contents of the included page, rather than its rendered content.
Thus the included file doesn't necessarily need to be able
to stand alone; it can contain code fragments and bean or
variable references which expect to be evaluated in the context
of the source page.
The include
action is appropriate for including shared segments of content
that can be rendered on their own. A typical usage would be
including standard headers, footers, or navigation elements
into each page. The use of the include directive however,
should be limited to reusing fragments of JSP code that must
be shared between multiple pages. In practice the include
directive is used far less often than the include action.
About
the author
Duane Fields is a Java developer, author, and Internet technologist
with nearly a decade of professional experience in the design
and development of leading edge Internet products and services.
Duane is also a respected member of the Java development community
and is frequently invited to speak at industry conferences
and events. He has co-authored two books and published numerous
articles on many aspects of web application development from
Java to Relational Databases. The newly expanded, second edition
of his best selling book "Web Development
with JavaServer Pages" was released in December
of 2001.
|