| [ directory ] |
|
3.5 Special Actions When Beans Are CreatedNow that beans can live beyond the scope of a single page, any given instance of a jsp:useBean tag may or may not create a new instance of a bean. Often a page will need to take a special action when the bean is first created. This can be done with a simple extension to the jsp:useBean tag. So far, this tag has been used without a closing /jsp:useBean tag. If there is a matching close tag, anything in the body between the open and close tags will be evaluated when the bean is created and not subsequently. The content enclosed by these tags can be anything at all. For a start, it can be plain text. Listing 3.9, a slight modification to Listing 3.8, illustrates this ability. Listing 3.9 Displaying text when a bean is created<jsp:useBean id="counter2" class="com.awl.jspbook.ch03.CounterBean" scope="session"> Welcome to the site! </jsp:useBean> With this change, the user will see the message "Welcome to the site" when the bean is created. On the user's subsequent visits to this page, or any other using the counter bean, the message will not be displayed. Note that the identifier counter2 is used to distinguish this bean from the one used in Listing 3.8. If this were not done, the counter would register how many times the user had been to either page, and if counter.jsp were visited before counter2.jsp, the welcome message would not show up. In addition to text, the initialization block can contain jsp:setProperty tags. This can be useful when the bean needs to have certain values before it is used. In some cases, these default values can be placed in the bean itself, such as the initial value of 0 for the number of visits in the counter bean. In other cases, the values with which the bean needs to be initialized may depend on the current user or the page or any of thousands of other possibilities. Rather than putting values with these kinds of dependencies into the bean code, it makes more sense to let the page set up the beans as needed. Listing 3.10 shows another addition to Listing 3.8. This time, the bean will be told the name of the page on which it was created. Listing 3.10 Setting a value when a bean is created<jsp:useBean id="counter3" class="com.awl.jspbook.ch03.CounterBean" scope="session"> <jsp:setProperty name="counter3" property="firstPage" value="counter3.jsp"/> Welcome to the site! </jsp:useBean> <jsp:setProperty name="counter3" property="incrementCount" value="1"/> The first page you visited was <jsp:getProperty name="counter3" property="firstPage"/> You have seen <jsp:getProperty name="counter3" property="count"/> page(s) on this site. The bean will now keep track of where it was created. If this bean were then used on multiple pages, it could report on the total number of pages visited, as well as the first page. At this point, it is natural to wonder whether there are corresponding close tags for jsp:getProperty and jsp:setProperty. In fact, there are not. Although it is often necessary to perform different actions, depending on whether a property is set, this is done using special JSP tags called conditionals, which will be discussed in the next chapter. |
| [ directory ] |
|