Oracle9i XML Database Developer's Guide - Oracle XML DB Release 2 (9.2) Part Number A96620-02 |
|
|
View PDF |
This chapter describes how to write Oracle XMl DB applications in Java. It includes design guidelines for writing Java applications including servlets, and how to configure the Oracle XML DB servlets.
It contains these sections:
Oracle XML DB provides two main architectures for the Java programmer:
Because Java in the database runs in the context of the database server process, the methods of deploying your Java code are restricted to one of the following ways:
Stored procedures are easier to integrate with SQL and PL/SQL code, and require using Oracle Net Services as the protocol to access Oracle9i database.
Servlets work better as the top level entry point into Oracle9i database, and require using HTTP as the protocol to access Oracle9i database.
In this release, some of Oracle XML DB APIs are only available to applications running in the server. Table 20-1 illustrates which Oracle XML DB APIs are available in each architecture in this release. The "NO" fields will become "YES" in a forthcoming release.
Oracle XML DB APIs AVailable Inside and Outside Oracle9i Database
When choosing an architecture for writing Java Oracle XML DB applications, consider the following guidelines:
If the downstream client wants to deal with XML in its textual representation, using HTTP to either access the Java servlets or directly access XMLType
resources, will perform the best, especially if the XML node tree is not being manipulated much by the Java program.
The Java implementation in the server can natively move data from the database to the network without converting character data through UCS-2 Unicode (which is required by Java strings), and in many cases copies data directly from the database buffer cache to the HTTP connection. There is no need to convert data from the buffer cache into the SQL serialization format used by Oracle Net Services, move it to the JDBC client, and then convert to XML. The load-on-demand and LRU cache for XMLType
are most effective inside the database server.
If the downstream client is an application that will programmatically access many or most of the elements of an XMLType object using Java, using JDBC XMLType
support will probably perform the best. It is often easier to debug Java programs outside of the database server, as well.
Oracle XML DB servlets are intended for writing "HTTP stored procedures" in Java that can be accessed using HTTP. They are not intended as a platform for developing an entire Internet application. In that case, the application servlet should be deployed in Oracle9iAS application server and access data in the database either using JDBC, or by using the java.net.* or similar APIs to get XML data through HTTP.
They are best used for applications that want to get into the database, manipulate the data, and write it out quickly as XML, not to format HTML pages for end-users.
Oracle XML DB provides a Protocol Server that supports FTP, HTTP 1.1, WebDAV, and Java Servlets. The support for Java Servlets in this release is not complete, and provides a subset designed for easy migration to full compliance in a following release. Currently, Oracle XML DB supports Java Servlet version 2.2, with the following exceptions:
web.xml
) is not supported in its entirety. Some web.xml
configuration parameters must be handled manually. For example, creating roles must be done using the SQL CREATE ROLE
command.RequestDispatcher
and associated methods are not supported.HTTPServletRequest.getCookies()
method is not supported.ServletContext
(and one web-app) is currently supported.HttpSession
class methods) are not supported. Servlets must maintain state in the database itself.Oracle XML DB servlets are configured using the /xdbconfig.xml
file in the Repository. Many of the XML elements in this file are the same as those defined by the Java Servlet 2.2 specification portion of Java 2 Enterprise Edition (J2EE), and have the same semantics. Table 20-2 lists the XML elements defined for the servlet deployment descriptor by the Java Servlet specification, along with extension elements supported by Oracle XML DB.
See Also:
Appendix A, "Installing and Configuring Oracle XML DB" for more information about configuring the |
Oracle XML DB handles an HTTP request using the following steps:
xdbconfig.xml
file, as specified by the Java Servlet 2.2 specification.ServletInputStream
, and writes output to the ServletOutputStream
, and returns from the service()
method.The Oracle database keeps one Java VM for each database session. This means that a session reused from the session pool will have any state in the Java VM (Java static variables) from the last time the session was used.
This can be useful in caching Java state that is not user-specific, such as, metadata, but DO NOT STORE SECURE USER DATA IN JAVA STATIC MEMORY! This could turn into a security hole inadvertently introduced by your application if you are not careful.
The DOM
Node class has an Oracle-specific method called write()
, which takes the following arguments, returning void:
java.io.OutputStream
stream: A Java stream to write the XML text tocharEncoding
: The character encoding to write the XML text in. If null, the database character set is usedShort
indent
The number of characters to indent nested XML elementsThis method has a shortcut implementation if the stream provided is the ServletOutputStream
provided inside the database. The contents of the Node are written in XML in native code directly to the output socket. This bypasses any conversions into and out of Java objects or Unicode (required for Java strings) and provides very high performance.
The APIs supported by Oracle XML DB servlets are defined by the Java Servlet 2.2 specification, the Javadoc
for which is available, as of the time of writing this, online at: http://java.sun.com/products/servlet/2.2/javadoc/index.html
Table 20-3 lists non-implemented Java Servlet 2.2 methods. In this release they result in runtime exceptions.
Interface | Methods |
---|---|
HttpServletRequest |
getSession(), isRequestedSessionIdValid() |
HttpSession |
ALL |
HttpSessionBindingListener |
ALL |
The following is a simple servlet example that reads a parameter specified in a URL as a path name, and writes out the content of that XML document to the output stream.
The servlet code looks like:
/* test.java */ import javax.servlet.http.*; import javax.servlet.*; import java.util.*; import java.io.*; import javax.naming.*; import oracle.xdb.dom.*; public class test extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { OutputStream os = resp.getOutputStream(); Hashtable env = new Hashtable(); XDBDocument xt; try { env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.xdb.spi.XDBContextFactory"); Context ctx = new InitialContext(env); String [] docarr = req.getParameterValues("doc"); String doc; if (docarr == null || docarr.length == 0) doc = "/foo.txt"; else doc = docarr[0]; xt = (XDBDocument)ctx.lookup(doc); resp.setContentType("text/xml"); xt.write(os, "ISO8859", (short)2); } catch (javax.naming.NamingException e) { resp.sendError(404, "Got exception: " + e); } finally { os.close(); } } }
To install this servlet, compile it, and load it into Oracle9i database using commands such as:
% loadjava -grant public -u scott/tiger -r test.class
To configure Oracle XML DB servlet, update the /xdbconfig.xml
file by inserting the following XML element tree in the <servlet-list>
element:
<servlet> <servlet-name>TestServlet</servlet-name> <servlet-language>Java</servlet-language> <display-name>XML DB Test Servlet</display-name> <servlet-class>test</servlet-class> <servlet-schema>scott</servlet-schema> </servlet>
and update the /xdbconfig.xml
file by inserting the following XML element tree in the <servlet-mappings>
element:
<servlet-mapping> <servlet-pattern>/testserv</servlet-pattern> <servlet-name>TestServlet</servlet-name> </servlet-mapping>
You can edit the /xdbconfig.xml
file with any WebDAV-capable text editor, or by using the updateXML()
SQL operator.
To test the example servlet, load an arbitrary XML file at /foo.xml
, and type the following URL into your browser, replacing the hostname
and port number as appropriate:
http://hostname:8080/testserv?doc=/foo.xml