Cincom

Primer : Using JSP Beans


The cornerstone of the J2EE model is the ability to make use of classes stored within the Smalltalk image. The method by which you have access to the objects in the image is via a mechanism called JavaBeans, or just simply Beans.

This lesson describes how to use beans to access objects.

1. If the Web Toolkit is not already running and your sandbox site not configured, please do so now.

2. Below is an over-simplified graphic that exemplifies what beans are all about.


Figure 1. Accessing objects in the Smalltalk image from a web page

JavaBeans, or just beans, in 25 words or less, allow us to create and access objects from web pages. They are the J2EE version of the session variables in the ASP model (23 words!).

3. For this example, we will create a simple class (StringBean) and use J2EE techniques to manipulate an instance of the StringBean class.


Figure 2. The StringBean class and the initialize method

4. Using the System Browser in VisualWorks, create the StringBean class in the category of ZZZ (we chose the category of ZZZ because it would be easy to find - at the very bottom of the category list).

     Smalltalk.VisualWave defineClass: #StringBean
     superclass: #{Core.Object}
     indexedType: #none
     private: false
     instanceVariableNames: 'quote quotation '
     classInstanceVariableNames: ''
     imports: ''
     category: 'ZZZ'
    
Note that we are creating the class in the VisualWave namespace. It's not that we had to - we just wanted to show an example of using a different namespace other than Smalltalk for the beans example.

5. In your favorite text editor, enter the following and save this file as bean1.ssp in the sandbox directory (just copy from here and paste it into your editor).

<HTML>
<HEAD>
<TITLE>Using JavaBeans with SSP</TITLE>
</HEAD>
<BODY>

<H1>Using JavaBeans with SSP</H1>

<jsp:usebean id="stringBean" class="VisualWave.StringBean" scope="request"/>

<LI>Initial value (getProperty):
<I><jsp:getproperty name="stringBean" property="getQuote" /></I>

<LI>Same value (ASP expression): <I><%= stringBean getQuote %></I>

<br><br>
Now changing the quote...using jsp:setproperty
<br><br>

<jsp:setproperty name="stringBean" property="setQuote" value="Best string bean is Fortex" />

<LI>Initial value (getProperty):
<I><jsp:getproperty name="stringBean" property="getQuote" /></I>

<LI>Same value (ASP expression): <I><%= stringBean getQuote %></I>

<br><br>
Now changing the quote...using ASP expression
<br><br>

<% stringBean setQuote: 'My favorite is Kentucky Wonder'. %>

<LI>Initial value (getProperty):
<I><jsp:getproperty name="stringBean" property="getQuote" /></I>

<LI>Same value (ASP expression): <I><%= stringBean getQuote %></I>
</BODY>
</HTML>

There is a lot going on here so we will break this down line by line.

<jsp:usebean id="stringBean" class="VisualWave.StringBean" scope="request"/>
The "tag" <jsp:usebean /> is one of many JSP expressions that use an XML syntax. It is equivalent <jsp:usebean > </jsp:usebean >, but since all the action happens with the attributes of this tag, the XML syntax is preferred in the JSP camp. You will see this with all other expressions that use the jsp: tag.

The <jsp:usebean /> expression is used to instanciate the object of the class specified by the class attribute and bind it to the variable specified by the id attribute. The expression above would be equivalent to:

stringBean := StringBean new.

What makes this expression more powerful is the scope attribute. This attirbute allows you to share the bean across more than one page. The possible values for scope are:
  • page - (default value) allows beans to be accessible by scriplets or expressions later on in the same page
  • application - allows beans to be accessible by scriplets or expressions later on throughout the same application
  • session - allows beans to be accessible by scriplets or expressions later on in the same session
  • request - allows beans to be accessible by scriplets or expressions later on in the same page - virtually no different than page
The most common value for scope is session and you will see this value used as an effective means of sharing the same object (bean) across multiple pages.
<jsp:getproperty name="stringBean" property="getQuote" />
The "tag" <jsp:getproperty /> is used to execute the method specified by the property attribute for the object (bean) specified by the name attribute. The expression above would be equivalent to:

stringBean getQuote.

The method getQuote (its name being a Java convention) should simply return the value of the instance variable quote (i.e. it's a "getter" method). Note that the typical Smalltalk naming conventions make this concept easier to understand since you would simply create a "getter" method of the same name as the instance variable.

For example, since our StringBean class has an instance variable called quotation and a getter method of the same name, you would have the following code:

<jsp:getproperty name="stringBean" property="quotation" />

It was important to point out this disctinction. The property attribute is actually a method of the name class; it is not a short-cut means by which to access an object's instance variable.
<jsp:setproperty name="stringBean" property="setQuote" value="Best string bean is Fortex" />
The "tag" <jsp:setproperty /> is used to execute the method specified by the property attribute for the object (bean) specified by the name attribute. The parameter to be passed to the method is specified by the value attribute. The expression above would be equivalent to:

stringBean setQuote: 'Best string bean is Fortex'.

This page shows an example of using both ASP and JSP techniques for "setting and getting" properties from our object. Even though we used a JSP convention to create our object (jsp:usebean), we could still make reference to is using ASP expressions.

Next we will change the scope attribute to session to see if our object persists across multiple pages.
6. In your favorite text editor, enter the following and save this file as bean2.ssp in the sandbox directory (just copy from here and paste it into your editor).

<HTML>
<HEAD>
<TITLE>Using JavaBeans with SSP</TITLE>
</HEAD>
<BODY>

<H1>Using JavaBeans with SSP</H1>

<jsp:useBean id="stringBean" class="VisualWave.StringBean" scope="session"/>

<LI>Initial value (getProperty):
<I><jsp:getProperty name="stringBean" property="quotation" /></I>

<LI>Same value (ASP expression):
<I><%= stringBean quotation %></I>

<br><br>
Now changing the quote...using jsp:setproperty
<br><br>

<jsp:setProperty name="stringBean" property="quotation" value="Best bean is Fortex" />

The quote should now have been changed
<br><br>
<LI>Initial value (getProperty):
<I><jsp:getProperty name="stringBean" property="quotation" /></I>

<LI>Same value (ASP expression):
<I><%= stringBean quotation %></I>

<br><br>

Click <a href=Bean3.ssp>here</a> to see if it's true on the next page.

</BODY>
</HTML>

7. In your favorite text editor, enter the following and save this file as bean3.ssp in the sandbox directory (just copy from here and paste it into your editor).

<HTML>
<HEAD>
<TITLE>Using JavaBeans with SSP</TITLE>
</HEAD>

<BODY>

<H1>Using JavaBeans with SSP</H1>

<jsp:useBean id="stringBean" class="VisualWave.StringBean" scope="session"/>

<LI>Initial value (getProperty):
<I><jsp:getProperty name="stringBean" property="quotation" /></I>

<LI>Same value (ASP expression):
<I><%= stringBean quotation %></I>

<br><br>

</BODY>
</HTML>

8. Let's fire these pages up in our browser.


Figure 3. The beans2.ssp page


Figure 4. The beans3.ssp page

Sure enough, the scope attribute of session allowed us to set the instance variable of quote on one page to a different string than its default and value and have it persist on a second page.

Summary

Using beans is really quite simple. Use the <jsp:usebean /> expression to instanciate the object, <jsp:getproperty /> to retrieve the value of an instance variable of that object (bean) and <jsp:setproperty /> to set the value of an instance variable of that object (bean).