Java Reference
In-Depth Information
18.7.2
Let's review what we need our servlet application to do for us. Given an account
name and the initial dollar amount, we need to:
Design
• Create a top-level account with that amount of dollars
• Display the current account and its total and remaining dollars, along with
a list of its subaccounts, if any
• Create subaccounts, specifying a name and dollar amount
• Make a selected subaccount be the current one, displayed as above
After each or any of these actions, the servlet has to spit out the HTML
page for the user to view. If the user wants to create a subaccount, then the
servlet produces a form page for entering the name and dollar amount for the
subaccount. When the user presses a Create button on that page, the browser
tells the servlet (via the form data) that the servlet should create the subaccount
and redisplay the current account with this new subaccount added to its list.
TIP
It may help to think of the servlet as a two-step process, with a current and fu-
ture perspective. The first step is the action that the servlet must perform based
on the supplied parameters (e.g., create a new account). The second step is
the creation of the page allowing the user to take the next (future) action. That
page reflects the state of things after the parameter-driven action has occurred.
In our example, that means showing the list of subaccounts including the one
that we just created.
Let's spell out in more detail what our interactions with the servlet will be,
and describe what output we expect for each of those inputs. We will create a
keyword to tell the servlet what function we want it to perform; we'll call the
parameter func . We will sometimes need two other parameters: name and
dollars .
Table 18.1 shows our design as a compact reference.
The code for our servlet is at http://www.javalinuxbook.com/ . Let's
look at some of the key parts of the servlet in more detail. We'll look at:
1) reading the parameters, 2) the core business logic of the servlet, as described
in Table 18.1, and 3) how we create and output the HTML.
Search WWH ::




Custom Search