JSP Forward Action

JSP Forward 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 JSP forward action implies that the current request is forwarded to a static page, another JSP page, or a Java Servlet. The processing of the current JSP is terminated.
  • Here is the notation for the JSP forward action element:
    XML Description
    <jsp:forward attributes > content </jsp:forward> The content can bee one or more JSP param (parameters) delivered to the forwarded page.
  • The following table lists the attribute(s) for the JSP forward action element:
    Attribute Description
    page Should consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.

Example of using JSP forward 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 ForwardAction).

In this example, we need a file we want to forward the request to.

  • This file will show a list of Java Key words if you are lucky with the right permission. We use a random generator in this example to create a random permission (yes - this is only for demonstration).
    Here is the KeyWords.jsp:
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html> 
      <head> 
        <title>Java key words</title> 
        <style>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          }
          table { 
            border-color: #aad5ff;
            border-style: solid;
            border-width: 0 0 1px 1px;
            border-spacing: 0;
            border-collapse: collapse;
          }      
          tr,th,td {
            border-color: #aad5ff;
            border-style: solid;
            padding: 2px;
            border-width: 1px 1px 0 0;
            vertical-align: central; 
          }
          table tr th {
            color: blue;
          }
        </style>    
      </head> 
      <body > 
        <h2>Java key words</h2> 
        <c:choose >
          <c:when  test="${param.permitted=='true'}"> 
            <table  style="width:300px;"> 
              <tr class="gradientdown">
                <th>Number </th>
                <th>Key word </th>
                <th>Description</th>
              </tr>
              <tr style="background-color: lightyellow;">
                <td style="text-align: center;">1 </td>
                <td>private </td>
                <td>Private accessor</td>
              </tr> 
              <tr style="background-color: #e0e6ff;">
                <td style="text-align: center;" >2 </td>
                <td>public </td>
                <td>Public accessor</td>
              </tr>
              <tr style="background-color: lightyellow;">
                <td style="text-align: center;">3 </td>
                <td>double </td>
                <td>Double float varable</td>
              </tr> 
              <tr style="background-color: #e0e6ff;">
                <td style="text-align: center;" >4 </td>
                <td>interface </td>
                <td>Java Interface</td>
              </tr>
              <tr style="background-color: lightyellow;">
                <td style="text-align: center;">5 </td>
                <td>class </td>
                <td>Java Class</td>
              </tr> 
            </table>
            <p><i>You got the permission - no need to refresh the page.</i></p>
          </c:when> 
          <c:otherwise> 
            <p><i>You have not been allowed to see this - refresh the page.</i></p>
            <p><i>You have tried : ${param.tried} time(s).</i></p>
          </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 KeyWords).

  • Based on request parameter, permitted, the page will show two different things depending on whether permitted is true or false. We are using the core JSTL library and a EL expression for this test.

We need a index.jsp file as start page to demonstrate the JSP forward action element.

  • Here is the main JSP file:
    <!DOCTYPE html>
    <html> 
      <head> 
        <title>The forward Action Example</title> 
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
      </head> 
      <body> 
        <h2>The forward action Example</h2> 
        <%! int tried = 0;%>
        <%
          int randomNum = (int) (Math.random() * 100);
          if (randomNum > 50) {
        %>
        <jsp:forward page="KeyWords.jsp" >
          <jsp:param name="permitted"  value="true"/>
        </jsp:forward>
        <% } else {
          tried++;
          request.setAttribute("tried", tried);
        %>
        <jsp:forward page="KeyWords.jsp" >
          <jsp:param name="permitted"  value="false"/>
          <jsp:param name="tried"  value="${requestScope.tried}"/>
        </jsp:forward>
        <% }%>
      </center> 
    </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).

  • In this file we create a random permission based on the outcome from a random generator in Java.
  • We forward the request with a parameter, permitted, which is true or false. If the parameter is true then the user has permission to see the java key words.
  • We also use a variable, tried, which counts the number of attempts to obtain permission.
  • The tried variable is specified as a parameter in the jsp:forward action so we can show the user the number of failed experiments to obtain permission

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

Creating Web-server Deployment descriptor.

  • The context-root (in example /ForwardAction) 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>/ForwardAction</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.
  • If the user got permission the browser will display:
    If the user NOT got permission 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.