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

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

3.6 Making Beans Last Forever

Although it is not a scope, a related technology extends the lifetime of a bean. Beans can be frozen by saving their contents into files, and these files can then be transparently turned back into beans when they are needed.

The mechanism to turn beans into files is called serialization, a built-in facility of the Java programming language. Serialization is also very useful to JSP authors because it allows beans to be tailored, or customized, before they are used.

For example, a software company might make a bean that computes how quickly money in a bank account will grow. One property of this bean would be the interest rate. Many different banks could purchase this bean, set the interestRate property to the appropriate value, and then save the bean.

When they create their Web pages, the JSP authors at these banks can use the standard bean tags to access the bean, and none of the pages they create will need to worry about the interest rate. This also makes these sites easier to maintain. When the interest rate changes, the administrator will simply need to replace the serialized file with one containing the new rate, and all the pages will automatically use the new value. In a sense, serialization can be used to "bake in" values that are appropriate to a site. JSPs can then use these values as if they were intrinsic to the bean.

The details of how serialization is done are beyond the scope of this book, but many programs hide the details and make it easy to create such frozen beans. Sun provides one called the Bean Box, although this is targeted primarily for people using beans to build graphic front ends and is overkill for the kinds of beans used in JSPs. A much simpler editor from Canetoad Software is included on the CD-ROM for this book, along with instructions for its use.

Using a serialized bean is no more complicated than creating a bean from scratch; it simply requires a slight variation to the jsp:useBean tag:

<jsp:useBean
      id="bean name"
      beanName="bean name"
      type="bean class"/>

Here, id is the name by which the JSP will use the bean, the same as always, and beanName should be the name of a file containing a serialized bean. By convention, such files end with the .ser extension, which should not be included in the name. Finally, type is the class or interface for which the bean is an instance. Note that type is used instead of class because the class is implicitly provided by the serialized file. An instance always knows what class it is an instance of, and this is true even when that instance has been stored in a file. The type is still necessary because the JSP still needs to assign a type to the variable that will hold the bean. In practice, the type can usually be thought of as the bean's class.

Listing 3.11 shows a JSP that uses a serialized bean to get information about a record, in this case "Tinderbox" by Siouxsie and the Banshees.

Listing 3.11 Using a serialized bean
<jsp:useBean id="album" beanName="tinderbox3"
 type="com.awl.jspbook.ch03.AlbumInfo"/>

<body
  bgcolor="<jsp:getProperty
              name="album"
              property="bgColor"/>"
  text="<jsp:getProperty
              name="album"
              property="textColor"/>">

<h1><jsp:getProperty name="album" property="name"/></h1>

Artist: <jsp:getProperty name="album"
            property="artist"/><p>
Year: <jsp:getProperty name="album" property="year"/></p>

This looks much like the other examples in this chapter, except for the jsp:useBean tag and the fact that there is no obvious place where the properties have been set. The reason is that they are not set in the JSP but have already been stored in the serialized file.

It would be nice if this JSP could also display the list of tracks, but there is a problem. In general, the page won't know in advance how many tracks are on a given CD. The bean could have a separate property for each track, and the page could display any fixed number of tracks in the obvious way:

<li><jsp:getProperty name="album" property="trackName1"/>
<li><jsp:getProperty name="album" property="trackName2"/>
<li><jsp:getProperty name="album" property="trackName3"/>

However, if the CD has only two tracks, this will display an extra bullet with no name next to it. If the CD has four or more tracks, some won't get shown at all. What is needed is a way to repeat some region of HTML, once for each track in the bean. This is called iteration and is covered in the next chapter.

This use of serialization makes beans behave a little like a database. Perhaps the "tinderbox" bean came as part of the collection of beans for all of the Banshees' albums. To create pages for these others, it would be necessary only to change the beanName to "Hyaena" or "Juju", or so on. In fact, the deep connection between beans and databases is explored more fully in Chapter 6.

    [ directory ] Previous Section Next Section