|< Free Open Study >|| |
At one stage or another, when working with servlets in the enterprise, we will need to work with persistent data from an external data source. The persistent data stores most relevant to the servlet environment are databases, naming and directory services and XML. For example:
When building a web site, we might want to store the content of each web page in a relational database, such as SQL Server or Oracle
Some parts of our site may be restricted to authorized users, in which case we might need to provide a means to verify user credentials against an LDAP directory service
Our web site might need to access and display data from a third party, such as a weather forecast or stock information, which will probably be made accessible using XML
We can access a relational database in Java using the JDBC programming interface. The JDBC API consists of abstract classes and interfaces that are implemented by database vendors.
JDBC provides developers with a standardized way of accessing and manipulating data.
The API is used to represent physical database connections and statements; actual database operations are expressed using Structured Query Language (SQL).
More information on JDBC can be found in Beginning Java Databases (ISBN 1-86100-437-0) and Professional Java Data (ISBN 1-86100-410-9). Sun also provides a JDBC tutorial at http://java.sun.com/products/jdbc/learning.html.
The examples in this chapter will demonstrate of the uses of JDBC in a servlet scenario. Although all the examples will be general enough to be used with any relational database, we will demonstrate them using MySQL (available from http://www.mysql.com).
A naming service constructs a unique name (using a standard format) for any given set of data. An internet domain name, such as www.apress.com, and an e-mail address, such as <email@example.com>, are examples of unique names managed by a naming service.
A naming service is often used by a directory service, which is a special type of service used for optimizing read access to an underlying data store. A directory service is used to look up data entries by a given name or criteria, using a standard set of API calls. A telephone book is an example of a real-life directory service that we use to look up phone numbers by a person's name.
Unlike a relational database management system (RDBMS), which can relate and query any number of database objects, a directory service can only work with highly specific lookup operations, which are specified by the API. For example, a naming service that stores user information can generally only be used to issue queries based on the predefined attributes of the user. For example, "give me the full name of the user with the login name john". If this information was stored in a relational database, we could issue any type of query (so long as it conforms to the semantics of SQL). For example, "give me the number of users whose names begin with j and who have less than $3000 in monthly wages".
In addition, unlike most relational databases, a directory service does not support transactions. However, the advantage of a directory service is that it is generally faster than a relational database at specific lookup operations, as it is optimized for that type of access.
Just as there are different relational database systems available, such as SQL Server and Oracle, there are many different types of directory service protocols. One of the most common is the Lightweight Directory Access Protocol (LDAP). Similarly, just as JDBC provides a uniform interface to data, we can use the Java Naming and Directory Interface (JNDI) API to access the various types of directory service protocols.
Each J2EE-compliant application server provides a default JNDI provider implementation that can be used to lookup and bind data sources and other types of objects.
More information about LDAP can be found in Professional Java Data (ISBN 1-861-00410-9), or Implementing LDAP (ISBN 1-861-00221-1).
As a language of text-based data structures Extensible Markup Language (XML) is ideal for storing servlet (and web application) configuration information. It is widely accepted as a standard for document storage and a number of APIs are available for parsing XML content.
Although XML is listed here alongside databases and directory services, it is merely a language for structuring data, and not a persistent data store in the sense that a relational database is. XML can be stored in virtually any type of data source, although flat text files and relational database tables are the two most common.
More information on XML can be found in Beginning XML (ISBN 1-861-00341-2) and Professional Java XML (ISBN 1-861-00401-X).
|< Free Open Study >|| |