FileDocCategorySizeDatePackage
AtmServlet.javaAPI DocExample2257Wed Apr 05 11:25:42 BST 2000None

AtmServlet

public class AtmServlet extends HttpServlet

Fields Summary
Account
act
Constructors Summary
Methods Summary
public voiddestroy()

    // This is where we would save the balance to a file
  
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)


    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();

    out.println("<HTML><BODY>");
    out.println("<H2>First Bank of Java ATM</H2>");
    out.println("Current Balance: <B>" + act.balance + "</B><BR>");
    out.println("<FORM METHOD=POST ACTION=/servlet/AtmServlet>");
    out.println("Amount: <INPUT TYPE=TEXT NAME=AMOUNT SIZE=3><BR>");
    out.println("<INPUT TYPE=SUBMIT NAME=DEPOSIT VALUE=\"Deposit\">");
    out.println("<INPUT TYPE=SUBMIT NAME=WITHDRAW VALUE=\"Withdraw\">");
    out.println("</FORM>");
    out.println("</BODY></HTML>");
  
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

    
    int amt=0; 

    try {
      amt = Integer.parseInt(req.getParameter("AMOUNT"));
    } 
    catch (NullPointerException e) { 
      // No Amount Parameter passed
    }
    catch (NumberFormatException e) {
      // Amount Parameter was not a number
    }
   
    synchronized(act) {
      if((req.getParameter("WITHDRAW") != null) && (amt < act.balance))
        act.balance = act.balance - amt;
      if((req.getParameter("DEPOSIT") != null) && (amt > 0))
        act.balance = act.balance + amt;
    } // end synchronized block

    doGet(req, resp);                   // Show ATM screen
  
public voidinit(javax.servlet.ServletConfig conf)

    super.init(conf);
    act = new Account();
    act.balance = 0;