16.2. The JSP Pages

Java Server Pages (JSP) is a technology that allows regular, static HTML, to be mixed with dynamically-generated HTML written in Java programming language for encapsulating the logic that generates the content for the page. Refer to the Java Server Pages (http://java.sun.com/products/jsp/) and the Quickstart Guide (http://java.sun.com/products/jsp/docs.html) for more details.

16.2.1. Example

The following example shows a sample JSP page that lists the content of a cart.

<!-- Get the session -->
<%@ page session="true" %>

<!-- The import to use -->
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.Vector"      %>

<html>
<body bgcolor="white">
  <h1>Content of your cart</h1><br>
  <table>
    <!-- The header of the table -->
    <tr bgcolor="black">
      <td><font color="lightgreen">Product Reference</font></td>
      <td><font color="lightgreen">Product Name</font></td>
      <td><font color="lightgreen">Product Price</font></td>
    </tr>

    <!-- Each iteration of the loop display a line of the table -->
    <%
      Cart cart = (Cart) session.getAttribute("cart");
      Vector products = cart.getProducts();
      Enumeration enum = products.elements();
      // loop through the enumeration
      while (enum.hasMoreElements()) {
          Product prod = (Product) enum.nextElement();
    %>
    <tr>
      <td><%=prod.getReference()%></td>
      <td><%=prod.getName()%></td>
      <td><%=prod.getPrice()%></td>
    </tr>
    <%
    } // end loop
    %>
  </table>
</body>
</html>

It is a good idea to hide all the mechanisms for accessing EJBs from JSP pages by using a proxy Java bean, referenced in the JSP page by the usebean special tag. This technique is shown in the alarm example http://www.objectweb.org/jonas/current/examples/alarm/web/secured, where the .jsp files communicate with the EJB via a proxy Java bean ViewProxy.java http://www.objectweb.org/jonas/current/examples/alarm/beans/org/objectweb/alarm/beans/ ViewProxy.java.