A database allows the storage of data in an orderly
fashion that allows information to be retrieved from it. The easiest and now
standard way of retrieving data is by using Structured Query Language (SQL),
the scope of which is well beyond this report (see Database Systems). SQL
allows databases to be queried, updated and edited relatively simply,
however it can be used to design very sophisticated queries and updates.
The following sections discuss the three most common ways
of accessing databases across the Internet.
2.3.3.1 Common Gateway Interface (CGI)
The Common
Gateway Interface defines how scripts communicate with Web servers’. A CGI
script will receive data and return it in a CGI compliant manner. CGI
scripts are non-interactive, they are initiated and run to completion,
retrieve their results and return the results to the user. This is currently
the most widely used method for interfacing Web applications to data
sources. 2.3.3.2 Active Server Pages (ASP)
{Internet1} Active Server pages are a server-side scripting
technology for building web pages that are both dynamic and interactive. An
active server page is a text file with the extension .asp, this file
contains HTML, client- and server-side script.
The implementation of ASP was created by Microsoft and is intended as an
open technology server-side framework, allowing developers to create
websites from the many Component Object Model(COM) data sources available to
them, such as Microsoft Access.
ASP is
designed to be easy to understand and program, yet support interaction
between page user and server, allow access to databases and directory
services via web pages, and allow the use of powerful COM components. It is
server-based and browser independent, leaving only the questions of which
client-side script (JavaScript, VBScript or JScript) and stylesheet to use.
2.3.3.3 Java Java is one of the newest and
rapidly developing programming languages. The World Wide Web gives the ideal
platform for Java to create interactive applications; these are no longer
just scripts running on the Web server. They are fully interactive and allow
for all the functionality of an application programmed in any of the other
major programming language (e.g. Visual Basic or C++), but can be downloaded
and are portable. Any application written in Java will run on all major
platforms with a Java-based browser, different versions are no longer
required for different machines.
These applications can be written and embedded into web
pages (these applications are known as applets). These programs are then
interactive; they can respond to user input, mouse movement, etc. this is an
executable program, not just a script.
The virtual machine is what makes Java so portable, where
other programming languages would compile source code into machine code
(platform and architecture dependant), Java compiles into bytecodes.
Bytecodes can be read by any Java Virtual Machine, but machine code is very
much dependant on the computer, it’s operating system and architecture. The
Java Virtual Machine runs as part of any Java enabled Web browser and
converts bytecodes into a language that the machine can understand, knows as
“write once, and run anywhere”. Java also has it’s own memory management
techniques which make it useful when downloading code from a server and
running on a client machine.
JDBC {Internet2} JDBC (which is a trademark,
not an acronym; although often thought of as “Java Database Connectivity”)
is a set of Java classes and interfaces for executing SQL statements, which
provide developers with a standard method for connecting to databases. With
JDBC database commands can be sent to any relational database (One program
will access databases from various different vendors). Java’s ability to
automatically download over a network makes it ideal for network database
applications with no software other than a web browser (mostly coming as
standard with the operating system) being required on the client-side.
Java’s property of write once, run anywhere allows many machines of
different types (Macintosh, PC’s running Microsoft Windows or Unix) to
connect to the same database across any network, either the Internet or an
intranet.
In it’s simplest form JDBC does three things.
- Connects to a database.
- Sends an SQL Statement.
- Processes the results.
Below is a brief code fragment which demonstrates this.
Connection con = DriverManager.getConnection ("jdbc:odbc:wombat",
"login", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next())
{
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
Using JDBC with the JDBC-ODBC Bridge, instead of using ODBC directly (which involves installing drivers on every machine)
provides an automatically installable, portable and secure client-server
database platform.
The JDBC API supports both two-tier and three-tier models for database access.
Figure 2.1 - A two-tier architecture using JDBC
A two-tier architecture allows an Application or Applet to access a
database directly. This requires a specific driver for the database in
question to be installed on the client machine and then the server can be
access across a company intranet or the Internet.
Figure
2.2 - A three-tier architecture using JDBC.
A three-tier architecture (Figure 2.2) allows commands to be sent to a middle tier, which
then makes the connection and SQL calls to the database. This approach is
particularly appealing to programmers because it allows updates to be made
to the server without it affecting the client applet (so long as the
function calls remain the same).
Remote Method Invocation (RMI){Internet3}
Remote Method Invocation allows distributed applications that are
Java-to-Java to be created. By using this method it is possible to create
remote objects, these object methods can then be invoked from another Java
virtual machine (whether it be on the same host or a different one). Java
programs can make a call on a remote object by looking it up in the RMI
Registry or by receiving it as an argument or return value.
It is possible for a client to call a remote object on the server, and
that server also to be a client of other remote objects, making calls to
them.
For RMI to work the interface of the remote object must be available on
the server and the implementation of the object should have registered
itself with the already running RMI Registry. The remote object should have
installed the RMI security manager and be running. The client side applet
can then access all methods of the remote object once a reference has been
created.
Figure 2.3 - Client-Server Environment
Below are examples of the interface, implementation and client for a
simple “Hello World” program using RMI.
Below are examples of the interface, implementation and client for a simple “Hello World” program using RMI.
Interface
package examples.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws RemoteException;
}
Implementation
package examples.hello;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello
{
public HelloImpl() throws RemoteException
{
super();
}
public String sayHello()
{
return "Hello World!";
}
public static void main(String args[])
{
if (System.getSecurityManager()
== null)
{
System.setSecurityManager(new RMISecurityManager());
}
try
{
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name "HelloServer"
Naming.rebind("HelloServer", obj); System.out.println("HelloServer bound in registry");
} catch (Exception e)
{
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
Client
package examples.hello;
import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class
HelloApplet extends Applet {
String message = "blank";
// "obj" is the identifier that we'll use to refer
// to the remote object that implements the "Hello"
// interface
Hello obj = null;
public void init() {
try {
obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer");
message = obj.sayHello();
} catch (Exception e) {
System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawString(message, 25, 50);
}
}
The remote object as well as being compiled with the Java Compiler (javac.exe) must be
compiled with the RMI tool (rmic.exe). This produces a stub and a skeleton,
which are required when using RMI.
Figure 2.4 - RMI Architecture1
As shown in figure 2.4, an application is initialised on
the Server (Virtual Machine A), this is known as the remote object. The
remote object registers itself with the RMI Registry (another Java
application that stores references to remote objects).
When the client (on Virtual Machine B) tries to reference
a method within the remote object, it accesses the RMI Registry (on the
Server) through a TCP socket connection. When the registry confirms that the
method is valid it will return the remote object to the client in the form
of a stub.
The client now invokes a method that is contained within
the remote object’s stub, this can involve passing of parameters and return
values. Parameters are forwarded to the remote object through a skeleton
object. When the remote object completes the method, any necessary return
values are sent back through the skeleton and stub objects and received by
the client. |