Home arrow Java & J2EE arrow Page 6 - The JSP Files (part 7): Bugs, Beans And Banks

Bean Bag - 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.

  1. The JSP Files (part 7): Bugs, Beans And Banks
  2. Bugathon
  3. Exceptionally Clever
  4. Bad News
  5. You Throw(), I'll Catch
  6. Bean Bag
  7. Turning Up The Heat
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 2
April 18, 2001

print this article


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 at http://java.sun.com/docs/books/tutorial/javabeans/. 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 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.{mospagebreak title=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.

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: