Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Publish dynamic XML content with Cocoon 2, Part 3: Cocoon 2: Build database-driven sites

Generate and publish XML and HTML content based on database data

Leigh Dodds (leigh@xmlhack.com), Developer and editor, Ingenta, Ltd.
Photo of Leigh Dodds
Leigh Dodds is team leader of the Research and Technology Group at Ingenta, Ltd. He has five years of experience in developing on the Java platform, and he has spent the last three years working with XML and related technologies. Leigh is also a contributing editor to xmlhack.com, and has been writing the regular "XML-Deviant" column on XML.com since February 2000. He holds a bachelor's degree in biological science, and a master's in computing. When he's not wrestling with pointy brackets, Leigh can be found making silly noises with his son, Ethan.

Summary:  The first two tutorials in this series introduced the basic concepts of the Cocoon 2 architecture and XML Server Pages (XSP) as a means for creating and publishing dynamic XML content. This tutorial goes a natural step further and discusses how to use Cocoon 2 to generate and publish XML and HTML content based on database data. Any Web site that has reached a certain size and complexity will require a database to manage at least part, if not all, of its data.

View more content in this series

Date:  30 May 2002
Level:  Introductory PDF:  A4 and Letter (116 KB | 30 pages)Get Adobe® Reader®

Activity:  4156 views
Comments:  

About this tutorial

Should I take this tutorial?

This tutorial assumes that you are familiar with the Cocoon 2 application and with XML Server Pages (XSP). The previous two tutorials in this series, "Introducing Cocoon 2" and "Working with XML Server Pages", cover the relevant background material (see Resources).

The tutorial is for Java developers who have previous experience working with databases in a Java environment and wish to learn how to apply that knowledge to building Cocoon 2 applications. You should at least be familiar with JDBC or SQL although no detailed knowledge is required. If you need a refresher, check Resources for links to other tutorials that cover these technologies.

In this tutorial, you learn to:

  • Configure and manage JDBC connection pools for use in Cocoon 2 applications
  • Select data from databases and publish it as XML using XML Server Pages
  • Insert, update, and delete data
  • Configure Cocoon 2 to carry out form validation on user-submitted data

Databases and Cocoon 2

The first two tutorials in this series introduced the basic concepts of the Cocoon 2 architecture and XML Server Pages (XSP) as a means for creating and publishing dynamic XML content. This tutorial goes a natural step further and discusses how to use Cocoon 2 to generate and publish XML and HTML content based on database data. Any Web site that has reached a certain size and complexity will require a database to manage at least part, if not all, of its data.

Java frameworks that provide database access generally include the following basic features:

  • Connection pooling for managing connections to the database
  • A database abstraction layer constructed around the JDBC API
  • Form handling functionality to provide validation and feedback for user-submitted data

Each of these features is supported by Cocoon 2 and will be discussed in this tutorial. Because Cocoon 2 is an XML framework, this functionality can also be used to expose legacy data for application integration purposes as well as direct Web site publishing.

While Cocoon 2 introduces these database integration features, it was not designed as a complete Java application server framework. Cocoon 2 does not compete with servers based around the J2EE architecture as it doesn't provide the same level of resource and transaction management, load-balancing, and deployment options. Cocoon 2 is most suitable for applications that involve little sophisticated business logic and in which processing is mostly aimed at content generation.

However, you can still use Cocoon 2 in conjunction with an EJB server. Cocoon 2 can easily act as a replacement presentation layer rather than the more commonly used Java Servlets and Java Server Pages. In short, Cocoon 2 aims to meet the 80/20 rule by delivering the most flexibility from a small number of features.


Tools

The examples in this tutorial have been created and tested using Cocoon 2.0.2. It is assumed that the user has already successfully installed Cocoon as detailed in the "Introducing Cocoon 2" tutorial. (See Resources). Download the example code for this tutorial in dbc2.tar, the sample archive file.

To get the most out of this tutorial, you should have access to a relational database, either an open source database such as MySQL, or a commercial product such as DB2. See http://mysql.com for download information and technical documentation on MySQL. See http://www.ibm.com/software/data/db2/ for product information related to IBM's DB2 database. This tutorial does not cover the set-up and installation of a database environment.

You should have a suitable Java JDBC driver for your database. See Resources for links to Web sites from which you can obtain JDBC drivers. Drivers are generally packaged as part of a database distribution.

Throughout the tutorial, two environment variables are frequently referenced. These are defined as follows:

  • $TOMCAT_HOME -- the directory in which the Tomcat application server has been installed (for example, /usr/local/jakarta-tomcat-4.0.1)
  • $COCOON_HOME -- the Cocoon Web application directory; this is typically $TOMCAT_HOME/webapps/cocoon

The sample application


Figure 1. Diagram of sample database schema
Diagram of sample database schema

The examples in this tutorial are all based on a very basic sample application. The sample application has been kept intentionally simple to not obscure the technical details. The tutorial's emphasis is on the technical aspects of the database integration; therefore basic steps such as using XSP pages or configuring pipelines is not covered.

The demonstration application is a music CD catalog capable of storing lists of CDs categorized by musical genre. This basic functionality could be used as the core of an e-commerce system that sells music across the Web, or as a community Web site that allows fans to submit music reviews.

The schema for this simple database contains only two tables whose relationship is shown in the diagram on the left. A category has a unique identifier (cat_id), and a human-readable name.

An album has the following properties:

  • A numeric identifier (alb_id)
  • An associated category (cat_id)
  • A title
  • An artist/group name (artist)
  • A date recording when it was added to the catalog (submitdate)
  • A count of the number of tracks on the album (num_tracks)

Many other kinds of information could be recorded, but these simple properties are sufficient to demonstrate much of the Cocoon 2 functionality.


Installing the samples

The sample archive, which can be accessed from Download, includes an SQL script to generate the database schema and populate it with some sample data. To install the samples, do the following steps:

  1. Shutdown Tomcat.
  2. Unzip the sample archive into $TOMCAT_HOME/webapps, preserving the directory structure. This creates a dbc2 directory that contains a Cocoon 2 sitemap and the samples.
  3. Copy over the $COCOON_HOME/WEB-INF into the newly created $TOMCAT_HOME/webapps/dbc2 directory. This creates a second instance of the Cocoon 2 Web application for running the samples.
  4. Run the $TOMCAT_HOME/webapps/dbc2/dbc2.sql script in your database environment to create the sample data.
  5. Follow the instructions in the next section, Configuring database connections , to configure a database connection pool.
  6. Once this is configured correctly, you can access the samples from http://localhost:8080/cocoon/dbc2/index.html.

1 of 11 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=138221
TutorialTitle=Publish dynamic XML content with Cocoon 2, Part 3: Cocoon 2: Build database-driven sites
publish-date=05302002
author1-email=leigh@xmlhack.com
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.