HomeJava & J2EE Page 7 - The JSP Files (part 7): Bugs, Beans And Banks
Turning Up The Heat - Java
In this week's episode, find out how the JSP Exception objectprovides developers with a graceful way to recover from script errors. Andthen take a quick tour of the JSP directives you need to know in order tointegrate standalone JavaBeans into your JSP scripts.
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:
<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!