13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

18.7 Designing a BudgetPro Servlet41718.7.2 DesignLet’s review what we need our servlet applicati<strong>on</strong> to do for us. Given an accountname and the initial dollar amount, we need to:• Create a top-level account with that amount of dollars• Display the current account and its total and remaining dollars, al<strong>on</strong>g witha list of its subaccounts, if any• Create subaccounts, specifying a name and dollar amount• Make a selected subaccount be the current <strong>on</strong>e, displayed as aboveAfter each or any of these acti<strong>on</strong>s, the servlet has to spit out the HTMLpage for the user to view. If the user wants to create a subaccount, then theservlet produces a form page for entering the name and dollar amount for thesubaccount. When the user presses a Create butt<strong>on</strong> <strong>on</strong> that page, the browsertells the servlet (via the form data) that the servlet should create the subaccountand redisplay the current account with this new subaccount added to its list.TIPIt may help to think of the servlet as a two-step process, with a current and futureperspective. The first step is the acti<strong>on</strong> that the servlet must perform based<strong>on</strong> the supplied parameters (e.g., create a new account). The sec<strong>on</strong>d step isthe creati<strong>on</strong> of the page allowing the user to take the next (future) acti<strong>on</strong>. Thatpage reflects the state of things after the parameter-driven acti<strong>on</strong> has occurred.In our example, that means showing the list of subaccounts including the <strong>on</strong>ethat we just created.Let’s spell out in more detail what our interacti<strong>on</strong>s with the servlet will be,and describe what output we expect for each of those inputs. We will create akeyword to tell the servlet what functi<strong>on</strong> we want it to perform; we’ll call theparameter func. We will sometimes need two other parameters: name anddollars.Table 18.1 shows our design as a compact reference.The code for our servlet is at http://www.javalinuxbook.com/. Let’slook 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 describedin Table 18.1, and 3) how we create and output the HTML.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!