Question:
Write a Java servlet, HTMLBank.java that creates the HTML form. Use an instance variable for the balance. IF the user enters a valid amount in the amount text box and then clicks the Deposit button, add the amount to the balance; if the user clicks the Withdrawn button, subtract the amount from the balance. If no amount was entered or if the amount is negative or zero, output a simple HTML page that displays an appropriate error message. Just say then to compile and the url in the Web browser. (second part) to execute a second thread start another instance of our web browser and lead the servlet in it as well. Switch between the 2 instances, perform transactions with each. Write a short paragraph that explains how the displayed balance verifies that the variable, balance, is shared. HINT: use DecimalFormat to format numeric output. Use static method, Double.parseDouble(StringVariable) to convert to String from the text box parameter, amount, to a double. Be sure that an amount was entered before converting to a double.
My other question:
Did I understand correctly and did I do as they ask? PS if they say Switch between the two instances and perform transactions they mean that I should have to web pages open and then test to see if the one's balance is correct to the other one??
My Code:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.net.*; import java.text.DecimalFormat; public class HTMLBank extends HttpServlet { public DecimalFormat pattern = new DecimalFormat("$#0,000.00"); // To set a patter for display public Double actBalance = 0.00; // initial display value public String displayBalance = pattern.format(actBalance); double entered = 0.0; public void init(ServletConfig conf)throws ServletException { super.init(conf); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); //HttpSession session = req.getSession(true); // get current session out.println("<html>"); out.println("<head>"); out.println("<title>Online Bank ATM Simulator</title>"); out.println("</head>"); out.println("<body>"); out.println("<form method=POST action=/servlet/HTMLBank>"); out.println("<center>"); out.println("<hr>"); out.println("<h1>Bank ATM Simulation</h1><br>"); out.println("Amount: <input type=text name=ENTRY size=20 maxlength=15><br><br>"); out.println("Balance:"+displayBalance+"<br><br>"); out.println("<input type=submit name=DEPOSIT value=\"Deposit\"> "); out.println("<input type=submit name=WITHDRAW value=\"Withdraw\"> "); out.println("<input type=submit name=BALANCE value=\"Balance\"> <br><br>"); out.println("<hr>"); out.println("</form>"); out.println("</table>"); out.println("</body>"); out.println("</html>"); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { entered = Double.parseDouble(req.getParameter("ENTRY")); } catch(NumberFormatException e) { entered = 0.0; } if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance) && (entered > 0)) { actBalance = actBalance - entered; doGet(req, res); } else if((req.getParameter("DEPOSIT")!=null) && (entered > 0)) { actBalance = actBalance + entered; doGet(req, res); } else if(req.getParameter("BALANCE")!=null) { displayBalance = pattern.format(actBalance); doGet(req, res); } else { res.setContentType("text/html"); PrintWriter out = res.getWriter(); res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); //HttpSession session = req.getSession(true); // get current session out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Incorrect Input Warning..</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("<H1>Invalid Entry</H1>"); out.println("<H2>You have entered an incorrect amount!!!</H2>"); out.println("<H3>You must enter a number only and it must be greater than 0.0</H3>"); out.println("</from>"); out.println("</BODY>"); out.println("</HTML>"); } } }