Read an Excerpt
Chapter 1: Three-Tier Web Applications
This chapter and the next serve as preparation for the rest of the book. Their goal is to make sure we have covered everything we need before really setting off, in Chapter 3. If you are impatient and well prepared, you can skip large parts of these two chapters and come back as needed for the missing details.
What is it that we need? Primarily, three things:
- an understanding of the structure of three-tier Web applications
- a familiarity with the Servlet API
- a familiarity with JDBC
In this chapter, we introduce three-tier applications and discuss a simple example that will lay the basics of this topic: a Phonebook application that can be used to look up phone numbers. Admittedly, this is not the jazziest or coolest application for a book to start with: for a while we were thinking of a database of plants, each with a Latin name, an image and a piece of music appropriate for the plant. However, that would just distract us from the subject and make the two "packing-up-for-the-journey" chapters longer than they need to be. There will be enough excitement before the book is over.
Our introductory Phonebook example is necessarily simple in its assumptions and primitive in its structure but, once the basics have been mastered, we can immediately produce a Better Phonebook that allows a person to have more than one phone number. More importantly, it separates out the different tasks of the middle tier, such as database access and HTML generation, to different classes that can be independently developed without modifying the basic servlet. We also include some utility classes and a useful Logger class. At that point we will have seen two slightly different servlets and will be ready for an overview of the Servlet API 2.1 that rounds off this chapter.
- Three-tier applications
- Servlet basics, including request-response processing in a servlet
- JDBC basics: drivers, connections, queries and resultsets
- Servlet life cycle
- The Logger class and the advantages of do-it-yourself logging
- More JDBC: prepared statements
- The basics of HTML generation
- An overview of the Servlet API
The last item on this list is rather large. We make it worth its space by providing added value over and above the JavaDoc documentation. We show the logical structure of the API and bring out the most important parts of it. In the end we provide something like an "inverted index" that links common tasks to those parts of the AN that are needed to accomplish them.
Three-Tier Applications
A distributed Web application has its different parts spread over different computers on a network. What parts are there? A common design pattern is to divide the application into View, Controller and Model: these are the logical components of the application, and their distribution merely represents the most obvious structuring of the underlying logic.
The Model (or application logic) is the internals of the application which "models" its data. The View and the Controller together form the user interface: the View (or presentation logic) shows the user different parts and aspects of the Model, and the Controller (or business logic) allows the user to modify the values of the Model, or change how the Model is viewed. Often, the user will be unaware that the Controller exists: in its simplest form, it merely handles the communication between the other two tiers. Depending on the constraints of your project, you will see some variance in where the application and business logic reside. In addition to the Model, View and Controller, the application needs a persistent store: some place to save its state between different runs of the application. That store can be a file system or a database.
Think of a familiar application, like Microsoft Excel. The view is a grid of rows and columns that show the numbers and formulas in different parts of the spreadsheet, or perhaps the graph of those numbers and formulas. The controller consists of the buttons on toolbars, menu commands and key shortcuts. The model is a constraint propagation system that ensures that changes in one area of the spreadsheet are propagated throughout the rest of it, so that the constraints expressed by the formulas are maintained. The state of the spreadsheet is saved in a file that has a proprietary .x1s extension.
Distributed applications usually separate out the user interface and put it on a client computer; that way, many clients can share the same program running on a server. The persistent store can either be on the same or a different server. A generic three-tier application is arranged in three tiers like so:
Three-tier applications predate the Web, but the Web has profoundly changed them because a distributed Web application is based on open and widely accepted standards. It uses the Web browser as its user interface. This means that it runs on a TCP/IP network and uses the HTTP protocol to communicate with the server. The HTTP protocol uses HTML and other standard MIME types for the data it passes between the server and the client. The situation on the back end is more complex, but by-and-large, the framework we are using (servlets,JDBC and relational databases) is reasonably close to the open standard ideal (even if it would be hard to work within a subset of SQL that is acceptable to all available databases). A distributed three-tier Web application is arranged in three tiers like so:
Many variations on this picture are possible, especially if the web page on the client contains a Java applet. For instance, (assuming the security issues are addressed) the applet can open a direct line of communication to the database, or it could open a socket and talk TCP/IP to a dedicated port on a computer somewhere, bypassing the Web server and HTTP. We will investigate these and other possibilities later in the book. For now, our task is to learn the basic framework of the above diagram. This means learning servlet and JDBC programming. This chapter will cover the basics; the next will cover more advanced topics....