Before you start
This is the first part in a two-part tutorial series that demonstrates how easy it is to embed Derby as a full-featured relational database into a Web application server. In this first tutorial, you examine one way to set up Derby within your application server and, subsequently, how you can use Derby to provide a back-end data store for dynamic content within an application. For the example in this series, you're going to develop a simple Web-based wiki. To follow along with this first tutorial, you should feel comfortable working with simple servlets and have a basic understanding of Java™ Database Connectivity (JDBC). You use Apache Ant to build the application, so being comfortable running an Ant build file won't hurt either. This tutorial also covers some basics about regular expressions, which you use to parse and display wiki text in a browser.
With the recent contribution made by IBM to the open source community -- donating the source code for their Cloudscape database to the Apache Foundation (who renamed the open-sourced version to Derby) -- you now have yet another great open source database available to use within your applications. One of the nice features of Derby is that being, in essence, a baby brother to IBM DB2®, it provides users with a great stepping stone. Should you need or want to start your project on a smaller budget with a slightly less-powerful database, you can use Derby for your initial development, and then later you can make a virtually seamless move to a commercial alternative, such as DB2 (see Resources for articles on migrating Derby to DB2).
In this tutorial, you'll see how easy it is to embed Derby into a Web application, which can then be used to provide data and dynamic content for that application. Other items that this tutorial covers include the history and definitions of the wiki and a description of working with regular expressions. This tutorial is structured as follows:
- What's a wiki? -- Briefly discusses the history of the wiki, what a wiki is, and the intended purpose of the wiki concept.
- Set up the system -- Provides an overview of how to set up the application and how to embed Derby for use within the application. Then you walk through the build process and get the wiki up and running.
- Create a user account -- Demonstrates how to use the wiki pages to create a wiki user account.
- Create pages -- Shows you how to create pages in the wiki and demonstrates the wiki syntax that is supported by the wiki implementation.
- Use regular expressions -- Details some of the regular expressions used within the application and how to use the available Java APIs to do something useful with the regular expressions.
- Understand regular expressions -- Discusses more about how to learn regular expressions.
This tutorial assumes that you have a basic knowledge of the Java Servlet API and JDBC.
To run the example code in this tutorial, perform the following steps:
- Download and install the following applications
- Make sure the environmental variables outlined in Table 1 are defined in your shell.
Table 1. Environmental variables
| Variable name | Required setting |
|---|---|
| TOMCAT_HOME | Set to the root folder of your Tomcat installation |
| DERBY_HOME | Set to the root folder of your Derby installation |
| ANT_HOME | Set to the root folder of your Ant installation |
| JAVA_HOME | Set to the root folder of your Java installation |
| PATH | Ensure that ANT_HOME/bin is in your PATH |
- Extract the supplied ZIP file (see the Download section for the file) to your preferred location (this will be the project root).
The project is laid out as follows:
/Wiki/ /conf/ > contains DDL (data definition language) /deploy/ > created by ANT (will contain WAR file) /src/ > contains Java source code /web/ > contains JSPs and web.xml, etc. /build.xml > ANT build file
- The last thing you need to do is modify the /web/WEB-INF/web.xml file to specify where you want Derby to create your wiki database. In my case, I've set the
derby.system.homeproperty in the web.xml file to beW:\Development\WikiDB. See the following example taken from the web.xml file:<servlet> <servlet-class>net.humandoing.wiki.WikiServlet</servlet-class> <servlet-name>WikiServlet</servlet-name> <init-param> <param-name>derby.system.home</param-name> <param-value>W:\Development\WikiDB</param-value> </init-param> </servlet>
Note: Make sure that the value you specify is a directory and that it already exists. Second, you don't need to put a trailing slash. Third, if you are on a *nix-based system, your path would look something like /development/WikiDB, but you can create this directory wherever you want to.
If you don't specify a value or a valid value for this property, the application creates the wiki database under the Tomcat directory webapps/wiki/WEB-INF/WikiDB. This is OK, but it means that if you redeploy the wiki, any data that you entered into your wiki will be gone.

