Hello, World! Example

Hello, World! Example

In the time-honored tradition started by Kernighan and Ritchie's book, The C Programming Language, the first Echo example will display "Hello, World!" in a browser window. For all practical purposes, this is the simplest Echo application that can be built. The screen below presents the quite unremarkable output of the Hello World application:

If you have the Echo Tutorial Examples installed, you may run this example by visiting http://localhost:8080/EchoTutorial/helloworld. You must change the hostname and port number if your server is not running on localhost on port 8080.

All Echo applications will contain a minimum of two classes. The first, which must extend Echo's EchoServer class, is used to create a new "user-instance" of the Echo application for each user visiting the application. The second class is the user-instance of the application itself, and must extend the Echo class EchoInstance.

The Hello World application consists of only the two required classes. The first is HelloWorldServlet which extends EchoServer. The EchoServer class requires the developer to provide only a single method, newInstance(). The purpose of the newInstance() method is to create a unique user-instance of the Echo application when a new user first visits it. As can be seen from the example, newInstance() simply returns a new instance of the HelloWorld object. It is worth noting that EchoServer extends the Java Servlet container's HttpServlet class, thus making the Echo application available through any Servlet container.

The second class of this example defines the HelloWorld application itself. This class, named HelloWorld, extends EchoInstance. An EchoInstance represents a single user's instance of an Echo application. Again, the developer is required to provide one method: init(). The init() method plays the equivalent role of a desktop application's static main(String[] args) initialization method.

The init() method must return an Echo Window object. A Window object represents the contents of a user's browser window. When a user visits an Echo application, the application will place content in the user's open browser window. This open window is represented by the returned Window object.

import nextapp.echo.ContentPane;
import nextapp.echo.EchoInstance;
import nextapp.echo.Label;
import nextapp.echo.Window;
import nextapp.echoservlet.EchoServer;

public class HelloWorldServlet extends EchoServer {

    // Returns a new user-instance of the Echo application.
    public EchoInstance newInstance() {
        return new HelloWorld();
    }
}

class HelloWorld extends EchoInstance {

    // This init method is called when a user first visits the 
    // application.  It must return a Window object that will 
    // occupy the contents of the user's open browser window.
    public Window init() {
    
        // Create a new window.
        Window window = new Window();
        
        // Create a content pane.  Components may not be added 
        // directly to a window, only to a content pane.
        ContentPane content = new ContentPane();
        
        // Set the window's content to be the content pane.
        window.setContent(content);

        // Create a new label that says "Hello, World!"
        Label label = new Label("Hello, World!");
        
        // Add the label to the content pane.
        content.add(label);
        
        // Return the new window.
        return window;
    }
}

The internals of the init() method of the HelloWorld object serve to build up a representation of a browser window presenting the words "Hello, World!" The first operation is to create a new Window. A Window may only contain a specific type of Echo component, called a "pane". The two most commonly used pane components are ContentPane and ContainerPane. The latter is used to hold multiple sub-panes and will be discussed in a later chapter. For this example, a ContentPane is used. A ContentPane is capable of holding Echo Component objects.

checkboxes. Even windows and content panes are derived from the Component class.

In this example, our ContentPane contains only a single object: a Label. A Label is one of the most simple Echo components available. It is used to display a string of text, an image, or both. In this case, a new Label is added to the ContentPane that will display the text, "Hello, World!"

Once the construction of the Window has been completed, it is returned from the init() method. The newly created window will be displayed in a user's browser whenever the user launches this Web application by visiting HelloWorldServlet's URI.