Chapter 3. Writing a Web Application

This chapter gives the fundamentals of web application development with IT Mill Toolkit. The overview gives an introduction to the Application class that every user application must inherit. Every application has a main window. The main window and other windows are also managed by the application object, so we give a detailed description of the various window classes, together with some common design patterns. Also various resources, such as images and downloadable documents, are managed by the Application, so we look into the basic resource interfaces and classes. Finally, we look into the deployment of applications as Java Servlets in a web container.

Related topics in other chapters include the use of events and listeners, the basis of all user interaction in applications, which are are detailed in Section 2.4, “Events and Listeners”.

To gain more insight about application design with IT Mill Toolkit, you may want to read Chapter 9, Advanced Web Application Topics. For a newcomer into AJAX development, it explains the role of pages in AJAX web applications, and provides some basic design patterns for applications.

3.1. Overview

An application that uses IT Mill Toolkit must define an application class that inherits the abstract com.itmill.toolkit.Application class. The application class must implement the init() method.

public class MyApplication extends com.itmill.toolkit.Application {

    public void init() { 
        ... initialization code goes here ...
    }
}

The web application API may seem similar to Java Servlet API, but that is only superficial. IT Mill Toolkit framework associates requests with sessions so that an application class instance is really a session object. Because of this, you can develop web applications much like you would develop desktop applications.

How does it work? IT Mill Toolkit framework does basically everything it does on top of the Java Servlet API, which lies hidden deep under the hood, with the terminal adapter being the lowest level layer for handling requests from the web container. When the web container gets the first request for a URL registered for an application, it creates an instance of the ApplicationServlet class in IT Mill Toolkit framework that inherits the HttpApplet class defined in Java Servlet API. It follows sessions by using HttpSession interface and associates an Application instance with each session. During the lifetime of a session, the framework relays user actions to the proper application instance, and further to a user interface component.

Application class also provides facilities for window access, execution control, and theme selection.

As the application instance is really a session, it can end. If the user quits the application through the user interface, an event handler should call the close() method in Application. However, as the user interface runs under a web browser, a user can simply close the browser window and the application has no way of knowing it happened. When the user starts the browser again and opens the application URL, the application window will be rendered where the user left off. This can be desired behaviour in many cases, but often it is not and it creates security problems. A common solution is to use a timeout to terminate a session automatically.

The user application class needs to be registered in the web application. This is done in the web application package, see Section 3.7, “Application Environment”.