Home

 

Implementing the ListAccounts servlet

A skeleton servlet now exists but does not perform any actions when it is invoked. We now have to add code to the servlet in order to implement the required behavior.

The ListAccounts.java code of the servlet is already opened.

Locate the file:
c:\7672code\webapp\servlet\ListAccounts.java

Replace the contents of the ListAccounts.java with the sample file. This should compile successfully with no errors.

Look at the source code for the ListAccounts.java servlet. This class implements the doPost and doGet methods, both of which call the performTask method.

The performTask method does the following tasks:

First the method deals with the HTTP request parameters supplied in the request. This servlet expects to either receive a parameter called customerNumber or none at all. If the parameter is passed, we store it in the HTTP session for future use. If it is not passed, we look for it in the HTTP session, because it might have been stored there earlier.
Next the method implements the control logic. Access to the Bank facade is obtained through the ITSOBank.getBank method and it is used to get the customer object and the array of accounts for that customer.
The third section adds the customer and account variables to the HttpRequest object so that the presentation renderer (listAccounts.jsp) gets the parameters it requires to perform its job. The control of processing the request is then passed through to listAccounts.jsp using the RequestDispatcher.forward method, which builds the response to be shown on the browser.
The final part of the method is the error handler. If an exception is thrown in the previous code, the catch block will ensure that control is passed to the showException.jsp page.

See Figure | -9 for a sequence diagram of the design of this class.

The ListAccounts servlet is now complete. The changes should be saved and the source editor closed.
ibm.com/redbooks