86
Lesson 2 JSP Tutorial
Accessing a JavaBean
A JavaBean is a particular type of a Java class that conforms to a set of design standards
specified in the JavaBean API. A JavaBean does not inherit from any particular base class
or interface but must provide methods for accessing its properties. A JavaBean is different
from an EJB.
Within Java technology, some of the ways that you can use JavaBeans include the
following examples:
•
A JSP or servlet instantiates a bean and invokes its methods directly.
•
A JSP employs the
useBean
action to get and set JavaBean properties and optionally,
invokes its methods directly.
In the following tutorial procedure, a JSP accesses a JavaBean that encapsulates the
business logic to retrieve data from a database. You code a JSP action to instantiate and
reference a JavaBean that retrieves detailed trip information.
In this procedure, you use the following JSP actions:
In tripdetail.jsp, you instantiate the
TripBean
JavaBean and use it to access detailed
information about a selected trip.
To instantiate a JavaBean:
1 Open the file
tripdetail.jsp
located in jrun_root/servers/tutorial/compass-ear/
compass-war.
2 Where indicated in the file, add the following JSP code:
<jsp:useBean id="trip" scope="session" class="compass.TripBean"/>
<jsp:setProperty name="trip" property="tripId"/>
3 Review the
jsp:getProperty
code, which outputs the trip name, description, price,
start date, and end date.
The
TripBean
JavaBean provides the data access logic to retrieve information about a
trip from the database.
This tutorial lesson shows an example of application partitioning and role separation in
J2EE application development. The web developer builds the user interface in the JSP
and does not need to know Java. The Java developer writes the business logic and need
not be concerned about how the page looks. This lesson also demonstrates how to use the
useBean
,
setProperty
, and
getProperty
tags.
The next procedure shows how to write a JavaBean that is used in a JSP.
Element name
Purpose
jsp:useBean
Defines an instance of a JavaBean
jsp:setProperty
Sets the value of one or more JavaBean properties
jsp:getProperty
Writes the value of the bean property as a string to the page output
Summary of Contents for 38000382 - Macromedia JRun - Mac
Page 1: ...Getting Started with JRun...
Page 16: ......
Page 68: ...54 Chapter 5 Introduction to EJB...
Page 82: ......
Page 110: ...96 Lesson 3 EJB Tutorial...
Page 128: ...114 Lesson 4 Web Services Tutorial...
Page 132: ...118 Index...