You are on page 1of 14

Developing Web Applications Using PL/SQL

An Oracle Technical White Paper

March 1999

Web Applications today require that information be customized and dynamically generated, using
data from the database. Such customized and dynamically generated web pages can be generated
either in the middle tier or in the database. Oracle's stored procedure language, PL/SQL, deployed in
the database, is widely used in web-enabled database applications for describing the application’s
business and presentation logic. This white paper describes how PL/SQL can be used for developing
such web applications.


Oracle database application developers have been using PL/SQL as a stored procedural language for
executing business logic in the database. By adding a web gateway to the database and an HTTP
server, Oracle extended PL/SQL. It has become a powerful web application development platform as
well. Oracle Web Agent (OWS 1.0), developed as a CGI-bin application, enabled users to use
PL/SQL packages and procedures to develop web applications. As a part of the initial release, Oracle
provided a comprehensive mapping of HTML tags to PL/SQL packages. Later versions (up to 4.0)
added scalability and distributed capability to Oracle's PL/SQL specialized datatype, enabling it to be
a key component of a web development platform.


The PL/SQL web development platform consists of three parts that are integrated into Oracle's
Application Server. They are explained in detail below.

Developing Web Applications Using PL/SQL 2

March 1999
H Cartridge PL/SQL
T OAS Runtime
Client P

OAS Server

OAS Architecture

Figure 1.


HTTP server, a.k.a. listener, is the daemon that listens to the port (usually port 80) and understands
the HTTP protocol. This is an interchangeable component, allowing users to use either the Oracle-
provided default listener, or choose their existing HTTP server (Apache, Netscape, or Microsoft IIS
server). Any of these other HTTP servers can be easily configured and plugged into the OAS
infrastructure. The HTTP server communicates with an adapter, which in turn communicates with a
resource manager. The resource manager dispatches the client requests, and routes the responses
back to the client browsers.


The PL/SQL cartridge is a runtime-loadable component that acts as a gateway, to connect to the
database and marshal the HTTP requests and responses to and from the database. This component is
dynamically loaded and can be distributed across machine boundaries, improving application
scalability. The PL/SQL specialized datatype is implemented as an ORB object. The
implementation details are transparent to the web application developer.

Developing Web Applications Using PL/SQL 3

March 1999
The PL/SQL specialized datatype provides the syntactic and semantic mapping between URLs and
the PL/SQL stored procedures. It allows users to execute PL/SQL procedures stored within the
database, by marshaling the parameters and values sent via the GET or POST HTTP methods. The
responses are streamed back to the clients as HTML pages, after the PL/SQL procedure executes in
the database. The specialized datatype provides all the plumbing required for the database
communication, and hides all implementation issues from the web application developer.


This is a core component and provides a comprehensive set of mapping functionality for HTML tags,
using PL/SQL packages and procedures. In addition to HTML mapping, this component also
provides the infrastructure for communicating with the specialized datatype and a back end interface,
for marshaling the data over the network. A brief outline of the package functionality is
given below:

•= HTF: Hypertext Functions - PL/SQL functions for generating HTML tags

Contains the functions, with comprehensive HTML mapping, for generating the HTML tags needed for
creating dynamic web pages.

•= HTP: Hypertext Procedures - PL/SQL procedures for generating HTML tags

These procedures are wrappers for HTF functions that have identical functionality. Generally,
HTP procedures are used to generate the HTML tags with nested HTF functions.

•= OWA_COOKIE: HTTP Cookies - PL/SQL interface for managing HTTP cookies

The owa_cookie package is used for sending and receiving cookies in HTTP headers.

•= OWA_UTIL: Utility Package - General utility interface including HTTP server side interface

This package contains utility subprograms for performing operations such as getting the value of CGI
environment variables, printing the data returned to the client, and printing the results of a query in an
HTML table.

•= OWA_TEXT: Text Manipulation - Utility for managing large amounts of text

This package contains subprograms for manipulating strings in different formats.

Developing Web Applications Using PL/SQL 4

March 1999
•= OWA_PATTERN: Pattern Matching - Utility for pattern matching

This package enables the application developer to locate text patterns within strings, and replace the matched
string with another, given string.

•= OWA_IMAGE: Image Mapping - PL/SQL interface for handling image maps

This package enables the application developer to create an image map that invokes a PL/SQL cartridge
when clicked.

•= OWA_OPT_LOCK: Optimistic Locking - Optimistic locking utility for web interface

This package implements an optimistic locking scheme that can be used in stored procedures, to check if the
row that the user is interested in updating has been changed before updating it.

•= OWA_CUSTOM: Customization package - Package for customizing security and other routines

This package provides an extensible mechanism that enables application developers to deploy application-
specific PL/SQL packages.

•= UTL_HTTP: HTTP Calling Utility - Package to make HTTP requests from PL/SQL as HTTP

This package contains subprograms that make HTTP requests from PL/SQL, and that receive the HTTP

These packages allow the application developer to generate web pages and load the necessary
information from the database, using PL/SQL function calls within these pages. The OWA_UTIL
package is one of the most widely used packages, and exposes the HTTP server-side interface, HTML
Formatting, and SQL Query interfaces to PL/SQL application developers.


The PL/SQL specialized datatype and toolkit provide complete functionality that includes
performance optimizations, information security , and transaction management.


•= URL to PL/SQL procedure mapping: Simple and efficient way to map URLs to PL/SQL procedures,
to create dynamic web pages.

•= Globalization support through NLS: Comprehensive and complete support for Oracle supported
NLS languages.

Developing Web Applications Using PL/SQL 5

March 1999
•= Generic parameter passing: Flexible mechanism for handling query string parameters.

•= SQL File support: Support for executing anonymous blocks stored in SQL files.

•= Positional parameter binding: Support for binding values in query string parameters in positional

•= File upload/download: Interactive method for using the web to upload and download files into the


•= Connection Pooling: Provides an efficient implementation for reusing database connections across
HTTP requests.

•= Session Caching: Provides performance improvements, by reusing logical sessions across HTTP

•= State Management: A transparent implementation for maintaining transaction states across HTTP


•= Fault Tolerance: Provides robust and comprehensive support for fault tolerance.

•= Failure Recovery: Provides a resilient error recovery mechanism, resulting in high availability.

•= Error Reporting and Logging: Provides complete tracing and logging facilities to track errors and

•= Load balancing: Provides an automated and configurable way for distributing load across machines.

•= Life cycle management: Provides an efficient implementation for life cycle and policy management
for specialized datatypes.


•= Web-based and Database-based Authentication: Provides fine-grained security and access control,
supporting both web-based and database-based authentication mechanisms.

Developing Web Applications Using PL/SQL 6

March 1999

Application developers can develop transaction-enabled web applications that span across multiple
databases, using Oracle's Transaction Server (OTS). Oracle Applications Server supports a declarative
transaction model, where developers can associate URLs with transaction phases (begin, commit,
rollback) and the corresponding database transactions will be carried out within the scope of that
transaction, monitored by OTS. This feature does not involve any additional effort, and enables
developers to quickly transaction-enable their applications.


This section briefly explains different types of applications that can be developed and deployed, using
the Oracle database and PL/SQL. Two different applications are discussed in this section: Internet
and Extranet applications.


Here we will discuss a news and quote server site, a typical web site used in most web portals today.
A typical application would display a web page with customized greeting and content for an end
user. The functional features of this system must include content storage, search mechanisms,
security, and profile management.

This architecture typically includes a Feeds engine, a database server , and a web server interface.

The Feeds engine, basically, gathers the News and the quotes from a satellite feed, press wires, or
from other web sites, and loads them into the database. The articles are stored in a relational table.
A search capability can be added, by using Oracle's Context option. The Feeds loader can be an
Oracle application written in C or Java™, using OCI, JDBC, PRO*C, or J*SQL. The purpose of this
application is to insert news stories inside the content storage tables. An equivalent solution would
be to provide a web interface, using the HTTP PUT method to upload the news stories and image
files into the database.

A schematic diagram describing such a site appears below:

Developing Web Applications Using PL/SQL 7

March 1999
Oracle Server

Manager Table
Contents &
PL/SQL Engine
Engine Loader

Auth Manager Ad Manager

Table Table

Schematic of News Server

Figure 2.

The database schema must contain the logic and data for:

•= Authentication Manager: A module to authenticate users, manage passwords, cookies etc., and
meter usage.

•= Profile Manager: A module to customize content and presentation profiles for different users.

•= Ad Manager: A module to manage and meter advertisements.

•= Content Manager: A module to store articles and images

All these features are database-centric, and are ideally implemented within the database. Using
Oracle for storage and PL/SQL for expressing business logic with tightly coupled data access requires
minimal hardware, software, and technical resources. Storing the content in a database provides a
good documentation management system, with built in versioning, archiving, and
searching capabilities.

Developing Web Applications Using PL/SQL 8

March 1999

Let us take a Human Resources (HR) web site, a typical intranet site used in any corporate
environment. A successful HR web site would allow employees to manage their personal benefits
(like insurance services and 401K funds), view their own personal compensation packages, share
documents across the enterprise, and look at training registration. The figure shown below is a
schematic diagram of a corporate intranet web application.

Oracle Server
HR Benefits view Sales view

Output Business

HTTP Corporate

Auth Manager Table HR Personnel view Finance view

Schematic of a corporate intranet server

Figure 3.

Real world ERP applications project multiple views of the same corporate data, based on user need.
The intranet applications use the functional views and should not require any other data. The
deployed ERP applications that govern corporate operations that are written in PL/SQL can be

Developing Web Applications Using PL/SQL 9

March 1999
quickly web-enabled, by calling the business logic through PL/SQL web toolkit packages. This does
not involve any new business logic to be developed, except for adding presentation logic for HTML
generation. By enabling the database itself to use PL/SQL to generate dynamic web pages, a unique
and secure solution can be achieved. This kind of solution avoids data redundancy, since data is
stored and retrieved from only one resource. Developing the applications, using PL/SQL within the
database, leads to a highly available, secured system. This application environment provides secular
and controlled access for the intranet users, with different privileges that are based upon database
roles and privileges.


Let us take a supply chain management application, a typical extranet site used in consumer, vendor
corporate environments. For example, take a manufacturing company that uses other small vendors
to make semi-finished goods, or to supply raw materials. If the manufacturing company wants to
coordinate the shipping dates for their deliverables, they have to in turn coordinate the delivery dates
from their vendors.

Developing Web Applications Using PL/SQL 10

March 1999
Corporate DB &

HTTP Web Server

Corporate DB &
Website Raw Materials Vendor

Output Web Server

Corporate web site Corporate DB &


Web Server

Sub Assembly Distributor

Schematic of a extranet web site

Figure 4.

Current implementation of supply chain management software is primarily within a corporation,

leading to problems coordinating information across organizations. The coordination between these
organizations is done manually, over telephones, fax machines, or through batch mode EDI
processing. To reduce the inventory and to minimize the time to ship, a real time, open
communication mechanism is necessary. HTTP, with its openness and ubiquity, is an ideal
mechanism to implement such a system. Thus, PL/SQL, a tightly coupled language to express SQL
when enabled with HTTP client and server side interfaces (UTL_HTTP and OWA_UTIL PL/SQL
packages), can be efficiently used in extranet applications.

Developing Web Applications Using PL/SQL 11

March 1999

The PL/SQL packages and the specialized datatypes are optimized, for best performance. These
packages are optimized for HTML buffer stream packing, NLS translations. The specialized datatype
optimizations include connection pooling, session caching, multithreaded implementation to serve
concurrent requests, minimized round trips to the database for procedure, and parameter matching
and optimizations from the Oracle8i server itself.


Oracle web solutions support a fault-tolerant error recovery mechanism. The runtime support saves
the user from worrying about life cycle management, error tracing, and recovery. The system is
highly scaleable, and can be distributed across multiple hardware to achieve the best results.


The current implementation does not support URL mapping, to include complex PL/SQL data types,
like Object data types and Collections, Records, and RefCursors.


A script-based development environment, to embed PL/SQL blocks within HTML pages, is planned
for a future release. This feature will enable application developers to create HTML templates, and
to embed PL/SQL code blocks within them. This will significantly improve performance, since
HTML tag generation will not be necessary. This feature will enable customers to use standard
HTML editors to develop web pages and use an IDE environment to generate and embed PL/SQL
blocks within the HTML templates.

Developing Web Applications Using PL/SQL 12

March 1999

Web applications are increasingly becoming database-backed, driven by the need for significantly
high levels of personalization and dynamic content generation. In addition to that, corporations are
racing to quickly deploy existing applications on the web. For applications developed using PL/SQL,
OAS provides an excellent way of migrating existing applications to the Web, by allowing the
application developer to reuse large portions of existing business logic.


For more information and details on the above features in PL/SQL, visit our
web site at:, or send us an email at

Developing Web Applications Using PL/SQL 13

March 1999
Oracle Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065

Worldwide Inquiries:
Fax +1.650.506.7200

Copyright © Oracle Corporation 1999

All Rights Reserved

This document is provided for informational purposes only, and

the information herein is subject to change without notice.
Please report any errors herein to Oracle Corporation. Oracle
Corporation does not provide any warranties covering and
specifically disclaims any liability in connection with this

Oracle, Oracle Secure Network Services, and SQL*Net are

registered trademarks, and Oracle8i, PL/SQL, Pro*C, and Oracle
Expert are trademarks or registered 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.