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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

18.7 Designing a BudgetPro Servlet419Example 18.2 Implementing the BudgetPro servlet acti<strong>on</strong>sif ("begin".equals(act)) {Account top = new Account(name, theUser, dollars);sessi<strong>on</strong>.setAttribute("top", top);sessi<strong>on</strong>.setAttribute("current", top);nextPage = new AccountView(top);} else if ("mkacct".equals(act)) {// show the subaccount creati<strong>on</strong> pagenextPage = new SubPage(null);} else if ("cancel".equals(act)) {Account current = (Account) sessi<strong>on</strong>.getAttribute("current");nextPage = new AccountView(current);} else if ("create".equals(act)) {Account current = (Account) sessi<strong>on</strong>.getAttribute("current");try {current.createSub(name, dollars);nextPage = new AccountView(current);} catch (NumberFormatExcepti<strong>on</strong> nfe) {// show the subaccount creati<strong>on</strong> page (with error message)nextPage = new SubPage("Bad number format");}} else if ("cd".equals(act)) {Account current = (Account) sessi<strong>on</strong>.getAttribute("current");Account nextAcct = current.getSub(name);sessi<strong>on</strong>.setAttribute("current", nextAcct);nextPage = new AccountView(nextAcct);} else if ("back".equals(act)) {Account current = (Account) sessi<strong>on</strong>.getAttribute("current");Account nextAcct = current.getParent();sessi<strong>on</strong>.setAttribute("current", nextAcct);nextPage = new AccountView(nextAcct);} else {log("Unknown func=["+act+"]");resp<strong>on</strong>se.sendError(HttpServletResp<strong>on</strong>se.SC_NOT_IMPLEMENTED);}The way that we c<strong>on</strong>struct the output, it will all get sent back to the userin <strong>on</strong>e fell swoop. That’s fine for relatively short pages with rapid resp<strong>on</strong>se time.If resp<strong>on</strong>se time is a major c<strong>on</strong>cern and you are sending large quantities of data,you may want to change things a bit. Instead of building up the output in aStringBuffer and then getting it all back with a toString() call, you couldtake each of our append() calls and make them individual out.println()

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

Saved successfully!

Ooh no, something went wrong!