Standard Actions

A jsp page may include standard actions:

<jsp:include>
<jsp:forward>
<jsp:plugin>
<jsp:param>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>

Include and Forward

A multi-page web application consists of several Java server pages. One page can send the original HTTP request to another page. Forwarding a request means that the sender is finished processing the request and expects the receiver to send HTML back to the browser. Including means that the receiver will generate a piece of HTML that the sender will include in its HTML response to the browser:

Multi-page applications allow pages to be specialized and reusable.

Example: A simple dispatcher

A dispatcher is a special page (or servlet) that is the target of all form actions. When a dispatcher receives a request, it analyzes it, selects a handler page, then forwards the request to that page.

dispatcher.jsp

In the simplest case, the request tells the dispatcher which page should handle it. This means the dispatcher doesn't need to do any complicated analysis.

In the following example, the dispatcher receives a command from a form. If the command is "page1", then the request is forwarded to page1.jsp, etc:

<%@ page errorPage="error.jsp"%>
<html>
  <body>
  <h1 align="center">The Dispatcher</h1>
  <%
  String cmmd = request.getParameter("command");
  if (cmmd.equals("page1")) {%>
     <jsp:forward page="/page1.jsp"/><%
  } else if (cmmd.equals("page2")) {%>
     <jsp:forward page="/page2.jsp"/><%
  } else if (cmmd.equals("page3")) {%>
     <jsp:forward page="/page3.jsp"/><%
  } else if (cmmd.equals("page4")) {%>
     <jsp:forward page="/page4.jsp"/><%
  } else {
    throw new Exception("Unrecognized page");
  }
  %>
  </body>
</html>

error.jsp

Notice that the dispatcher throws an exception if it doesn't recognize the command. Where is this exception caught? Notice the page directive at the top of dispatcher.jsp:

<%@ page errorPage="error.jsp"%>

Page directives will be discussed in detail later. A J2EE servlet container allows programmers to designate a page to be the error page. This page has access to an implicit object called exception, which is bound to the exception thrown.

Here's a simple example:

<%@ page isErrorPage="true"
         import="java.io.*"%>
<html>
  <body>
    <%
      if (exception != null)
      {
        out.println(exception.getMessage());
      }
    %>
  </body>
</html>

page1.jsp

page1.jsp, page2.jsp, etc. simply display their names ("Page 1", "Page 2", etc.) and a form. The form is the same in all pages, and so the form is created on a separate page and an include directive is used to include it on each page:

<html>
  <body>
  <h1 align="center">Page 1</h1>
  <jsp:include page="/theForm.html"/>
  </body>
</html>

theForm.html

The form contains a drop down menu and a submit button. It sends the menu selection to the dispatcher:

<html>
  <body>
    <form action="dispatcher.jsp">
      <p>select a page:</p>
      <p>
        <select size="1" name="command">
          <option value="page1">
            Page 1
          </option>
          <option value="page2">
            Page 2
          </option>
          <option value="page3">
            Page 3
          </option>
          <option value="page4">
            Page 4
          </option>
          <option value="page5">
            Page 5
          </option>
        </select>
      </p>
      <p>
        <input type="submit"/>
      </p>
    </form>
  </body>
</html>

Demo

Here's page 1:

Here's the error page (which is a bit more elaborate that the one described earlier):

Example: Including a Table

actions.jsp

<html>
<head><title> Standard Actions </title> </head>
<body>
Here's our company's budget: <br /><br />

<jsp:include page="budget.html" flush = "true" />

</body>
</html>

budget.html

<table width="100%" border = "1">
<caption align="top"> Acme Inc. Budget for 2003 </caption>
<tr>
<th width="50%"> Item </th>
<th width="50%"> Cost </th>
</tr>
<!-- +++++++++++++++ ROW 1 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Paper Clips </td>
<td align = "center" width="50%"> $1000 </td>
</td>
</tr>
<!-- +++++++++++++++ ROW 2 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Pencils </td>
<td align = "center" width="50%"> $400 </td>
</td>
</tr>
<!-- +++++++++++++++ ROW 3 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Asprins </td>
<td align = "center" width="50%"> $950 </td>
</td>
</tr>
</table>

<jsp:useBean>, <jsp:setProperty>, <jsp:getProperty>

The useBean action simplifies the task of creating or finding a bean.

BudgetBean.java

public class BudgetBean implements java.io.Serializable {
    // properties:
    private float paperClips = 1000;
    private float asprin = 950;
    private float pencils = 450;
    // property getters & setters:
    public float getPaperClips() { return paperClips; }
    public void setPaperClips(float f) { paperClips = f; }
    public float getAsprin() { return asprin; }
    public void setAsprin(float f) { asprin = f; }
    public float getPencils() { return pencils; }
    public void setPencils(float f) { pencils = f; }
}

actions.jsp

<jsp:useBean id = "budget" scope = "request" class = "BudgetBean" />

<html>
<head><title> Standard Actions </title> </head>
<body>

<jsp:setProperty name = "budget" property = "paperClips" value = "100" />
<% budget.setPencils(500); %>

Here's our company's budget: <br /><br />

<jsp:include page="budget.jsp" flush = "true" />

</body>
</html>

budget.jsp

<jsp:useBean id = "budget" scope = "request" class = "BudgetBean" />

<table width="100%" border = "1">
<caption align="top"> Acme Inc. Budget for 2003 </caption>
<tr>
<th width="50%"> Item </th>
<th width="50%"> Cost </th>
</tr>
<!-- +++++++++++++++ ROW 1 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Paper Clips </td>
<td align = "center" width="50%">
   $<jsp:getProperty name = "budget" property = "paperClips" />
</td>
</td>
</tr>
<!-- +++++++++++++++ ROW 2 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Pencils </td>
<td align = "center" width="50%">
   $<%= budget.getPencils() %>
</td>
</td>
</tr>
<!-- +++++++++++++++ ROW 3 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Asprins </td>
<td align = "center" width="50%"> $950 </td>
</td>
</tr>
</table>