JSP Include Directive Element.

JSP include Directive elements

  • JSP Directive elements are used in the JSP file and provide information to the JSP container about the page. There are three directives available: page, include, and taglib.
  • The include directive will tell the JSP container to include another file in the position it is given.
  • You may code many include directives anywhere in your JSP page where needed.
  • You cannot pass parameters to the include file. The JSP include action has this opportunity.
  • JSP include directive element is specified either in the form of XML tags, or in the form of a JSP style notations.
    Here is the notation for the include directive element:
    JSP Style or XML Description
    <%@ include attributes %> or
    <jsp:directive.include attributes />
    The file to be included at the current position in the file. The file is specified using a URI to a file within the web application.
  • The following table lists the attribute(s) for the include directive:
    Attribute Valid values Description
    file Any valid relative URL to another JSP. This directive tells the container to merge the content of other external files with the current JSP during the translation phase.

Example of using JSP include directive element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is IncludeDirective).

  • It is normal to add all the include files to the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h1>The Company</h1>
    <hr>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr>
    Today is: <%= java.util.Calendar.getInstance().getTime() %> 

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Footer.jsp and folder should be WEB-INF).

  • Then we need a JSP file to demonstrate how we use the include directives.
    Here is the main JSP file:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
      </head>
      <body style="max-width:600px; text-align: center; ">
         <%@ include file="/WEB-INF/header.jsp" %>
        <h2>Presentation</h2>
        <p>
          We created our product because we knew there had to
          be a better way to build java applications. 
          We’re developers, so we know what developers want: 
          a modern programming language, powerful tools, 
          an efficient and delightful workflow and the 
          power to create the best user experiences. 
          We are on a mission to make it fast, easy and 
          fun to build great mjava applications.
        </p>
        <jsp:directive.include file="/WEB-INF/footer.jsp" />
        </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Presentation.jsp ).

How does this work?

  • We have created two JSP files, Header.jsp and Footer,jsp, which we want to include in other JSP documents.
  • With the include directive we includes these files where we want the contents of the files must be included in JSP file, Presentation.jsp.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>Presentation</servlet-name>
            <jsp-file>/Presentation.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>Presentation</servlet-name>
            <url-pattern>/Presentation</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>Presentation</welcome-file>
        </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the url Presentation. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /IncludeDirective) for the application will in most cases be specified by a server vendor deployment descriptor.
    The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.