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

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

5.2 The Header

The header is the site's simplest component, as its only job is to display a banner with the title of the page, along with the user's name as added in Chapter 3. In order to clean it up a little, the header will be modified to show only the user's name if the user has logged in. The resulting page is shown in Listing 5.1.

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

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

<center>
  <h2>
    Java News Today: <c:out value="${param.title}"/>
  </h2>
</center>

<c:if test="${user5.isLoggedIn}">
  <div class="left">
    Hello <c:out value="${user5.name}"/>!
  </div>
</c:if>

This is about as simple a page as one could hope for. It loads a bean, checks a property with the c:if tag, and displays a value with the c:out tag.

The bean is loaded from the session scope because the user information should remain active as long as the user is active on the site. If this bean were in the request scope, the user's preferences would need to be reloaded梠r worse, would be lost completely梠n every new page. If the bean were in the application scope, every user would share the same data, which would not allow each user to have different options. Thus, session scope is definitely the right place for this bean.

Note that the test simply checks the value of a property. The test does not need to check whether the ${user.5isLoggedIn == true}isLoggedIn property itself returns true or false directly.

    [ directory ] Previous Section Next Section