O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.5. Including Content in a JSP Each Time the JSP Handles a Request

Problem

You want to include content in a JSP each time it receives a request, rather than when the JSP is converted to a servlet.

Solution

Use the jsp:include standard action.

Discussion

The jsp:include action includes a resource in a JSP each time it receives a request, which makes jsp:include more of a dynamic include mechanism than the include directive (see Recipe 6.4). Using jsp:include, the included JSP segments have access to the including page's request , session, and application implicit objects, and to any attributes these objects contain. Use the jsp:include action in each location of the file where you need to import resources such as JSP segments from the same web application.

Tip

The import custom action, which is part of the core JSTL, can import resources from other web applications or from other locations on the Internet. See Recipe 6.8.

Example 6-10 shows a JSP page that receives submitted form information from another page in the web application. The receiving page uses jsp:include to include header and footer page segments at the top and bottom of the page.

Just to show that the included segments have access to the same request and session information as their parent page, the header segment displays the person's submitted name, which is stored in fname and lname request parameters, in the form of a greeting-related title HTML tag. The footer page element displays the session ID along with the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required