JSP Include Action

JSP Include Action Element

  • All the JSP action elements are used during the request processing phase, and not as the directive elements and script elements, which are used during the translation phase
  • All JSP action elements are specified in XML tags and can not be used in an XML directive as these have no end tag.
  • The include action will tell the JSP container to include another file in the position it is given.
  • The JSP include action can contain one or more JSP param actions, which provides parameters to the included file.
  • Here is the notation for the JSP include action element:
    XML Description
    <jsp:include attributes > content </jsp:include> The content can only be one or more <jsp:attribute>
  • The following table lists the attribute(s) for the JSP include action element:
    Attribute Description
    page The relative URL of the page to be included.
    flush Determines if the buffer for the output is flushed immediately, before the included page’s output.

Example of using JSP include action 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 IncludeAction).

In this example, we need a file we want to include.

  • This file is a constructed to be a controller for any user login.
  • Permitted users with passwords are hard-coded in the application which is not a normal thing to do (should be in a persistent storage device as MySQL instead).
    Here is the include file, login.jsp:
    <%@page import="java.util.HashMap"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%
      HashMap users = new HashMap();
      users.put("obama", "amabo");
      users.put("admin", "nimda");
      users.put("curt", "truc");
      request.setAttribute("users", users);
    %>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
      </head>
      <body>
        <c:choose>
          <c:when test="${param.pass == users[param.user]}" >
            <h1>You are logged in as '${param.user}'.</h1>
            <a href="index.jsp?type=logout">logout?</a> 
          </c:when>
          <c:when test="${param.type == 'logout'}" >
            <h1>You are now logged out.</h1>
            <a href="index.jsp?type=login">Login?</a> 
          </c:when>
          <c:otherwise>
            <h1>Wrong login name or password.</h1>
            <a href="index.jsp?type=login">Try again?</a> 
          </c:otherwise>      
        </c:choose>
      </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 javaCodes.tag and folder should be WEB-INF/tags).

  • In this file, we expect to receive user and password through parameters.
  • If the user exists in the Map (users) and the password is right for the user, the user is logged in.

We need a JSP file(s) to demonstrate use of the include file.

  • <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <!DOCTYPE html>
    <html>
      <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
      </head>
      <body>
        <c:choose>
          <c:when test="${empty param.loginname && param.type!='logout'}" >
            <form action="#" style="width: 180px; " method="POST">
              <fieldset style="border: 1px solid black; background-color: lightgrey;">
                <label for="loginname">LoginName:</label>
                <input name="loginname" value="" type="text" title="Obama, admin or curt"/>
                <label for="password">Password:</label>
                <input name="password" value="" type="password" title="amabo, nimda or truc"/>
                <input style="margin-top:6px;" type="submit" value="login"/>
              </fieldset>
            </form>
          </c:when>
          <c:otherwise>
           <jsp:include page="login.jsp" >
             <jsp:param name="user" value="${param.loginname}"/>
             <jsp:param name="pass" value="${param.password}"/>
           </jsp:include>
          </c:otherwise>      
        </c:choose>
      </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 index).

  • We use a html form input for the user to select username and password for login.
  • When the form is submitted the parameters for the include action are set.
  • I must emphasize that this is not a normal way of doing this. This is only for demonstration in the use of parameters by an include action tag.

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>JavaCodes</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>JavaCodes</servlet-name>
            <url-pattern>/JavaCodes</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>JavaCodes</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 welcome file with url JavaCodes. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /IncludeAction) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/IncludeAction</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • 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.