Previous Section  < Day Day Up >  Next Section

4.5 Accessing Application Data

The different parts of a Java web application need to be able access the same data objects. For instance, a product catalog object a listener creates at application start must be accessible by a servlet that processes a user query against the catalog, and the result of the query must be available to the JSP page that renders the response showing the result.

The Servlet API includes methods for managing this type of data as attributes of the objects representing the application, the user session, and the request, defined by these classes, respectively: javax.servlet.ServletContext, javax.servlet.http.HttpSession, and javax.servlet.ServletRequest.

All three classes contain these methods for managing the attributes:

public void setAttribute(String name, Object value);
public Object getAttribute(String name);
public void removeAttribute(String name);

In the product catalog example, the listener uses the setAttribute() method on the ServletContext to make the catalog available to the servlet, and the servlet uses the getAttribute( ) method to obtain it. To make the query result available to the JSP page, the servlet calls the setAttribute( ) method of either the HttpSession or the ServletRequest object (depending on how long the result must be available).

The JSP specification refers to these three attribute collections as different scopes: the application, session, and request scope. It also adds a collection of attributes available only to objects within the same JSP page during the processing of a request. It calls this the page scope. Figure 4-11 shows the lifetime and visibility of objects in the different scopes.

Figure 4-11. Lifetime and visibility of objects in different scopes
figs/Jsf_0411.gif

The JSP EL provides implicit variables that represent the scopes as java.util.Map instances, as shown in Table 4-5. Assuming the servlet in the product catalog example saves the query result as a bean with properties named rowCount and rows in a request attribute named result, the JSP page can access and use the data like this (using JSTL core actions):

<c:if test="${requestScope.result.rowCount == 0}">

  Sorry, not products match your criteria

</c:if>

<c:if test="${requestScope.result.rowCount != 0}">

  These products were found:

  <ul>

    <c:forEach items="${requestScope.result.rows}" var="row">

      <li>${pageScope.row.prodName}</li>

    </c:forEach>

  </ul>

</c:if>

EL expressions actually look for attributes in all scopes if no scope is specified, in the order page, request, session, and application, so the example can be simplified like this:

<c:if test="${result.rowCount == 0}">

  Sorry, not products match your criteria

</c:if>

<c:if test="${result.rowCount != 0}">

  These products were found:

  <ul>

    <c:forEach items="${result.rows}" var="row">

      <li>${row.prodName}</li>

    </c:forEach>

  </ul>

</c:if>

This concludes our tour through HTTP, servlets, and JSP. While we've barely scratched the surface of these technologies, we have covered the most important aspects that you must be aware of in order to use JSF. If all of this is new, you may want to return to this chapter to refresh your memory when the other chapters rely on an understanding of what's covered here.

    Previous Section  < Day Day Up >  Next Section