The JSP Files (part 7): Bugs, Beans And Banks - Turning Up The Heat (
Page 7 of 7 )
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. Here it is:
temperature.zipAnd
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!