ACET




REST tutorial

Setting up the environment

Create a working directory

If you haven’t already done so, you may wish to create a separate working directory in which to store all of the files for this tutorial.

Java

You should already have a Java Development Kit (JDK) installed. Ensure that you have JAVA_HOME set correctly, and check that you can run the compiler:

javac -version

Tomcat

Apache Tomcat is a Java Servlet container implementation. It acts as a web server, allowing requests for resources on that web server to be directed to specific pieces of Java code. Whilst it is powerful and complex, for small applications like the one in this tutorial, it is fairly straightforward to install, configure and run.

Download Tomcat:

Unpack it:

(In Linux) tar -xzf apache-tomcat-6.0.18.tar.gz

(In Windows) Open the zip archive, and copy the directory called apache-tomcat-6.0.18 into your working directory.

Set CATALINA_HOME:

(In Linux) Open up a terminal, and type export CATALINA_HOME=/path/to/apache-tomcat-6.0.18

(In Windows) Open up a command window (Start→Run..., cmd) and type set CATALINA_HOME=C:\path\to\apache-tomcat-6.0.18

Update your $CATALINA_HOME/conf/tomcat-users.xml file, so that you can use the Tomcat manager:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <user username="tomcat" password="tomcat" roles="manager"/>
</tomcat-users>

Start Tomcat:

(In Linux) $CATALINA_HOME/bin/startup.sh

(In Windows) %CATALINA_HOME%\bin\startup.bat

Test Tomcat by visiting http://localhost:8080/. You should see an “Apache Tomcat” web page, congratulating you on setting up Tomcat correctly.

Hypersonic database

We will also need some form of data store in which to keep the dynamic data for our application. Hypersonic SQL (HSQL) is a pure Java SQL database library, which can be run in stand-alone form, or embedded into an application. We will be using it in the latter form in this tutorial, as it involves the least amount of work.

Download Hypersonic.

Unpack it:

(In Linux) unzip hsqldb_1_8_0_10.zip (In Windows) Open the zip archive, and copy the hsqldb directory into your working directory.

Setting up a database

We need to configure a database for Hypersonic, create a couple of tables (one for the auction items in our application, and one for the bids), and a test item.

First, set up a database in Hypersonic. Create a file, hsql.rc, containing the following text:

urlid auction
url jdbc:hsqldb:file:/path/to/tutorial/auction;shutdown=true
username sa
password

Then run the Hypersonic SQL tool, and create a new database, some tables, and a couple of items:

java -cp hsqldb/lib/hsqldb.jar org.hsqldb.util.SqlTool --rcFile hsql.rc auction
sql> create table item (id integer identity, 
     title varchar,    
     description varchar,
     reserve double,
     expiry datetime);
sql> create table bid (id integer identity, 
     user varchar,
     itemid integer,
     value double,
     timestamp datetime);
sql> insert into item (title, description, reserve, expiry)
     values ('Combine harvester',
     'Unused, as new, complete with key. Unwanted present.',
     200.00,
     '2009-03-27 15:03');
sql> insert into item (title, description, reserve, expiry)
     values ('Bike',
     'Has a basket, a bell that rings and things to make it look good.',
     15.00,
     '2009-03-27 15:09');
sql> commit;
sql> \q

You should now be able to see a file called auction.script in your working directory. This is the Hypersonic SQL database (in this case, it simply contains a log of the SQL commands needed to generate the database in memory).

Now move on to the next section of the tutorial to start creating the servlet.

Valid XHTML | Copyright | Last Modified: 1/Apr/2009 |