Java Reference
In-Depth Information
Example 18.2 Implementing the BudgetPro servlet actions
if ("begin".equals(act)) {
Account top = new Account(name, theUser, dollars);
session.setAttribute("top", top);
session.setAttribute("current", top);
nextPage = new AccountView(top);
} else if ("mkacct".equals(act)) {
// show the subaccount creation page
nextPage = new SubPage(null);
} else if ("cancel".equals(act)) {
Account current = (Account) session.getAttribute("current");
nextPage = new AccountView(current);
} else if ("create".equals(act)) {
Account current = (Account) session.getAttribute("current");
try {
current.createSub(name, dollars);
nextPage = new AccountView(current);
} catch (NumberFormatException nfe) {
// show the subaccount creation page (with error message)
nextPage = new SubPage("Bad number format");
}
} else if ("cd".equals(act)) {
Account current = (Account) session.getAttribute("current");
Account nextAcct = current.getSub(name);
session.setAttribute("current", nextAcct);
nextPage = new AccountView(nextAcct);
} else if ("back".equals(act)) {
Account current = (Account) session.getAttribute("current");
Account nextAcct = current.getParent();
session.setAttribute("current", nextAcct);
nextPage = new AccountView(nextAcct);
} else {
log("Unknown func=["+act+"]");
response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
}
The way that we construct the output, it will all get sent back to the user
in one fell swoop. That's fine for relatively short pages with rapid response time.
If response time is a major concern and you are sending large quantities of data,
you may want to change things a bit. Instead of building up the output in a
StringBuffer and then getting it all back with a toString() call, you could
take each of our append() calls and make them individual out.println()
Search WWH ::




Custom Search