More action with Struts 2
In a recent review of Struts 2 in Action, JW Blogger Oleg Mikheev notes that Struts 2 is "just a collection of extensions built upon WebWork, which is ultimately the right thing to learn before starting a Struts 2 project." While Struts 2 has some architectural flaws, Oleg calls WebWork well-designed, well-tested, and reliable. What are your experiences using Struts 2 and WebWork?

Also see "Hello World the WebWork way," a JavaWorld excerpt from WebWork in Action, by Patrick Lightbody and Jason Carreira.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

XML and Java: A potent partnership, Part 3

Learn how to integrate Java and JavaScript, two popular programming languages

When it comes to getting the most bang for your buck in the world of programming languages, nothing beats a good scripting language. A scripting language provides a clean, easy-to-use, high-level toolkit geared toward high-speed application prototyping and development. This month, I'll demonstrate how to integrate JavaScript, a popular scripting language, into Java, so that Java applications can enjoy these benefits as well.

Our past

Recently, we've explored the interface between Java and XML. More specifically, we've been looking at ways to use Java's ability to dynamically load code to improve our ability to process XML -- in particular, to handle XML tags we didn't specifically design our application to handle.

"XML and Java: A Potent Partnership": Read the whole series!



The Java-based framework I developed over the preceding two columns was capable of examining tags and their attributes, and even analyzing the context in which tags occur in order to properly handle them. As an application developed within this framework encountered novel tags in its XML input, it located and loaded code to handle those tags. The one feature it lacked was scripting.

Adding scripting to the framework brings a number of advantages. First, scripting languages provide a natural and concise method of specifying behavior (scripting languages are programming languages, after all). Second, since scripts are text, they can be incorporated into the XML itself. This is essential for XML applications such as the GUI definition application we've been developing.

The plan

Here's the roadmap for what lies ahead.

Because of the sheer volume of what I'm about to cover, I'll present this material in two parts. This month, I will cover the JavaScript language and discuss how to integrate the Rhino JavaScript implementation into Java. Next month, I'll demonstrate how to integrate JavaScript into our XML framework.

Rhino

There are several capable scripting languages available for Java, covering a wide feature space, from compiled to interpreted and from procedural to declarative. The list includes versions of many of our favorites from the days before Java: TCL (Jacl), Python (JPython), JavaScript (Rhino), Scheme (at least a dozen different implementations), and so on. For an exhaustive list, take a look at Robert Tolksdorf's Web page, "Languages for the Java VM," in the Resources section at the bottom of this article.

For this exercise, I've selected the Rhino JavaScript implementation. I chose JavaScript because it's reasonably easy to use and learn, it's powerful, it's already both a de facto and a de jure standard (see ECMA-262 in the Resources), and it's extensible.

JavaScript was developed by Netscape for use in its Web browser. Originally called LiveScript, the name was changed to JavaScript at roughly the same time that Java was incorporated into Netscape Navigator 2.0. Since then, JavaScript (or a derivative) has found its way into most major browsers.

Resources