O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ListManager Controller

The controller class for our ListManager application is named Controller, and is listed in Example 20-9. When first created, its init( ) method connects to a database and uses that database connection to create the UserFactory object that manages mailing list subscriptions. The most important thing to understand about this servlet is that its doGet( ) method handles all requests made to the ListManager application. Regardless of what page is displayed in response, the request is first handled through the Controller servlet.

The key to making this work is the WEB-INF/web.xml configuration file in the je3.war web application archive. This file contains the servlet initialization parameters used in the init( ) method to specify how to connect to the database. Most importantly, however, the web.xml file includes servlet mappings that specify that the Controller servlet should be invoked in response to the filename "ListManager/" (prefixed with the appropriate host, port, and "je3/", of course) and also in response to any filename of the form "ListManager/*.action". The doGet( ) method looks up the name by which the servlet was invoked, and uses that to dispatch to a method that can take the appropriate action. Hyperlinks within the web application all include a ".action" suffix, so they are all directed back to the Controller servlet.

The Controller servlet knows how to handle actions named "login", "edit", "unsubscribe", and "logout", and dispatches to methods ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required