# FreeMarker Example Web Application 2

To try this example you should visit `http://yourServer/thisWebApp/index.a`

Note: To simplify the example, the guest-book entries are not stored persistently. If you reload the servlet all guest-book entry will lose.

## What's this example about?

This is a very primitive controller servlet class (was written based on FreeMarker Example Web Application 1) and a simple guest-book appliction that uses the conroller servlet class. This whole stuff is a very primitive thing. It is only to get you started if you plan to develop some custom "framework" for your Web applications, rather than using an already written framework. Note that a Web application framework can use very different approach than this example.

## How this example works?

This example uses a primitive controller servlet, `example.ControllerServlet`. To add application specific behavior, you should extend this servlet with an application specific subclass, that adds the so-called action methods. Here we implement a primitive guest book application by extending `ControllerServlet` with `GuestbookServlet`, that adds 3 action methods:

- `indexAction`: Shows the whole guest-book.
- `formAction`: Show the from where you enter a guest-book entry.
- `addAction`: Adds a new guest-book entry.

The `ControllerServlet` calls the action methods when it receives client requests. It deduces the name of the action methods to call from the request URL. The servlet will be invoked only if the request URL is ending with ".a", as you can see in the `WEB-INF/web.xml`, otherwise a static file (for example this html file, or an image file) will be returned as is. To deduce the method name, the servlet removes the `.a` and the Web-application directory from the request URL, and then appends `"Action"`. Thus, if you type `http://yourServler/thisWebApp/foo.a`, then it will try to call the `fooAction` method.

Each action method gets two parameters: the `HttpServletRequest`, and the `Page` object. The `Page` object plays the role of `HttpServletResponse`, but instead of calling low lever methods, you add objects to the data model with `put(name, value)`, and choose the template (the view) with `setTemplate(templateName)`; the tedious dirty work is done by `ControllerServlet`.

The templates are stored in the `WEB-INF/templates` directory.

For more details read the source code.

Back to the index page