Eoug2000 Paper60 1.0
Eoug2000 Paper60 1.0
Eoug2000 Paper60 1.0
1. Summary
At CERN, as increasingly elsewhere, server based applications using the PL/SQL cartridge and Java are preferred as they make minimum demands on the client and ease maintenance. The PL/SQL cartridge, the original method offered by Oracle, has proved reliable and effective. We have used it to build web applications for more than four years. Newer applications are using Servlets together with the application server. This paper presents the different techniques that we designed and the pitfalls that we encountered in diverse projects such as a technology transfer database, a product management tool and a system to keep track of physics events. Important considerations are security, ease of maintenance, transaction handling and modularity. Most of the experience has been gained using the PL/SQL cartridge and this will be contrasted and compared with the newer Java techniques
2. Introduction
The Oracle Application Server PL/SQL cartridge is one of the techniques provided by Oracle to publish database-oriented content on the Web. It is based on PL/SQL, Oracles proprietary procedural language. First versions have been released in 1995 and it has since had a lot of success. A few years ago, Sun Microsystems introduced Java as a language with the following main characteristics: object orientation, no pointer arithmetic, and compilation into a machine independent bytecode that is executed in a virtual machine. The Java Servlet technology is a standard Java Application Programming Interface defined by Sun for creating dynamic content and for extending the functionality of a web server. In this paper, we will describe the foundations of these two techniques and compare some of the key features for dynamic content oriented sites: security, transaction mechanism and manageability. We will also present some various features only available in the Servlet technology and give some advice, including presentation of some tools. This paper is by no mean a guide to PL/SQL nor to Java/JDBC/Servlet programming, many sites and books are available for this purpose: for the PL/SQL cartridge, I would especially recommend PL/SQL programming (Steven Feuerstein, 1995)1; for Servlet programming, I would advise Professional Java Server Programming (Danny Ayers and al, 1999)2.
2/15
The PL/SQL Web toolkit is made of a set of packages which provide procedures and functions to generate most of the HTML tags. For example htp.headOpen would generate <HEAD> and htp.title(Hello) would generate <TITLE> Hello </TITLE>. All of these procedures write into a PL/SQL table that is transferred to the client at the end of the call. The following is a simple example of a package with a procedure that multiplies the salaries of the employees in the EMP table and then lists the employee identifiers, names and salaries.
PACKAGE basic IS PROCEDURE update_and_display(p_factor_num IN NUMBER DEFAULT 1); -- default is to not change the salaries END basic; PACKAGE BODY basic IS CURSOR cur_emp IS select empno,ename,sal from emp; PROCEDURE update_and_display(p_factor_num IN NUMBER DEFAULT 1) IS BEGIN htp.htmlOpen; -- typical HTML element htp.headOpen; htp.title('Employees'); htp.headClose; htp.bodyOpen; UPDATE emp SET sal=p_factor_num*sal; -- multiply the salaries FOR emp_rec IN cur_emp LOOP -- loop on the cursor htp.print('['||emp_rec.empno||']'||emp_rec.ename||'=>'||emp_rec.sal); htp.br; END LOOP; htp.bodyClose; htp.htmlOpen; EXCEPTION -- catch exception and WHEN OTHERS THEN -- and return an error page rollback; htp.print('Err update_and_display, '||p_factor_num); htp.bodyClose; htp.htmlClose; END update_and_display; END basic;
A call to http://host:port/basic.update_and_display?p_factor_num=1.1 would raise the salaries by 10% and lists the employees with their salaries.
3/15
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class BasicServlet extends HttpServlet { // implement the Servlet class public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // set the content as web page PrintWriter out = new PrintWriter (response.getOutputStream()); // get a writer stream out.println("<html>"); // directly write onto the stream out.println("<head><title>Title</title></head>"); out.println("<body>some text in the body"); out.println("</body></html>"); out.flush(); // flush the ouput to the browser } }
In addition to the Java2 Enterprise Edition platform facilities, Oracle provides a toolkit similar to the one used for years with PL/SQL: the idea is to have a set of classes that will generate HTML tags. The good point with this package is that one can hope that a simple upgrade and a recompilation would make your application compliant with the next version of HTML (current toolkit is HTML 3.2 compliant). Here is a simple example that uses the oracle.html classes and produces a page with a simple fixed content; note that this page is HTML 3.2 compliant (as you can check for example using http://validator.w3.org/), whereas our two previous examples are not fully compliant, as they would need a DOCTYPE that is produced by default with the Oracle classes. These oracle.html classes also help performance, as explained later.
import import import import javax.servlet.*; javax.servlet.http.*; java.io.*; oracle.html.*;
public class BasicOracle extends HttpServlet { // implement the Servlet class public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out_str = response.getOutputStream(); // retrieve an output stream out_str.println("Content-type: text/html"); // indicate the MIME type out_str.println(); HtmlHead head = new HtmlHead("Title !"); // generation of an HTML tag HtmlBody body = new HtmlBody(); HtmlPage page = new HtmlPage(head, body); // page is the head plus the body body.addItem(new SimpleItem("Some body text")); // add items to the body page.print(out_str); out_str.flush(); } } // print the page // flush output to the browser
4/15
3.2. Non-text
It also happens that one needs to send binary information across WAN such as images, sounds, videos, compressed files, etc. This has been possible as of Oracle Web Application Server version 3 thanks to the cartridge extensibility framework, but it is almost impossible with the PL/SQL cartridge as it is unpractical to handle binary data due to PL/SQL limitations. With Servlets, it is very easy, as we just need to change the way we retrieve the writer to obtain a binary-capable one and then to use the large number of available libraries to produce the binary data to be transmitted. For example, to send a simple image with a few words as a GIF, one can extend the following piece of code. It makes use of a GifEncoder, a class from ACME Laboratories 3. Note that these image manipulations are usually built with AWT (Abstract Window Toolkit), which currently needs access to a graphical display to work; it may be a problem for the Unix type servers.
res.setContentType("image/gif"); ServletOutputStream out = res.getOutputStream(); Frame frame = new Frame(); Image image = frame.createImage(400, 60); Graphics g = image.getGraphics(); g.setFont(new Font("Serif", Font.ITALIC, 48)); g.drawString("Hello world!", 10, 50); new GifEncoder(image, out).encode(); // set MIME as GIF // binary stream // create an image
// draw a string using AWT // encode the image // and send it to the stream
One can also use all of the TCP based features of Java to do what was difficult to achieve with PL/SQL. Sending mails (using for example JavaMail), accessing HTTP URLs (for example to retrieve some XSL file -Extensible Stylesheet Language, a way to transform XML documents-), JNDI services (-Java Naming and Directory Interface-, a common interface to various directories like LDAP -Lightweight Directory Access Protocol- or NDS -Netware Directory Service-), FTP (File Transfer Protocol) can be made with Java. One can even use the futurist JINI (connection technology to let devices register on the network and talk with one another) techniques.
5/15
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <! to be HTML 4.0 compliant > <HTML> <HEAD> <TITLE> A simple JSP </TITLE> </HEAD> <BODY> <H3> Welcome! </H3> <! standard tags > <P><B> Today is <%= new java.util.Date() %> <! Java scriptlet > </B></P> </BODY> </HTML>
In essence, one can see that JSP code, in the end, is no more than a Java Servlet and thus one cannot do more or better than with direct Servlet programming. JSP is a very handy way to avoid long and errorprone Java programming and to concentrate on the logic (that we advise to implement as Java Beans) and on the presentation, letting the JavaServer Page engine glue both of them with automatic generation of Java code.
4. Security
The security features are some of the most important requirements for web applications as soon as confidential data are made available on the web. Access control is also a necessity as soon as data modification is allowed through the web. In this chapter, I will describe the functionalities offered by OAS and the PL/SQL cartridge.
4.1. Confidentiality
In the OAS, all of the requests come from the listener; the various listeners that can be plugged in OAS (namely Spyglass, the default one, but also Apache and IIS) support Secure Socket Layer. As this encryption mechanism is done at the listener level, it is completely independent from the cartridges that will then process the requests and thus is the same for both PL/SQL and the Servlet cartridges. To me, an important feature is missing in the Oracle Application Server releases that we have available for production (up to 4.0.8.1): the ability to restrict an application (in the OAS sense) to a specific port. It would be very handy and secure to be able to define a SSL encrypted listener and to indicate that requests to a sensitive application have to go through the SSL port -currently requests may come to the applications via any of the listeners ports-. We have read that such an enhancement has been made in 4.0.9.
6/15
With 4.0.8.1, one can use a LDAP (Lightweight Directory Access Protocol) server, as the source of the users to define a Realm, the LDAP server can be an Oracle Internet Directory, but may also be another corporate LDAP server. As described in OAS Security Guide (Oracle Corp, 1999)4, such Access Control Lists may be used and allow much more flexibility than the previously described fixed set of users. This also avoids having to reload the configuration file at each change. The following example would define a Realm for users who are in the finance or marketing groups in myCompany in the US.
(group=ou=finance,o=myCompany,c=US) | (group=ou=marketing,o=myCompany, c=US)
4.3.2. Pitfall
The common security pitfall that I have seen in projects using the PL/SQL cartridge is due to the fact that PL/SQL procedure names are case insensitive and thus a protection on /shop/plsql/private_delete would not prevent a user from accessing /shop/plsql/PriVate_DeLeTe. To workaround this problem, I suggest to protect applications at the root level and then unprotect (indicating, for example, a restriction to all IP addresses) the public parts of the application.
5. Transaction handling
5.1. Introduction
The web as handled by the HTTP protocol is basically stateless, a request for a web page does not make reference to the previous one and a connection to the web server is created for each request (the connection can be kept with HTTP 1.1, but it does not change the underlying stateless mechanism). This has been one of the biggest problems to solve when creating a web application (or porting to the web an older application) for which the concepts of locking, transaction and session context are important. We have used these concepts with Graphical User Interface tools for years (Oracle Forms automatically puts a lock on a record when you start to edit it). But how is it possible to do the same on the web? We are now all used to the shopping basket that we can see in the electronic commerce applications. In order to build such applications, we need to tackle these different issues. We will look at a few of the facilities offered by the PL/SQL cartridge: cookies, optimistic locking and transaction handling. We will then compare them with the new facilities provided with the Servlet technology: session tracking, session and application contexts and how to handle transactions in this environment.
7/15
5.2.2. Cookies
A more sophisticated method that solves the previously mentioned problem is based on a technique called cookies; the main idea is to transmit to the browser information that can later be queried by the application. It is the basis of almost every Internet application that we currently use. The PL/SQL cartridge can handle cookies in an easy way thanks to the OWA_COOKIE package. The storage and handling of information has to be handled in a programmatic way.
8/15
9/15
This technology resolves big problems that would have had to be solved programmatically otherwise. But the impossibility to join a transaction (for example in the case where the begin transaction has not been called and thus the transaction is not initiated) has to be programmatically managed and there is no documentation on how to do it with the current API.
5.3.3. JDBC/JTS
Java DataBase Connectivity is the way to access databases from Servlets; Oracle JDBC drivers provide extended facilities like LOB access, batch loads, and calls to PL/SQL stored objects. One of these extended functionalities is the compliance with the Java Transaction Service. It uses the same transaction service as the PL/SQL cartridge but in a programmatic way. With the JTS API, one needs to connect to the resource manager, and then to connect to the database using a special JDBC driver, to do some operations on the connection (being direct JDBC calls or calls to PL/SQL stored units) and finally to commit on the resource manager. The advantage over the PL/SQL access to the transaction service is that it is a programmatic approach and thus allows easy catching of problems linked with the resource manager transaction.
6. Manageability
When it comes to developing and maintaining large applications, the amount of effort dedicated to plan and document how the application will grow is essential. Some techniques can be used to help the teams in this work: N-accounts architecture, API based interfaces, packaging and coding convention. These techniques can be implemented in different ways depending on the choices made for the language and the architecture.
6.1. Packages
In large applications with tens or hundreds of different actions, it is very important to define a hierarchy of sub-programs which can be developed by different persons or teams. Both PL/SQL and Java provide ways to define packages: in Java, they are simply the classes themselves, in PL/SQL, they are defined as packages; one can have both public and private methods in Java and with PL/SQL. To call these packages with the PL/SQL cartridge, one has no choice but to call them in the URL. A fully dynamic alias mechanism could be implemented using the dynamic SQL facilities (DBMS_SQL in Oracle7 and execute immediate in Oracle8i), but this would slow down the processing of the requests and reduce the benefits of dependencies. With the Servlet system, the idea suggested in Developing Scalable, Reliable, Business Applications with Servlets (Oz Lubling and Leonardo Malave, 1999)6 is to call a main Servlet and pass an argument to it that will help to redirect the call to another class. This mechanism can be used to alias and cache the complexity of the design into simple names of modules. The concept is to have a main entry point that will then dynamically instantiate the requested module and pass to it the different arguments (authentication, values given by the user in a form, request and response). URLs have to be sent in a form like http://host:port/servlet/MyServlet?module=mymodule&.... The main Servlet has to implement the doGet and doPost methods, all of the modules have to inherit from the same class (called mModule in the example) which has a method to receive the arguments and another to process and create the output. Here is a skeleton of the technique.
10/15
Class c = Class.forName( "mypackage." + (String)appModules.get(module) ); mm = (mModule) c.newInstance(); // instantiate an object of the requested module } catch (IllegalArgumentException iaex) { printError(res, "Module not found"); return; } mm.setParams(request, response, user_id, database_connection, log_file); // pass request parameters to the appropriate module mm.process(); // and run the request
11/15
http://alephwww.cern.ch/scanbook/scanbook.html, the PL/SQL cartridge has been used to produce nonHTML results that are transmitted to a Java application (or an applet) which handles the user interface. As Servlets generate any text just like PL/SQL cartridge does, the methods mentioned before can be used. But Java also enables more complex architectures; for example, Enterprise Java Beans (a technique based on Remote Method Invocation part of the Java2 Enterprise Edition platform) can be used to implement the business logic while having the presentation layer based on Servlets. Oracle is providing Business Component for Java (BC4J) to simplify building reusable server-side modules with JDeveloper 3.0; Building Java applications for the Oracle Internet platform with Oracle JDeveloper (Oracle Corporation, 1999)10 is a good introduction to this technique. This tool allows rapid development of database oriented business logic modules which can then be deployed as Enterprise Java Beans or CORBA distributed objects.
6.6. Tools
Availability of tools has an impact on the choice of one or another technology and is essential to the success of a project. Several types of tools can be seen as important for a technology: syntax highlighting, debugger, impact analysis study, version management, migration assistants, automatic generation wizards, and associated case tools PL/SQL has always been a language with very few tools to help developing and debugging. PL/SQL dependencies help developers to have an idea of the impact of the changes they make on tables or stored objects. Tools are now coming with several debuggers; browsing tools, high end Integrated Development
12/15
Environments. Development of non-windows based tools is on going, some of them being in the Open Source movement. Oracle has published the non-supported WebAlchemy tool that helps to migrate static web pages to PL/SQL stored units. Unfortunately the Oracle tool to help debugging PL/SQL cartridge application, see http://www.olab.com/, is no longer accessible. For Java, lots of tools are available and many of them include support for Servlets. Oracle is providing JDeveloper that has some database-oriented features, it includes a local Servlet server that can be used in the debugging environment.
7. Performance
Sooner or later, one has to cope with the performance problem. Some projects target performance in the early phases and make choices accordingly. Other, often smaller, projects look at tuning when userwaiting times become unacceptable.
13/15
8.1.1. Simpler
Servlet programming is much more complex than with the PL/SQL cartridge; in particular, PL/SQL is much better integrated with the Oracle database than Java binding (JDBC): just figure out the number of lines to open a cursor and loop around it with both technologies, including exception handling. Database connection is the basis for the PL/SQL cartridge as code gets executed in the database, whereas the programmer has to take care of database connection in the Java Servlet model. For example, even if the connection has been opened in the init procedure, one always needs to check (trapping SQLException) that the connection is still valid and working, reopening it if necessary.
14/15
Oracle PL/SQL Programming by Steven Feuerstein, 1995, OReilly and Associates, ISBN 1-56592-142-9 Professional Java Server Programming by Danny Ayers and al, 1999, Wrox Press, ISBN 1-861002-77-7 GifEncoder, ACME Laboratories http://www.acme.com/java/software/ Oracle Application Server 4.0.8.1 Security Guide by Oracle Corporation, 1999 mod_perl Coding Guidelines by Stas Bekman, 2000, http://www.perlmonth.com/columns/perl_apache/perl_apache.html?issue=10 Developing Scalable, Reliable, Business Applications with Servlets by Oz Lubling and Leonardo Malave, http://developer.java.sun.com/developer/technicalArticles/Servlets/Razor/index.html Code Conventions for the JavaTM Programming Language by Sun Microsystems, 1999, http://java.sun.com/docs/codeconv/index.html Oracle Application Server 4.0.8.1 Developers Guide: PL/SQL and ODBC Applications by Oracle Corporation, 1999 Oracle PL/SQL Tips and Techniques by Joseph C. Trezzo, 1999, Osborne McGraw-Hill; ISBN 0-07882-438-9 Building java applications for the Oracle internet platform with Oracle JDeveloper, an Oracle white paper, 1999, http://technet.oracle.com/products/jdev/htdocs/jds_oip.htm Oracle Performance Tuning, by Richard J. Niemic, 1999, Oracle Press, ISBN: 0-07-882434-6 Java or PL/SQL? (French and German), by Guido Schmutz Trivadis SA, 2000, http://www.trivadis.ch/ StringBuffer versus String, by Reggie Hutcherson, 2000, http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html
3 4 5
10
11 12 13