Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

Mastering Apache Velocity

Mastering Apache Velocity

by Joseph D. Gradecki, Jim Cole

A comprehensive tutorial on how to use the power of Velocity 1.3 to build Web sites and generate content

Designed to work hand-in-hand with Apache Turbine, Struts, and servlets, Velocity is a powerful template language that greatly enhances the developer's ability to customize Web sites. It separates Java code from the Web pages, making a site more maintainable.


A comprehensive tutorial on how to use the power of Velocity 1.3 to build Web sites and generate content

Designed to work hand-in-hand with Apache Turbine, Struts, and servlets, Velocity is a powerful template language that greatly enhances the developer's ability to customize Web sites. It separates Java code from the Web pages, making a site more maintainable. Because of this, it is a viable alternative to JSPs and PHP and is expected to become the standard template engine. In addition to its use with Struts and Turbine, Velocity can also be used to generate Java and XML source code, XML schemas, HTML templates, and SQL code.

Even with all its promise, finding expert instructions on how to properly program with this language has been difficult. This code-intensive tutorial gives you all the tools you'll need.

It begins by quickly bringing you up to speed on all of the Velocity fundamentals and the Velocity Template Language. You'll then learn how to apply Velocity in a variety of areas with the help of richly detailed code examples. Additionally, you'll be taken through the steps of building a complete application in order to see how you can utilize all of the techniques and technologies discussed in the book. Covering the latest features of Velocity 1.3, Mastering Apache Velocity shows you how to:

• Build Java-based Web sites with Struts, servlets, Turbine, and other open-source tools

• Generate a wide variety of Web content and code, including Java, XML, SQL, and Postgres

Product Details

Publication date:
Java Open Source Library
Product dimensions:
7.45(w) x 9.21(h) x 0.86(d)

Read an Excerpt

Mastering Apache Velocity

By Joseph D. Gradecki Jim Cole

John Wiley & Sons

Copyright © 2003

Joseph D. Gradecki Jim Cole
All right reserved.

ISBN: 0-471-45794-9

Chapter One

Web Development Basics

If you remember back almost 10 years ago, you might be able to visualize
how the World Wide Web got its start. Telnet and FTP were among the first
examples of this profound way of communicating. These technologies
weren't directly associated with the Web, but they were certainly a precursor.
Gopher, on the other hand, was a technology that demonstrated how machines
connected on the Internet could be used to share information with people all
across the world.

Soon after gopher was being used to pull information from various places and
sites, the World Wide Web was developed-and the rest is history. This book
explores a part of Web development that has flourished in recent years: the presentation
of dynamic data to the client. Our focus is on a new technology called
Velocity. In this chapter, we provide an overview of the history of Web development
and bring us into the present.

Static Web Pages

It all started with static Web pages; individuals posted photographs of their family
and students posted their lasted research findings. As you would expect, this
was in the early '90s. Amazon was still a concept inbudding entrepreneurs'
heads, and the trading of pictures was basically nonexistent. A page was written
in HTML and had no content produced from a database or other application.
The closest thing to a WYSIWYG GUI for HTML was a yellow legal pad.

The information provided on a static Web page consisted of the content the
page creator wanted to put on it-and nothing more. If users didn't need that
particular information, they had no way to interact with the Web page in order
to bring forward the desired content. The page creator could provide links that
led the user to other pages of information, but the content was still that of the
page creator.

Introducing CGI

CGI (Common Gateway Interface) was one of the more profound technologies
to invade the development of Web pages. Developed in 1993, CGI is a way of
interfacing the Web page with the back-end server responsible for serving
pages to the user. You can see an example of the interface in a search site. If you
go to Yahoo! or Google, you type a topic that you want to learn about into an
edit line typically positioned next to a Submit button. This edit line and button
are part of an HTML form. When you click the Submit button, an action takes
place that is typically a call to another Web page or possibly an application.

When you're using CGI, the action is an application written in a variety of languages,
such as C, Perl, or C++. CGI is not the application itself but serves as
the interface between the form action and the application. When a user clicks
the Submit button, the CGI is responsible for transferring any information from
the HTML form to the server and activating the application on the HTTP server.
The application on the server executes a set of instructions and returns to the
interface a Web page that is displayed to the client browser. Listing 1.1 shows
an example of a simple C CGI application.

int main(int argc, char* argv) {
printf("content-type: text/html\r\n");

Hello World!




Listing 1.1 A simple CGI application in C.

Because CGI applications execute on the Web server, the issue of security is
important. Most servers require that the CGI applications be placed in a directory
called /cgi-bin. The server typically won't allow a CGI application to execute
anywhere than on the server. It is important to note that CGI applications
are built using high-level languages, and were in the beginning quite frustrating
to write until proper libraries came along. At first, these languages weren't typically
used by graphic designers or those who just wanted to put up an interactive
page. As the Web industry started to grow, another option was needed.

Scripting: JSP/ASP/PHP

As you might expect, using CGI and Perl, C, or C++ wasn't something the new
crop of Web designers were going to be able to do. This meant that Web development
companies had to hire both Web designer and software development
talent in order to produce the results the new demanding clients expected. In
addition to this fact, the major players in the software development community,
like Sun and Microsoft, wanted in on the dynamic Web development world. So
they each created a server-side language that could handle getting information
from a back-end system to the client.

Unlike high-level languages that typically have to be compiled into a binary that
will execute on a specific machine, scripting languages are designed to make it
easy to write applications and execute them within an interpreter. The interpreter
is written in a high-level language and executes on the machine where
the HTTP server is located, thus eliminating the need for the Common Gateway

To use the scripting languages, you create a Web page in which the statements
and keywords of the scripting language are embedded in the same file as the
HTML that will be displayed to the user. Listing 1.2 shows an example of such a

Time Example

The time is

Listing 1.2 A scripting language page.

In Listing 1.2, we added some scripting code that displays the current time.
When users browse to this page, they see the current time as supplied by the
HTTP server. So, how does this all occur?

The scripted Web page is placed on the Web server just like any other HTML
page. However, the extension applied to the scripted file is not .html or .htm; it
has an extension like .asp, .jsp, .php, or something else. These extensions are
important because they tell the Web server how the file should be handled
when accessed by a client. If the requested file has an extension other than
.htm, the server sends the file to an interpreter. The system administrator will
have already told the Web server about all possible extensions it might have to
handle and the associated interpreter for the file type.

The interpreter then processes the scripted file and interprets only the scripted
code, leaving all of the HTML intact. During the processing of the file, the
scripted code might place additional HTML into the file as needed. The additional
HTML probably relates to information requested by the user.

At this point, the interpreter returns the final HTML file to the HTTP server,
which in turn provides the page to the client's browser. By using a scripting language,
the Web designer doesn't have to be familiar with high-level programming
languages. Unfortunately, the scripting languages can become complex
and using them may be no more efficient than using CGI and a C++ program.

Future Development

While we are on the subject of scripting languages, it should be noted that there
are some available-like JavaScript, Java, and ActiveX-which can be used and
subsequently compiled by the server for better performance compared to the
interpreted languages. Listing 1.3 shows an example of using Java in an HTML

<%@ page language='java' import='java.sql.*' %>

<% ResultSet rs;
try {

connection = DriverManager.getConnection(
statement = connection.createStatement();

Listing 1.3 Using Java in an HTML page. (continues)

Listing 1.3 Using Java in an HTML page. (continued)

Embedding Java into an HTML page is probably the ultimate in adding dynamic
capabilities to a Web site. Not only do you have to know Java, but an additional
piece of server software is needed to execute the embedded code. An application
server like Resin or Tomcat compiles the Java into a servlet, which is then
executed by the application server when a user browses the HTML page.

In all of the different types of dynamic pages we've discussed so far, the code
for the view and the business logic is intermixed. As you learn in the next chapter,
this intermixing can produce headaches for an organization.

What's Next

In this chapter, we examined the history of Web page development. While many
readers will already be aware of this history, this material is a good background
for those just getting into the Web arena. In the next chapter, we dive into the
methodologies commonly used in the development of Web applications and
focus specifically on the MVC (Model-View-Controller) paradigm.


Excerpted from Mastering Apache Velocity
by Joseph D. Gradecki Jim Cole
Copyright © 2003 by Joseph D. Gradecki Jim Cole.
Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

JOSEPH D. GRADECKI is a senior software engineer at Comprehensive Software Solutions and an associate professor of computer science at Colorado Technical University. He is an expert in using open source and other tools for building Web and distributed applications. Gradecki is also the author of several books including Mastering JXTA (Wiley).
JIM COLE is a senior software engineer specializing in custom J2EETM development, database management, and security maintenance. He is an active developer who regularly uses open source tools such as Struts and Velocity, and also has experience with Perl and PHP.

Customer Reviews

Average Review:

Post to your social network


Most Helpful Customer Reviews

See all customer reviews