Ajax programming with the Java Scripting API

An excerpt from 'Beginning Java SE 6 Platform: From novice to professional'

Jeff Friesen's Beginning Java SE 6 Platform: From Novice to Professional is out now from Apress. In this article Jeff introduces you to his new book by presenting excerpts from its chapter on the Java Scripting API (JSR 223). You'll learn some of the fundamentals of the Java Scripting API, such as how to obtain a script engine, get the script engine to evaluate scripts, and communicate with scripts via script variables. You'll also have the chance to apply what you've learned to a project involving the XMLHttpRequest object used in Ajax programming.

Java SE 6 officially arrived on December 11, 2006. Shortly thereafter, I wrote Beginning Java SE 6 Platform: From Novice to Professional for Apress. The book consists of 10 chapters and five appendices that explore many new and improved features in Java SE 6. The final appendix previews several features that will likely appear in Java SE 7.

Chapter 9 focuses on the Java SE 6 Scripting API, which enables Java programs to interact with scripts written in Ruby, PHP, Groovy, JavaScript, and other scripting languages. This API was developed under JSR 223: Scripting for the Java Platform; it consists of six interfaces, five regular classes, and one exception class that are stored in the javax.script package.

This article, an excerpt from Chapter 9, gets you started with the Java Scripting API. You'll learn how to obtain a script engine (a software component that evaluates scripts), how to get the script engine to evaluate scripts, and how to communicate with scripts via script variables.

I also present new material not found in the book, giving you a chance to apply what you've learned in a project that bridges the gap between Java and Ajax (Asynchronous Javascript and XML) programming. I present a Swing application that uses the Java Scripting API and XMLHttpRequest object to obtain instantaneous weather data, which the application's GUI displays.

Obtaining a script engine

Prior to performing other scripting tasks, a Java program must obtain an appropriate script engine. A script engine exists as an instance of a class that implements the ScriptEngine interface or extends the AbstractScriptEngine class. The program begins this task by creating an instance of the ScriptEngineManager class via one of these constructors:

  • The public ScriptEngineManager() constructor works with the calling thread's context classloader if one is available, or the bootstrap classloader otherwise, and a discovery mechanism to locate ScriptEngineFactory providers.
  • The public ScriptEngineManager(ClassLoader loader) constructor works with the specified classloader and the discovery mechanism to locate ScriptEngineFactory providers. Passing null to loader is equivalent to calling the former constructor.

The program uses the ScriptEngineManager instance to obtain a list of factories via this class's public List<ScriptEngineFactory> getEngineFactories() method. For each factory, ScriptEngineFactory methods, such as String getEngineName(), return metadata describing the factory's script engine. Listing 1 presents an application that demonstrates most of the metadata methods.

Listing 1. EnumerateScriptEngines.java

// EnumerateScriptEngines.java

import java.util.*;

import javax.script.*;

public class EnumerateScriptEngines
{
   public static void main (String [] args)
   {
      ScriptEngineManager manager = new ScriptEngineManager ();

      List<ScriptEngineFactory> factories = manager.getEngineFactories ();
      for (ScriptEngineFactory factory: factories)
      {
           System.out.println ("Engine name (full): "+
                               factory.getEngineName ());
           System.out.println ("Engine version: "+
                               factory.getEngineVersion ());
           System.out.println ("Supported extensions:");
           List<String> extensions = factory.getExtensions ();
           for (String extension: extensions)
                System.out.println ("  "+extension);
           System.out.println ("Language name: "+
                               factory.getLanguageName ());
           System.out.println ("Language version: "+
                               factory.getLanguageVersion ());
           System.out.println ("Supported MIME types:");
           List<String> mimetypes = factory.getMimeTypes ();
           for (String mimetype: mimetypes)
                System.out.println ("  "+mimetype);
           System.out.println ("Supported short names:");
           List<String> shortnames = factory.getNames ();
           for (String shortname: shortnames)
                System.out.println ("  "+shortname);
           System.out.println ();
      }
   }
}

Assuming that no additional script engines have been installed, you should observe the following output when you run this application against Java SE 6:

Engine name (full): Mozilla Rhino
Engine version: 1.6 release 2
Supported extensions:
  js
Language name: ECMAScript
Language version: 1.6
Supported MIME types:
  application/javascript
  application/ecmascript
  text/javascript
  text/ecmascript
Supported short names:
  js
  rhino
  JavaScript
  javascript
  ECMAScript
  ecmascript

The output reveals that an engine can have both a full name (Mozilla Rhino) and multiple short names (rhino, for example). The short name is more useful than the full name, as you will see It also shows that an engine can be associated with multiple extensions and multiple MIME types, and that the engine is associated with a scripting language.

1 2 3 4 5 Page
Recommended
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more