Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Embedding PL/SQL Code in Web Pages (PL/SQL Server Pages)

The METHOD=GET format is more convenient for debugging and allows visitors to pass exactly the same parameters when they return to the page through a bookmark.

The METHOD=POST format allows a larger volume of parameter data, and is suitable for passing sensitive information that should not be displayed in the URL. (URLs linger on in the browser's history list and in the HTTP headers that are passed to the next-visited page.) It is not practical to bookmark pages that are called this way.

Examples of PL/SQL Server Pages

This section shows how you might start with a very simple PL/SQL server page, and produce progressively more complicated versions as you gain more confidence.

As you go through each step, you can use the procedures in "Loading the PL/SQL Server Page into the Database as a Stored Procedure" on page 13-29 and "Running a PL/SQL Server Page Through a URL" on page 13-30 to compile the PSP files and try them in a browser.

Sample Table

In this example, we use a very small table representing a product catalog. It holds the name of an item, the price, and URLs for a description and picture of the item.

Name

Type

----------

-------------

PRODUCT

VARCHAR2(100)

PRICE

NUMBER(7,2)

URL

VARCHAR2(200)

PICTURE

VARCHAR2(200)

Guitar 455.5

http://auction.fictional_site.com/guitar.htm http://auction.fictional_site.com/guitar.jpg

Brown shoe 79.95

http://retail.fictional_site.com/loafers.htm http://retail.fictional_site.com/shoe.gif

Radio 9.95

http://promo.fictional_site.com/freegift.htm

Developing Web Applications with PL/SQL 13-31

Embedding PL/SQL Code in Web Pages (PL/SQL Server Pages)

http://promo.fictional_site.com/alarmclock.jpg

Dumping the Sample Table

For your own debugging, you might want to display the complete contents of an SQL table. You can do this with a single call to OWA_UTIL.TABLEPRINT. In subsequent iterations, we use other techniques to get more control over the presentation.

<%@ plsql procedure="show_catalog_simple" %> <HTML>

<HEAD><TITLE>Show Contents of Catalog (Complete Dump)</TITLE></HEAD> <BODY>

<% declare

dummy boolean; begin

dummy := owa_util.tableprint(’catalog’,’border’); end;

%>

</BODY>

</HTML>

Printing the Sample Table using a Loop

Next, we loop through the items in the table and explicitly print just the pieces we want.

We could adjust the SELECT statement to retrieve only a subset of the rows or columns.

We could change the HTML or the location of the expressions to change the appearance of each item, or the order in which the columns are shown.

At this early stage, we pick a very simple presentation, a set of list items, to avoid any problems from mismatched or unclosed table tags.

<%@ plsql procedure="show_catalog_raw" %> <HTML>

<HEAD><TITLE>Show Contents of Catalog (Raw Form)</TITLE></HEAD> <BODY>

<UL>

<% for item in (select * from catalog order by price desc) loop %> <LI>

13-32 Oracle Database Application Developer's Guide - Fundamentals

Embedding PL/SQL Code in Web Pages (PL/SQL Server Pages)

Item = <%= item.product %><BR> price = <%= item.price %><BR> URL = <I><%= item.url %></I><BR>

picture = <I><%= item.picture %></I> <% end loop; %>

</UL>

</BODY>

</HTML>

Once the previous simple example is working, we can display the contents in a more usable format.

We use some HTML tags around certain values for emphasis.

Instead of printing the URLs for the description and picture, we plug them into link and image tags so that the reader can see the picture and follow the link.

<%@ plsql procedure="show_catalog_pretty" %> <HTML>

<HEAD><TITLE>Show Contents of Catalog (Better Form)</TITLE></HEAD> <BODY>

<UL>

<% for item in (select * from catalog order by price desc) loop %> <LI>

Item = <A HREF="<%= item.url %>"><%= item.product %></A><BR> price = <BIG><%= item.price %></BIG><BR>

<IMG SRC="<%= item.picture %>"> <% end loop; %>

</UL>

</BODY>

</HTML>

Allowing a User Selection

We have a dynamic page, but from a user point of view it may still be dull. The results are always the same unless you update the catalog table.

To liven up the page, we can make it accept a minimum price, and present only the items that are more expensive. (Your customers' buying criteria may vary.)

When the page is displayed in a browser, by default the minimum price is 100 units of the appropriate currency. Later, we will see how to allow the user to pick a minimum price.

<%@ plsql procedure="show_catalog_partial" %>

Developing Web Applications with PL/SQL 13-33

Embedding PL/SQL Code in Web Pages (PL/SQL Server Pages)

<%@ plsql parameter="minprice" default="100" %>

<HTML>

<HEAD><TITLE>Show Items Greater Than Specified Price</TITLE></HEAD> <BODY>

<P>This report shows the items whose price is greater than <%= minprice %>. <UL>

<% for item in (select * from catalog where price > minprice order by price desc) loop %>

<LI>

Item = <A HREF="<%= item.url %>"><%= item.product %></A><BR> price = <BIG><%= item.price %></BIG><BR>

<IMG SRC="<%= item.picture %>"> <% end loop; %>

</UL>

</BODY>

</HTML>

This technique of filtering results is fine for some applications, such as search results, where users might worry about being overwhelmed by choices. But in a retail situation, you might want to use an alternative technique so that customers can still choose to purchase other items.

Instead of filtering the results through a WHERE clause, we can retrieve the entire result set, then take different actions for different returned rows.

We can change the HTML to highlight the output that meets their criteria. In this case, we use the background color for an HTML table row. We could also insert a special icon, increase the font size, or use some other technique to call attention to the most important rows.

At this point, where we want to present a specific user experience, it becomes worth the trouble to lay out the results in an HTML table.

<%@ plsql procedure="show_catalog_highlighted" %> <%@ plsql parameter="minprice" default="100" %> <%! color varchar2(7); %>

<HTML>

<HEAD><TITLE>Show Items Greater Than Specified Price</TITLE></HEAD> <BODY>

<P>This report shows all items, highlighting those whose price is greater than <%= minprice %>.

<TABLE BORDER> <TR> <TH>Product</TH>

13-34 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g