[ Team LiB ] Previous Section Next Section

20.11 ListManager Views

The View classes used by the ListManager application are JSP pages. Although the Controller class can handle four actions (login, edit, unsubscribe, and logout) the web application requires only two views, login.jsp and edit.jsp, which are shown in Examples Example 20-10 and Example 20-11.

login.jsp displays a login form, obviously. As part of the form, it displays an optional message, specified by the Controller servlet with the HttpServletRequest.setAttribute( ) method. This message explains errors ("Incorrect Password") or provides confirmation ("logged out"). The login form has input fields for an email address and password, and has buttons to log in and edit an existing subscription or to subscribe a new address to the list. Whichever button the user clicks, the email address and password are sent to the "login.action" URL, which is handled by the login( ) method of the Controller servlet.

The edit.jsp page includes a form that allows the user to edit his mail preferences; this form POSTs to the "edit.action" URL, which is handled by the edit( ) method of the Controller. It also includes buttons that POST to the "logout.action" and "unsubscribe.action" URLs, which are handled by the logout( ) and unsubscribe( ) methods of the Controller.

Note that both login.jsp and edit.jsp clearly document their inputs and outputs. This helps integrate these View objects with the action methods of the Controller servlet. This technique is particularly helpful for large web applications in which a Java programmer is developing the Controller and a web designer is developing the JSP pages that constitute the View objects.

Example 20-10. login.jsp
<%-- ------------------------------------------------------------------------
  - login.jsp: Displays a login form for the ListManager web application. 
  - Inputs: The page expects the name of the mailing list in
  - ${applicationScope.listname} (the "listname" attribute of ServletContext).
  - It also looks for an optional error or status message in
  - ${requestScope.loginMessage} (the "loginMessage" attribute of the
  - ServletRequest)
  - Outputs: When submitted, the form POSTs to "login.action", and defines
  - parameters named "email" and "password".  If the user submits with the
  - Subscribe button, it defines a "subscribe" parameter.  Otherwise, if the
  - user submits with the Login button, it defines a "login" parameter.
  ----------------------------------------------------------------------  --%>
<%@taglib prefix="tags" tagdir="/WEB-INF/tags" %><%-- custom tag directory --%>
<head><title>ListManager: Login</title></head>
<div align="center">
<%-- Get list title from ServletContext attribute --%>
<h1>List Manager: ${applicationScope.listname}</h1>

<%-- Use the custom "box" tag to highlight the login form --%>
<tags:box title="Login" bgcolor="#ccf" border="3" margin="70" padding="20">
<%-- Get login message from request attribute and highlight with a tag. --%>
<%-- The rest of the file is standard HTML --%>
Please enter an e-mail address and password to subscribe to this
mailing list or to log in and change your delivery preferences.
<form action='login.action' method="post">
    <tr>        <%-- First row: email address --%>
      <td align='right'>Email-address:</td>
      <td><input name='email'></td>
    </tr><tr>   <%-- Second row: password --%>
      <td align='right'>Password:</td>
      <td><input type='password' name='password'></td>
    </tr><tr>   <%-- Third row: buttons --%>
      <td align='center' colspan=2>
        <input type=submit name="subscribe" value='Subscribe'>
        <input type=submit name="login" value='Login'>
<%-- Demonstrate <jsp:include> to include servlet output in a page --%>
This page has been accessed
<jsp:include page="/Counter">
<jsp:param name="counter" value="login.jsp"/>
</jsp:include> times.
Example 20-11. edit.jsp
<%-- ---------------------------------------------------------------------
  - edit.jsp: Edit subscription preferences, unsubcribe, or log out
  - Inputs: this page expects a user to be logged in and ${sessionScope.user}
  - (the "user" attribute of HttpSession) to be the User object for that user.
  - It expects ${applicationScope.listname} to contain the name of the mailing
  - list.
  - Outputs: This page displays 3 forms which POST to "edit.action",
  - "logout.action" and "unsubscribe.action" respectively.  The edit form
  - includes HTML checkboxes, and defines a parameter "html" if the user
  - indicates that she prefers HTML-formatted e-mail.  It defines the
  - parameter "digest" if the user prefers to receive message digests.
  - The logout and unsubscribe forms define no parameters.
  ---------------------------------------------------------------------- --%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %><%--custom tags directory--%>
<head><title>ListManager: Delivery Preferences</title><head>
<body><div align="center">
  <h1>List Manager: ${applicationScope.listname}</h1>
  <tags:box bgcolor="#ccf" border="3" margin="75" padding="20">
    <!-- This table puts the edit form on the left and stacks the -->
    <!-- unsubscribe and logout buttons on the right. -->
      <!-- Custom tag creates a titled box for the edit form -->
            bgcolor="#fff" border="1" margin="25" padding="20"
            title="Preferences for ${sessionScope.user.emailAddress}">
        <!-- The edit form -->
        <form action="edit.action" method="post">
          Send messages in HTML format:
          <%-- Note the use of a ${  } EL expression inside these tags to --%>
          <%-- set the default state of the checkboxes --%>
          <input type="checkbox" name="html"
          Send digests:
          <input type="checkbox" name="digest"
          <input type="submit" value="Save Preferences">
      <!-- The unsubscribe form -->
      <form action="unsubscribe.action" method="post">
        <input type="submit"
        value="Unsubscribe ${sessionScope.user.emailAddress}">
      <!-- The logout form -->
      <form action="logout.action" method="post">
        <input type="submit" value="Logout ${sessionScope.user.emailAddress}">
    [ Team LiB ] Previous Section Next Section