Specifying Automatically Included Files

If a lot of tag libraries are used for an application, or a set of pages need the same page attribute values to be set, it’s easier to maintain the application if all the common declarations are placed in one file that is then included in all JSP pages. You can do so with the include directive, but then you run the risk of forgetting to include the file in a page. JSP 2.0 introduces a better solution: automatic includes.

You can use one or more <include-prelude> elements to include files at the beginning of all JSP pages that match the JSP property group URL pattern, and one or more <include-coda> elements to include files at the end of the JSP files. As opposed to all other configuration properties, you can even use multiple JSP property groups to define these two elements; the include elements from all groups that match the request URL are applied:

<web-app ...>
  ...
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <include-prelude>/WEB-INF/segments/taglibDecl.jspf</include-prelude>
                  <include-prelude>/WEB-INF/segments/errorPageDecl.jspf</include-prelude>
                  <include-coda>/WEB-INF/segments/copyright.jspf</include-coda>
    </jsp-property-grop>
    <jsp-property-group>
      <url-pattern>/main/*</url-pattern>
      <include-prelude>/WEB-INF/segments/noSessionDecl.jspf</include-prelude>
    </jsp-property-group>
  </jsp-config>
  ...
</web-app>

In this example, I use one group for the *.jsp pattern that include files with common tag library and error page declarations ...

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.