Building JSP Internet Applications with Oracle JDeveloper
An Oracle Technical White Paper
April 24, 2000
Contents
Introduction
In the early days
of the Internet, web content consisted primarily of a set of static HTML pages and
pretty graphics. Corporate websites tended not to be much more than experimental online brochures
with static content which tended to be mostly out of date.
In recent times, businesses have realized the incredible
potential of doing business on the Internet, and the Internet has
transformed
into a bustling online
commercial environment with a wide array of sophisticated Internet applications. Today's
Internet customers have come to expect an
ever growing set of
attributes from commercial Internet applications such as:
- Superior Performance - An Internet Application
must respond in a timely manner. Internet users will simply
move on to another site if the response time is too slow.
-
Current, Personalized Content - This
content must be dynamically generated based on personal attributes of the user.
Platform Independent Browser
Requirements - Users should not have to install
proprietary plug-ins to interact with the application.
Functionally Rich, Easy To Use Interface Controls - These
include navigators tools, search windows and selection controls.
These customer requirements drive a specific set of
production requirements for the Internet application
producer:
- Scaleable Servers - Applications
must be deployed to the most Scaleable and performant machines available.
- Platform Independent Code - As the
business grows it is essential to be able to easily migrate the server code
to the most scaleable platform regardless of the server's operating
system.
- Productive, Standards Based Development
Environment - To attract new customers, there is always a constant
need for improvement and augmentation of services provided by Internet
application producers thus requiring a productive, rich development
environment which is based on standard programming
models.
Satisfying these requirements has
not been a trivial effort. Internet application producers have had to delve
into numerous proprietary and/or home grown solutions only to eventually find
them woefully inadequate in meeting today's ever more stringent Internet application
requirements.
Oracle JDeveloper and Oracle 8i fully empower building Internet applications based on open standards technologies thus
allowing Internet application producers to rise to the challenge of building component
based, professional quality Internet applications which not only will scale, providing
superior performance, but will run on any standards based application
server.
Oracle JDeveloper, Business
Components for Java and Oracle8i
By
fully leveraging the power
of JavaBeans, Servlets, JavaServer Pages and a powerful new server-side development framework, Oracle JDeveloper
provides a truly productive environment for creating industrial strength Internet applications.
As
a demonstration of the power of these technologies, a sample
Internet application
was built using Oracle JDeveloper and
Oracle 8i technologies. In keeping with current popular trends on
the Internet, the theme of this application was chosen to be
an Auctions website, thus the BC4J Auctions Internet application was born! This application is
now being offered as a sample application in Oracle JDeveloper 3.1.
The BC4J Auctions Application
The BC4J Auctions application uses
the latest in server-side Java technologies, such
as JavaServer Pages,
Java Beans, and Java Stored Procedures. The application also uses the Business
Components for Java application development framework which consists of Java and XML
to encapsulate the business logic.
The application allows customers to search and bid
on auction items as well as allow them to place their own items up for
auction. Since the application user interface is 100% HTML, it does not require
any proprietary browser plug-ins or downloaded applets
so it will
function on any browser with superior performance. Since the application is written
in Java (JavaBeans and JSP pages) it will run on any application
server supporting Servlets or JSPs.
Figure 1: A Runtime View of the BC4J Auctions Sample Application
The BC4J Auctions
application specifically consists of JavaServer Pages, Java Beans, Business Components (written in
Java and XML) and a Java Stored Procedure all of which can
be simultaneously developed in JDeveloper!
Figure 2: The BC4J Auctions Application Architecture
The
BC4J auctions is a true Java 2 Platform Enterprise
Edition compliant application as described and illustrated by the
J2EE BluePrints for developing and deploying component-based enterprise applications
from Sun Microsystems.
Figure 3: UML Diagram for the BC4J Auctions Application Entities
The core of the
auctions database design is the Items and Bids tables which hold a
master-detail relationship. Each auctions item also belongs to a specific category defined
in the Categories table. Since the application requires users to log in before
bidding, a Customers table is also included.
The BC4J auctions user interface is a set of JavaServer Pages
primarily utilizing a set of Web enabled Java
Beans, or Web Beans, to dynamically generate the HTML which make up the visual controls such as the search window, category
picker or query results page. The JSP pages invokes the Web Beans using the <JSP:USEBEAN> JSP
tag. These Web Beans query data from the database and present the data dynamically in HTML
to the user. The Web Beans also provide a way for the user
to input data such as bidding on an item, registering as a customer
etc..
Figure 4: BC4J Auctions Web Beans
By utilizing a component based approach, the application was
easy to build and is very easily modifiable and extendible. By merely modifying
or subclassing the application components, the application can be altered
to perform any number of services such as an online store, an airline/hotel
reservation system, a reverse auction or whatever is the next popular thing to
hit the Internet!
Building a Component Based JSP Application
Historically Internet applications have been built using an
incredibly diverse set of technologies. Some applications utilize home-grown
technologies which may suffice temporarily for a single application, but in the
long run these solutions can become extremely hard to maintain or extend. Other
Internet applications are often built using commercial, proprietary development
environments which force the application to forever remain trapped in a
proprietary technology which may not scale, or may not be easily upgraded or
extended.
By using a standards based, open development technology such as Java Servlets and JavaServer pages,
it is possible to build Internet applications with Oracle JDeveloper which not only can scale, but are
portable to any Java enabled server.
Another advantage of using Java as a development
environment for Internet applications is that it is an Object Oriented language
which affords the developer a productive and flexible programming environment.
An Internet application developed in Java can thus be reduced to a set of Java
components which are instantiated in a set of Javaserver Pages or Java
Servlets.
Designing an Internet Application HTML Interface
The typical Internet
Application development shop is usually comprised of two distinct types of people: the Web
Designers and the Web developers. Oracle JDeveloper and JavaServer Pages provide
the perfect medium to blend the HTML and graphics created by a Web designer
with Java code produced by a Web developer.
A well designed Internet application is typically created in the following
manner:
Web Designers will use their own preferred HTML and graphical editing tools to create a non-functional HTML prototype
design. Here
is an example of the HTML prototype of the BC4J Auctions
application.
Figure 5: The BC4J Auctions Design Mock-up
Once the prototype is complete, the Web development
staff implements the backend technology to bring the application to life. By
using Oracle JDeveloper, the prototype HTML files are easily converted into
JavaServer Pages and all the remaining Java development is done in Oracle JDeveloper.
This includes developing a business logic layer to handle the
application's functionality as well as the communication to the database.
Building the Business Logic using
BC4J
Every Internet application requires a sophisticated set
of server-side business logic to run the application. The business logic code
handles the overall application functionality such as what data can be viewed,
by whom, and what data can be modified. This translates to a
set of
rules and methods which govern all interactions with a datasource(s) as well as other
services which provide performance enhancing caching and connection pooling.
Writing
server-side
business logic can be extremely complex to build literally resulting in
thousands of lines of code and with today's Internet production
requirements, this code also has to be fully portable, easily customizable
and extendible.
By
utilizing Oracle
JDeveloper and the Business for Components for Java framework, writing
business logic which is %100 portable, extendible and customizable is now
incredibly easy.
Building
Business Components
Oracle
Business Components for Java is a Java/XML powered framework
that enables productive development, portable deployment, and flexible customization
of multi-tier, database applications. Application developers use the
Oracle Business Components integrated design-time wizards, component editors,
and productive Java coding environment to assemble business logic components.
These components are package in a container called an Application Module which
can be deployed to any enterprise server platform supporting Java technology.
Database entities such as
tables, views and
foreign key associations are represented in
Java and XML by the Business Components: Entity Objects, View Objects
and Associations
respectively.
Entity Objects
are comprised of Java code which provide a
set of access methods to alter the database entity and XML code
which stores application meta-data (business
rules).
View Objects, also comprised of XML and Java code, are
analogous the database view in that it can represent specific groupings columns
from one or many Entity Objects.
Associationsand
View Links
emulate the foreign key references in a database
and is represented entirely in XML.
In the case of the BC4J Auctions application, a set of Business Components representing the
tables: Categories, Items, Bids, and Customers as well as
all Foreign Keys references
were easily created using the
Wizards.
Validations control the input of data into the
Entity Objects. Validations ensure that certain conditions are met before
allowing insertion or modification of data in Entity Objects.
The Entity Object
wizard allows for the easy
creation of validation rules. In the auctions applications there is a validation rule that
specifies that any bid submitted must be higher than all other bids for a
particular item. To introduce this
business rule, we simply use the re-entrant wizard for
the Bids Entity Object, and create a Validation rule. This rule specifies that
for any bid to be inserted or changed, the following rule must hold:
The bidprice entered must be higher than all
other bids entered for this particular Item. This rule
is based on the
result of the following SQL
Query:
select nvl(
max(bid_price),0) from bids where bids.item_id =
%ItemId%
Domains are
validated datatypes which can represent re-usable data formats such as currency,
credit card number, URL etc.
A Domain is a Java Class who's constructor
contains a built-in validation check which ensures a specific data format. Since
applications of all types have to perform data format validation, the reusable
domain concept was created in the Business Components framework.
For the BC4J Auctions application, in order to properly
render US currency amounts (2 numbers to the right of the decimal point), a US
Currency domain was created with a default value of "0.00". The
Domain's validation rule ensures that the value always has 2 decimal points.
Testing the Business
Components
After creating the Business Components
which represent the BC4J Auctions database entities, testing can be done
immediately without having to build a user interface. The BC4J development environment
includes a powerful test utility which allows the developer
to test all facets of the server side business logic including query, insert, update,
delete actions as well as triggering validation to execute.
By clicking on the View Object names in the left
navigation panel of the Test Utility, one can fully interact
with the data represented by the View Objects of the right panel.
To check that the foreign key references are properly
functioning, one can double-click on any of the View Link items. This will launch
a master-detail window showing the coordination between the two objects linked
via the Associations and Links which map to the foreign key reference in the
database.
Figure 6: The Business Components Browser (Test Utility)
Record Caching and Application Module Pooling
One of the key benefits of the Business Components for Java
framework is it's performance enhancing record caching and connection pooling capabilities.
Business Components are encapsulated in objects known as Application Modules when deployed.
The Application Module, when invoked by a client, caches database records
to minimize database transactions thus enhancing performance.
For Business Components deployed as an Internet application, which tend to have
large numbers of browser based HTML clients, an Application Module
Registry is provided. The AppModuleRegistry object maintains a pool of running Application Modules
which alleviates the need
to invoke and destroy Application Modules per every single HTML client.
For maximum scaleability, the Application Modules in the pool can be deployed in stateless mode which
allows each Application Module to service multiple
HTML clients.
Building and using dynamic HTML generating Java Beans
- Web Beans
Once the Business Components
have been tested, the data which they represent can be integrated into an
HTML interface via a set of Java Components or Web
Beans.
A Web Bean is simply
a Web enabled Java Bean which can dynamically produce HTML. A Web Bean can
also serve as a non-visual component which performs server-side services such as inserting
a row into a database table or performing a database
query.
Since a Web Bean is just a
Java Class, it can be used in either a JSP page or a
servlet. For usage in a JSP page, one simply uses the JSP tag
<JSP:USEBEAN..> to instantiate the Javabean and execute methods of the
bean.
Example: Here is how a Web Bean is invoked from a
JavaServer Page.
<JSP:USEBEAN
class="auctionbeans.quickpick" id="qp"
scope="request"> <% qp.initialize(application,session,request,response,
out,
"package1Module.Categories"); qp.render(); %>
Since a Servlet is pure Java code, a Web Bean is
simply a Class which is instantiated and it's methods are
executed.
Example: Here is
how the same Web Bean is invoked from a Java
Servlet.
// .. inside of the Servlet's GET
response method..
auctionbeans.quickpick qp;
qp = new
auctionsbeans.quickpick();
qp.initialize(application,session,request,response,
out,
"package1Module.Categories"); qp.render();
Since WebBeans operate in Servlets or JSP pages they must
be able to interact with the basic Servlet communication Objects such as
Response, Request, Context etc.. Oracle JDeveloper provides an easy to use
WebBean Implementation which comes predefined with a common set
of methods such as Initialize (Binds to the Servlet Request,
Response objects), render (Outputs an HTML stream to the
Response Object) and execute
(Executes server-side code. Does
not output HTML to client ).
For WebBeans which require connecting to a BC4J Business
Component (View Object), a DataWebBean Implementation is also
provided. The Initialize method for the DataWebBean has an
additional argument which specifies the BC4J View Object
to bind to.
JDeveloper provides a set of pre-built WebBeans which aid in Internet application development
such as Web Beans to allow full interaction
with BC4J View Objects as well as non data-bound Web Beans such
as a Toolbar or Tree Navigator.
To create a new Web
Bean, a Web Bean Wizard is provided. The Web Beans Wizard
will either generate an example databound Web Bean (extends DataWebBean Implementation)
or an example non databound Web Bean (extends the WebBean
Implementation).
The JSP Element Wizard makes inserting Web
Beans (<JSP:USEBEAN..> tags) into JSP pages a very simple process.
When using the JSP Element Wizard, the developer can select from a 'toolbox'
of Web Beans and graphically define the runtime attributes of the Web Bean and
when finished, the wizard inserts a properly coded <JSP:USEBEAN..> tag into the JSP
page.
BC4J Auctions Web Beans
The BC4J Auctions application utilizes both pre-built
JDeveloper Web Beans as well as custom built Web Beans:
Figure 7: How the Quick Pick Web Bean Works
Quick Pick
The Quick Pick Web Bean was custom built for this application by using the Web Bean
wizard to generate a simple Web Bean
with sample code which retrieves a set of records
from a View Object and prints them in an HTML table. By modifying the output of the sample Web Bean code
to resemble the Quick Pick design created by the Web Designer, a Quick Pick
Web Bean was created with the Designer's look and feel encoded into it.
Figure 7
illustrates how the Web Bean communicates with the Categories View Object which presents data from the Categories
Entity Object which in turn has a one to one relationship with
the Categories table from the database.
Other custom Web Beans, such as HotBids, were created in a similar fashion. They were initially created
with the Web Bean wizard and modified to perform their task with a
specific look and feel.
Figure 8: The Custom HotBids Web Bean
HotBids
In the case of the HotBids Web Bean, this is a
Web Bean which reports the top 5 hottest bids. The majority of the code for
this bean is actually in the form of a Java Stored Procedure.
The concept of a Hotbid, was created to show which Items
in the Auctions database had the most customer interest by calculating by how
much the bidders were over bidding each other. A Java Stored Procedure was
created which would take the Item's ID number as an argument and calculate a
HotBid value based on the average bid increment for this particular item. Since
this calculation is row intensive, it is best done directly inside of the
database server. To accomplish this, a Java Stored Procedure was deployed to an
Oracle 8i database.
A special Business Component
(View Object) was created which is comprised of only three columns from the Items
table: ID, ItemName and a calculated column (HotBidValue) which is actually a
call to the Hotbids Java Stored Procedure passing the Item Id as the argument.
The View Object also had an Order By HotBidValue clause ensuring that the Hottest
bids are reported first. The HotBids Web Bean simply has to query the top 5
rows from the View Object and present them in
HTML.
Figure 9: The JDeveloper RowSetBrowser Web Bean
RowSetBrowser
In the BC4J application, there are numerous times when
data from the result of a query is represented in a tabular HTML format. The
RowSetBrowser Web Bean, which is provided by JDeveloper, is designed specifically to display data from
a BC4J View Object in an HTML table. There
are a number of methods to customize the visual output such as column header names,
alternating row colors etc. The figure above shows how the RowSetBrowser Web
Bean is used to display a list
of bids
for a particular item. In this instance, the Bid Price, Bid Date
and Bidder's email address are displayed.
Figure 10: The JDeveloper FindForm Web Bean
FindForm
The JDeveloper FindForm Web Bean
serves both as an HTML client rendering bean as well as a server-side processing
bean. For the client, it has a render() method which displays
an HTML form with the target set to a JSP page with another findform Web Bean
calling the execute() method
to process the incoming text sent from the form. The server-side FindForm Web
Bean executes a query based on the text entered in the field. To show the
results, one must render a RowSetBrowser Web Bean after
the FindForm Web Bean.
Figure 11: The JDeveloper EditCurrentRecord Web Bean
EditCurrentRecord
The Auctions application has many instances where users are expected to insert
data such as adding an item to the Auctions database, bidding on an item and registering
themselves as customers. All of these processes were handled by using the
EditCurrentRecord Web Bean. The EditCurrentRecord Web Bean is a multipurpose web
bean which allows users to insert, update and delete data from a BC4J
View Object. When the render method is called, it displays an HTML form with fields
automatically generated based on the columns from the View Object. Like the
FindForm it also has a server-side EditCurrentRecord bean which exists in a separate page
to parse the incoming changes via an HTTP post from the rendered HTML edit
form.
Using Oracle JDeveloper and BC4J
based Web Beans provides an extremely elegant solution which offers all the
functionality
of a professional looking rich user interface
which is quite easy to create.
Putting It All Together
With the
business logic created, the HTML user interface designed and the Web Beans created the
final step is to transform the static HTML design mock-up into a real
Internet application.
This is done by inserting the Web Beans, which present
data from the Business Components, into the JSP pages by using the JSP
Element wizard.
For
Example: To insert the Hotbids Web Bean into the
JSP page, the JSP Element Wizard is invoked and the Hotbids Web Bean
is selected:
Figure 12: The JSP Element Wizard Inserting the HotBids Web Bean
Extending a JSP/Servlet
Application With XML Based Clients
In addition to generating HTML content, Web Beans can be used to generate content of any mime-type.
Because of the emergence of XML as an industry standard for applications data exchange, JDeveloper provides
an easy to use Web Bean which can dynamically generate XML data from a Business Component View Object.
Using the XML Data Generator Web Bean,
and XSL style sheets, it is possible to create an application user interface for virtually any
type of client be it
a wireless phone, palmtop computer or other devices yet to be invented. By simply leveraging the existing
Business Components framework, an XML client can be easily created and will
function with the same business logic as an HTML client!
Figure 13: The BC4J Auctions Application with XML Clients
Managing Web Beans and
Servlets With The Web Object Manager
One of the most
powerful advantages of developing an application in Java is its object oriented
nature which allows developers around the world to share reusable components. Since
Web Beans are just JavaBeans, they can easily be shared by any JSP/Servlet
application developer. Oracle JDeveloper 3.1 now has additional support for
managing reusable Web enabled Java Components by offering the Web Object
Manager.
The Web Object Manager is an Oracle JDeveloper 3.1
Wizard which allows for the maintenance of the re-usable Web Objects:
- Java Beans
- Web Beans (JDev Implementation - nondatabound)
- Data Web Beans (JDev Implementation - databound)
- Servlets
The Web Object Manager provides the ability to add (register)
external Web Beans (Java Classes) into the JDeveloper JSP development environment
and make it available in the JSP Element Wizard. It also allows the developer to
Subclass an existing Web Bean and customize it for their own use.
Figure 14: Adding a Web Bean to the Web Object Manager
The Web Object Manager also provides a way to register
Servlets so they will run simultaneously in the built-in webserver therefore it is
possible to run multiple servlets in JDeveloper at the same time.
Testing and Debugging a JSP/Servlet Application
Once the Web Beans have been inserted into the JSP
pages, testing can be done immediately by running the JSPs directly inside of
JDeveloper. Because Oracle JDeveloper has the built-in capability of running servlets and JSPs, one
simply has to select (highlight) the JSP file and right-click
run. This starts the built-in JSP/Servlet webserver and executes the selected JSP page. The
default browser of the development environment will then launch with the JSP
loaded.
Figure 15: Running a JSP Application in JDeveloper
Debugging a JSP or Servlet application is done
by first setting some debug points in either the JSP or Servlet code and then selecting
right-click Debug. This launches the JSP page or Servlet and will stop execution
at the preset debug points.
Figure 16: Debugging a JSP Application in JDeveloper
Deploying a JSP/Servlet Application
Deploying a JSP or Servlet based application is
conceptually very easy as long as the developer understands all of the components which make up
the running application.
A typical JSP application will
contain the following:
-
JSP pages
-
HTML files
- Additional support files. (images, css, Javascript
...)
- Java classes (Web Beans,
Business Components ..)
The JSP,
HTML and other support files can easily be deployed by copying them to the appropriate location on the
Web Server filesystem.
The Java classes which represent all of
the Server-side code such as the Business Components layer as well as any custom Web
Beans, must be deployed on the server and more specifically be in the CLASSPATH of
the Server's Java environment. The most efficient way to accomplish this is
to package all the Class files into a single Java Archive (JAR) file and deploy the single file
to the server. This is known as a local deployment of Business Components and provides optimal performance for
Internet applications.
To deploy in local mode, a JAR file of the business logic is created by
selecting the project which
contains the Business Components and invoking the Deployment Wizard (Project->Deploy->New Deployment Profile...)
and selecting: Deploy as a simple archive file.
Figure 17: Deploying a JSP Application
The subsequent wizard pages allow the developer to pick
exactly which files
to include or exclude. The Business Components, which are Java Classes and XML
files, are the only files needed in the JAR file. All other files such as
JSPs or HTML files would be excluded since they are copied directly to the WebServer filesystem and remain in an
unarchived format. *
* Future versions of the JSP specification will support a
deployment architecture which will allow for the packaging of Java Classes,
JSPs, HTML and images into a single file known as Web
Archive (WAR) file.
Once all of the components which make up
the application have been deployed to the server environment, the JSP application will run exactly as it had
run from JDeveloper.
Deploying Business Components as an EJB or Corba Object.
It is also possible to deploy the Business Components as an EJB or Corba Object, which is the preferred method
when creating traditional Java client applications.
For further info on Business Components
deployment options see the Oracle Business Components for Java technical white paper.
Debugging Deployed JSP/Servlet
Applications.
With Oracle JDeveloper 3.1 it is now possible to Debug
JSP or Servlet applications even after having been deployed. This is done by using the Java Platform Debugger Architecture (JPDA) which
provides debugging support for the Java 2 Platform. For more information see: Remote Debugging
Server Side Java
Summary
By taking full advantage of Oracle JDeveloper, Business
Components for Java and Oracle 8i, one can easily create an Internet application
which meets today's Internet customer's stringent requirements such as:
Current, personalized content which is dynamically
generated.
Platform independent browser requirements.
A set of rich, easy to use interface controls such
as navigators, search windows and selection controls.
Since an Internet application built with
JDeveloper and Business Components for Java is entirely based on Open standards and
the Java API, the application will be completely portable to any host environment from
a simple laptop to the largest mainframe. The application will also be
easy to modify and extend thus providing the customer with an endless stream of
future enhancements without having to recode or change
architecture.
Oracle
Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065,
USA
http://www.oracle.com |
Worldwide
Inquiries:
1-800-ORACLE1
Fax 650.506.7200 |
Oracle, JDeveloper,
and Enabling the Information Age are trademarks of Oracle Corporation.
All other company and product
names mentioned are used for identification purposes only, and may be trademarks
of their respective owners
Copyright Oracle Corporation
2000
All Rights Reserved |
|