Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Bean Bag

One of the most compelling things about JSP, from a development point of view, is that it allows for the easy integration of existing JavaBeans into JSP scripts (for those of you not in the loop, JavaBeans is object-oriented technology which allows developers to build reusable Java components or applications). The advantages of this are obvious: an organization which has already made an investment in JavaBeans technology can leverage off it to quickly reuse existing code modules, at minimal time and cost.

We're not going to get into the nitty-gritty of building a JavaBean here - there are innumerable tutorials out there on the topic, including a good one from Sun Microsystems. Instead, we're simply going to touch briefly on the JSP constructs which allow you to import a Bean into your JSP script, set Bean properties and access Bean methods.

JavaBeans are brought into a JSP script by means of the <jsp:useBean> action, which creates an instance of the Bean and identifies the scope of its activities.

The following code snippet creates an instance of the Bean "iceCream", identifies it with the unique ID "vanilla" and defines its scope to be limited to the "page".

<jsp:useBean id="vanilla" scope="page" class="iceCream">                                    
</jsp:useBean>

The "scope" attribute above defines the extent of the Bean's influence. For example, "scope=page" implies that the instance will remain active for the current page, while "scope=session" indicates that the instance will remain available throughout the session.

Taking It To The Bank

Closely related to <jsp:useBean> is <jsp:setProperty>, typically used to set Bean properties; these properties may be set explicitly, or on the basis of parameters available in the Request object (you remember this, don't you?)

The following code snippet uses the <jsp:setProperty> action to assign the value "7683" to the Bean property "accountBalance". Note that the <jsp:setProperty> action references an instance of a previously-defined Bean, named "account"

<jsp:useBean id="account" scope="page" class="Bank">                                    
<jsp:setProperty name="account" property="accountBalance"                                    
value="7683" /> </jsp:useBean>

If you'd like to set instance properties on the basis of data in the Request object - say, form values - you could use

<jsp:setProperty name="account" property="*" />

and JSP would automatically iterate through the Request object, match parameter names with available Bean properties, and assign values appropriately.

Turning Up The Heat

In order to illustrate how this works, we've written a simple Bean which accepts temperature values and converts them between Celsius and Fahrenheit scales. /#l#http://www.webmasterbase.com/examples/jspfiles/temperature.zip/#lt#/Here it is./#el#/

And here's a page which uses it:

<html>                                    
<head>                                    
<basefont face="Arial">                                    
</head>                                    
<body>                                    
<!-- initialize the Bean, set some defaults -->                                    
                                   
<jsp:useBean id="c" scope="page" class="Temperature">                                    
<jsp:setProperty name="c" property="celsius" value="10" />                                    
</jsp:useBean>                                    
                                   
<%                                    
// get the current temperature                                    
out.println("Temperature in Celsius is " + c.getCelsius() + "<p>");                                    
                                   
// turn up the heat                                    
c.setCelsius(36.8);                                    
                                   
// get the current temperature                                    
out.println("Temperature in Celsius is now " + c.getCelsius() + "<p>");                                    
                                   
// convert the temperature to Fahrenheit                                    
out.println(c.getCelsius() + " Celsius is " +                                    
c.convertCelsiusToFahrenheit(c.getCelsius()) + " Fahrenheit<p>");                                    
                                   
// ...and back again                                    
out.println(c.getFahrenheit() + " Fahrenheit is " +                                    
c.convertFahrenheitToCelsius(c.getFahrenheit()) + " Celsius<p>"); %>                                    
                                   
</body>                                    
</html>

And here's the output:

Temperature in Celsius is 10.0                                    
Temperature in Celsius is now 36.8                                    
36.8 Celsius is 98.24 Fahrenheit                                    
98.24 Fahrenheit is 36.8 Celsius

And that's about it for the moment. In the next - and final - article in this series, we'll be exploring JSP's tag libraries, which allow Web designers to add powerful funtionality to their Web pages without knowing JSP. Make sure you make it out here for that one!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links