14-2. Creating a Common Set of HTML Page Generation Procedures

Problem

Every web page you generate with a PL/SQL procedure requires a common HTML tag to start and another to finish every web page, and you do not wish to repeat the code to add those tags in every procedure you write for the Web.

Solution

Create a package that contains calls to the PL/SQL Web Toolkit procedures that produce the HTML code necessary to properly display a well-formed,1 HTML web page. In this example a package is created with two procedures, one to generate the HTML tags required to start a page and one to generate the closing HTML tags to finish a page.

CREATE OR REPLACE PACKAGE common AS

   PROCEDURE header (title VARCHAR2);
   PROCEDURE footer;

END common;

CREATE OR REPLACE PACKAGE BODY common AS

PROCEDURE header (title VARCHAR2) IS

BEGIN

   htp.p ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ' ||
          '"http://www.w3.org/TR/REC-html40/loose.dtd">'),
   htp.htmlOpen;
   htp.headOpen;
   htp.meta ('Content-Type', null, 'text/html;' ||
              owa_util.get_cgi_env('REQUEST_IANA_CHARSET') );
   htp.meta ('Pragma', null, 'no-cache'),
   htp.Title (title);
   htp.headClose;
   htp.bodyOpen;
   htp.header (2, title);

END HEADER;

PROCEDURE footer IS

BEGIN

-- This is a great place to add legal disclaimers, about us, contact us, etc. links
   htp.hr;  -- horizontal line
   htp.anchor ('http://www.mynode.com/legal_statement.html', 'Disclaimer'),
   htp.anchor ('http://www.mynode.com/About.html', 'About Us'),
   htp.bodyClose;
   htp.htmlClose;

END footer;

END common;

____________________

1 A well-formed HTML web page conforms to the standards defined by The World Wide Web Consortium (W3C). You can validate your HTML web pages at http://validator.w3.org/.

How It Works

Recipe 14-1 includes a test procedure to verify the DAD is setup correctly; however the test procedure does not produce a well-formed HTML page. Here is the updated example from Recipe 14-1, this time with calls to the common header and footer procedures.

create or replace procedure test as
begin
   common.header ('Test Page'),
   htp.p ('Hello World!'),
   common.footer;
end;

This procedure, when called from a web browser, produces the following HTML code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/Image
TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" NAME="" CONTENT="text/html;WINDOWS-1252">
<META HTTP-EQUIV="Pragma" NAME="" CONTENT="no-cache">
<TITLE>Test Page</TITLE>
<BODY>
<H2>Test Page</H2>
Hello World!
</BODY>
</HTML>

The header routine generates the necessary opening HTML code to properly display a web page. It begins by setting the document type, then sending the opening <HTML> and <HEAD> tags. It sets the content-type to the character set defined in the Apache environment variable, which is retrieved using a call to the PL/SQL Web Toolkit's owa_util.get_cgi_env routine. The Pragma <META> tag tells the browser not to store the page's content in its internal cache. This is useful when the PL/SQL routine returns time-sensitive data because the users need to see real-time data. The remaining code sets the title in the user's browser, opens the <BODY> tag and displays the title on the user's web browser.

The footer routine closes the <BODY> and <HTML> tags. As stated in the code's comments, this is a good place to include any legal disclaimers or other useful text or links required for every web page generated.

Oftentimes when creating an application, you will create several procedures that will make use of the same code. You could copy the code throughout your procedures, but it is more efficient and safer to write once and use in many different places. The creation of a common codebase that is accessible to each PL/SQL object within a schema can be quite an effective solution for storing such code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.234.70