Generating Code with Velocity

by Daniel H. Steinberg
ONJava Newsletter for 05/06/2004


Templates can obviously be used for more than generating the personalized spam that ends up in your inbox. In the first part of "Template-Based Code Generation with Apache Velocity", Giuseppe Naccarato explains basic concepts related to templates and transformations, and implements a simple code generator that takes an XML representation of classes and data members and generates the Java code to define them using Apache Velocity.

Download a copy of the Jakarta DBTags library and follow along with Deepak Vohra's tutorial "SQL Database Access with DBTags". After setting up your environment, the article takes you through obtaining a database connection and creating a database table. You then will generate a ResultSet from a Database Table. Finally, you will update a database table with a prepared statement.

Although you may want to persist information in XML files, as a Java developer you want to interact with this information as objects. Satya Komatineni explains Java Architecture for XML Binding in "The State of JAXB: Availability, Suitability, Analysis, and Architecture". Komatineni writes "JAXB makes use of XML schema definitions for generating the bindings between XML and Java. This current dependency on XML schemas is somewhat debated, but in situations where the schemas are readily available, JAXB works quite well in generating the Java classes."

You can start with a few techniques for generating images on the server-side and pushing them out to the client. In our featured article from, Joshua Marinacci demonstrates "Generating Images with JSPs and Servlets". He begins with a simple pie chart example. A JSP is used to parse the incoming request and pull out the information for the size of the slice and its background color. The pie chart is drawn into a buffered image and then written to a file, in this case as a PNG. Joshua also shows examples of displaying text using FontRenderContext and for generating thumbnail sketches of images.

To subscribe to the newsletter (or any O'Reilly Network newsletters), visit and select the newsletters you wish to receive in your user profile (you'll need to log in with your existing O'Reilly Network account -- if you don't yet have an account, you'll need to create one).

To change your newsletter subscription options, please visit and click the"Manage My Newsletters" link. For assistance, send help to

Until next week,

Daniel H Steinberg, editor and

Copyright © 2017 O'Reilly Media, Inc.