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.

418Chapter 18Servlets: Java Pressed into ServiceTable 18.1 BudgetPro servlet acti<strong>on</strong>sfunc parameterOther paramsActi<strong>on</strong>Next screenbeginname, dollarsCreate a top-level account, save inthe sessi<strong>on</strong>.mainmkacctn<strong>on</strong>en<strong>on</strong>esubacctcanceln<strong>on</strong>eGet account from sessi<strong>on</strong>.maincreatename, dollarsGet account from sessi<strong>on</strong>; createsubaccount.maincdnameGet account from sessi<strong>on</strong>, look upsubaccount by name, save as currentin sessi<strong>on</strong>.mainbackn<strong>on</strong>eGet account from sessi<strong>on</strong>, getparent from account, save as currentin sessi<strong>on</strong>.mainThe parsing of the parameters is very straightforward. The request parameter,part of the signature of the doGet() and doPost() methods, can be usedto retrieve the parameters we need:String act = request.getParameter("func");String name = request.getParameter("name");String dollars = request.getParameter("dollars");Notice that we always ask for all three parameters, even though we willoften use <strong>on</strong>ly <strong>on</strong>e (act). Once we have the requested functi<strong>on</strong> in act, it’s justa matter of if-then-else-ing our way through the possible values and takingthe appropriate acti<strong>on</strong>s. We store, or retrieve, the current account in thesessi<strong>on</strong> manager, thereby providing c<strong>on</strong>tinuity between browser requests(Example 18.2).The output is the page to send back to the browser. We createthat page as an object, either an AccountView or a SubPage. TheHttpServletResp<strong>on</strong>se provides us with an output channel <strong>on</strong> which to write.java.io.PrintWriter out = resp<strong>on</strong>se.getWriter();if (nextPage != null) {resp<strong>on</strong>se.setC<strong>on</strong>tentType("text/html");out.println(nextPage.toString());}

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

Saved successfully!

Ooh no, something went wrong!