站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

5.4 The Login Page

Now that a form has been provided so users can log themselves in on the system, there needs to be a page that will perform the necessary actions. The page that does this is shown in Listing 5.5.

Listing 5.5 The login handler
<%@ taglib prefix="c"
    uri="http://java.sun.com/jstl/core" %>

<jsp:useBean
  id="user5"
  scope="session"
  class="com.awl.jspbook.ch05.UserInfoBean"/>

<jsp:setProperty name="user5" property="*"/>
<jsp:setProperty
  name="user5"
  property="login"
  value="true"/>

<jsp:include page="top.jsp">
  <jsp:param name="title" value="Login"/>
</jsp:include>

<c:choose>
  <c:when test="${user5.isLoggedIn}">
    You have sucessfully logged into Java News Today!<p>
    Click <a href="jntindex.jsp">here</a> to proceed to
    your custom edition.
  </c:when>
  <c:otherwise>
    We're sorry, we were unable to log you in. Perhaps you
    mistyped your username or password; use the form on
    the left to try again.
  </c:otherwise>
</c:choose>

<jsp:include page="bottom.jsp"/>

This page begins with the usual things, including loading the UserInfoBean. The bean's properties are then set: the username and password from the login form in Listing 5.2. Part of the UserInfoBean's job as the model of users is to provide a mechanism that logs a user in on the system, given the username and password. This mechanism is triggered by setting the login property of the bean, which will cause the bean to check these values against a list of all users in the system; if a match is found, the isLoggedIn property will be set to true.

This setting of properties has to be done before the page top is included. If it were done afterward, the user's isLoggedIn property would still be false during processing of the header and navigation, and consequently the name would not be shown and the login form would.

The rest of the page is pretty anticlimactic: another c:choose tag used to determine whether the login succeeded and to display an appropriate message in either case.

    [ directory ] Previous Section Next Section