Table of Contents

Writing WebObjects Applications in Java

The major elements of a WebObjects application are the page creation and customization logic that binds WebObject page elements to real objects. The WebObjects Java Extensions allow those "real objects" to be pure Java objects that embody custom logic and compute results for HTML display.

With the Java Extensions, instead of scripting you create Java subclasses of next.wo.WebSession and next.wo.WebApplication, and build pages with subclasses of next.wo.Component. (Note that for this release, Java subclasses of next.wo.WebSession and next.wo.WebApplication must be placed in the .woa directory, while Java subclasses of next.wo.Component must live in the .wo directory with which they are associated.) To start your application, then, you simply point the Java Virtual Machine to your compiled Java subclass of Application; everything else will be loaded on demand.

Often you may find yourself building logic inside custom objects whose persistent storage is managed by the Enterprise Objects Framework. The Java classes that make up the next.eo package provide access to EOF. Use the Framework to manage your Java subclasses of next.eo.CustomObject. You can use EOModeler in conjunction with your Java objects: to name the class of the resulting object: simply provide the Java subclass name where appropriate, qualified by its package name (using slashes).

The WebObjects Java Extensions includes a Java package corresponding to each of the three key WebObjects Frameworks: Foundation, Enterprise Objects, and WebObjects. These packages are next.util, next.eo, and next.wo, respectively. See "The Java Packages" for information on the contents of these three packages.

Using WebObjects Builder

WebObjects Builder has been enhanced so that it can write a component's logic in Java. To do this, you must set your language preference to Java before you create a new application. Go to the preferences panel, and choose "Java" in the Languages display. After your preference is set to Java, WebObjects Builder creates .java files where it used to create .wos files (for example, Application.java, Session.java, and Main.java). It also checks your Java syntax for you when you try to save the component.

For more information on using WebObjects Builder, refer to the WebObjects Developer's Guide.

Using Project Builder

To construct a WebObjects application using Java, you'll need to use Project Builder to manage the project. Project Builder understands .java files and will allow you to install them in the Classes suitcase. Also, the Project Builder makefiles have been augmented so that they know how to compile .java files. Because of this, you can simply build your Java project just as you would any other. For more information on using Project Builder to construct WebObjects applications, refer to "Compiling and Debugging a WebObjects Application" in the WebObjects Developer's Guide.

Note: when using Project Builder to create WebObjects applications in Java, you must have the WebObjectsSupport bundle loaded. To load this bundle, start Project Builder, select Preferences from the Tools menu, and select Bundles from the pull-down list at the top of the Preferences panel. Click Add, and in the Open panel browse to NextDeveloper/PBBundles (relative to NEXT_ROOT). Select WebObjectsSupport.bundle, and click Open.

Building an Existing Project

The DodgeDemoJava example is a fairly substantial WebObjects application written entirely in Java, and serves as an excellent model of how you'd design a typical application. This demo uses custom logic, written in Java, to compute car leases. To build and run the example, follow these steps:

  1. Using the Windows NT Explorer, traverse to the WebObjects/Examples/DodgeDemoJava.woa directory beneath your web server's document root and double-click the PB.project file located there. Project Builder will launch, and the DodgeDemoJava application will be loaded.

  2. Click the build icon to bring up the Project Build window. In the Project Build window, click the build icon. DodgeDemoJava will build.

  3. Make sure that your web server is running.

  4. Start your web browser. If your browser is running on the same machine on which your server is running, enter the following URL:
    http://localhost/cgi-bin/WebObjects/Examples/DodgeDemoJava
    

    Otherwise, simply substitute your server name (myserver.next.com, for example) for localhost in the above URL.
The Java application should automatically start and make itself accessable through your browser. In the event that it doesn't start automatically, you can start it manually from a Bourne shell by changing to the Java application's ".woa" directory beneath your web server's document root (WebObjects/Examples/DodgeDemoJava.woa, in the above example) and typing the following (substituting the name of your application for "DodgeDemoJava", and the full path to your web server's document root for DOC_ROOT):

./DodgeDemoJava.exe -d DOC_ROOT/ Examples/DodgeDemoJava

After you've started your application manually, you should be able to access it from your web browser as detailed in step 4, above.

Creating a New Project

The creation of a new WebObjects project is a two-step process. First, you create a new project using WebObjects Builder. Then, launch Project Builder and create a new WebObjects Application, specifying the .woa directory created in the previous step as the Project Path. From this point on, you work on your project using Project Builder and WebObjects Builder as you normally would. Making the resulting project will produce an executable that can be launched like any other WebObjects application.

Browsing Java Classes

The WebObjects Java Extensions includes a tool that allows you to easily browse through the contents of various Java classes (including Objective-C classes that have Java "wrappers"). This tool is located in NextDemos\JavaBrowser.app (relative to NEXT_ROOT), and can be started simply by clicking on JavaBrowser.exe. Upon startup, you'll be able to browse any packages found in your CLASSPATH. If your CLASSPATH environment variable isn't set, you can specify an explicit path by selecting Preferences from the Class Browser's Tools menu, and editing the contents of the "Startup Paths" text view.

Double-click package names to display the contents of the package. Double-click the name of a class to display its fields and methods. By default, the main browser window shows the "Interface View," which displays the interface for the selected class. If source code is available for the selected class, clicking the Source button (located at the bottom of the browser display) will bring up that source. Otherwise, you can get some idea of the inner workings of the class by clicking Decompile: the selected class will be "decompiled" and the results will be displayed in the browser's main window.

Using Symantec Cafe with WebObjects Builder

Symantec Cafe may be the development environment of choice for WebObjects Java developers for several reasons: For some people, there may be some advantages to using Symantec Cafe instead of Project Builder: Symantec Cafe isn't for everyone, though:

Creating HelloWorldJava

The following procedure shows you how to use Symantec Cafe to create the "HelloWorldJava" application (it assumes that you have Symantec Cafe release 1.51 installed):
  1. Launch WebObjects Builder, and select Java in the Language Preferences. Create a new application named "HelloWorld.woa". Create a Main page, with a variable visitorName and an action sayHello. Create a Hello Page with a variable visitorName. Save the project.
    You needn't quit WebObjects Builder at this stage, but when you go back to it later, make sure you do a Revert to Saved on the project. If you close all component windows at this time, you'll need to click on Revert To Saved only if you modify Application.java or Session.java in Symantec Cafe.
  2. Copy the ".exe" file from any example into your .woa directory. Rename the executable "HelloWorld.exe". This copy and rename enables both autostart and a simple shell start of your application.
  3. Launch Symantec Cafe. Create a new project, using Project Express, as follows:
    1. In the "Name project" panel, create a HelloWorld.prj project. Symantec differs from Project Builder here in that it allows you to keep the project files together in the same directory (Cafe/Projects), while the Java files can be located anywhere you want.
    2. In the "Set project type" panel, set the Target Type to "Application".
    3. In the "Add files to project" panel, browse to your HelloWorld.woa directory and add the Application.java, Session.java, Main.wo/Main.java and Hello.wo/Hello.java files.
    4. In the "Initial settings" panel, enter your Classpath.
  4. Drag the project icon out of the tool bar to display the project window. Right-clicking on the project name gives you a menu which allows you to modify the project setting (to provide start-up arguments) or edit the project (to add new files), among other features.
  5. Select Project Setting and enter the following in the Compiler Output Directory field:
    PATH-TO-THE-APP/HelloWorld.woa/Java
    
    click Yes in the alert panel, confirming that you wish to create a new directory.
  6. Click on Application.java, and add an initializer:
    public Application() {
        super();
        System.out.println("Welcome to HelloWorld Java");
    }
    
  7. Click on Main.java, and complete the sayHello method:
    public Component sayHello() throws Exception {
        Hello nextPage = ((Application)application()).pageWithName("Hello");
        nextPage.setVisitorName(visitorName);
        return (Component)nextPage;
    }
    
  8. Click on Hello.java, and add a setVisitorName method:
    public void setVisitorName(String aName) {
        visitorName= aName;
    }
    
  9. Click the compile icon. If the compile fails, click on the failure line and the editor will position you at the offending line.
  10. Once everything compiles without error, you can either start your application from a shell, or you can simply allow it to autostart from your browser. If you did not copy the ".exe" file as instructed earlier in this procedure, you'll need to start the application from a shell by changing to the .woa directory and entering the following:
    java -classpath $CLASSPATH JavaMain -d  Examples/HelloWorld
    
As was mentioned previously, when debugging your applications you're on your own. The Symantec debugger will crash shortly after you start it on your application (this problem will be addressed once Sun's VM 1.1 is officially released). For now, your best bet is to use System.out.println().

Table of Contents Next Section