I'm struggling with a servlet that's suppose to represent an "online banking service".
Once you run the servlet you'll see it's an extremely simple example... You enter an amount in the textfield provided, then click on the apropriate button depeding what action you want to perform e.g. "deposit","withdraw" and "balance". Your balance then is displayed in a label underneath the textfield.
Now the problem is: I had to assign a default value for accBalance (0.00). Now everytime I deposit money in, for example $50, my balance does show $50. But when I want to do another diposit ontop of that, e.g. another $100. You'd think that the balance now should be $150 right? ..It doesn't, it then displays $100.. The reason for this (I think) is that everytime the servlet is re-loaded the accBalance is again set to 0.00.
*Once you run the servlet and test it out you'll see what I mean*
The code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HTMLBank extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
//Create variable
double accBalance = 0.00;
//Set MIME type of content returned to browser
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try{
//Starts outputting the HTML Form
out.println("<html><head>" +
"<title>Online Bank Simulator</title>" +
"</head><body>" +
"<hr color=\"#808000\">" +
"<center><h1>Banking Simulation</h1></center>" +
"<form method=\"POST\" action=\"../servlet/HTMLBank\">" +
"<center>Amount: <input type=\"text\" name=\"Amount\" size=\"20\"></center>");
// Gets input from user
//String strAmount = req.getParameter("Amount");
//Decides which action to take
String action = req.getParameter("act");
if(action != null)
{
if(action.equals("Deposit"))
{
double amount;
String strAmount = req.getParameter("Amount");
amount = Double.parseDouble(strAmount);
accBalance = accBalance + amount;
out.println("<br><center>Balance:"+accBalance+" </center> <br>");
}
else if(action.equals("Withdraw"))
{
double amount;
String strAmount = req.getParameter("Amount");
amount = Double.parseDouble(strAmount);
accBalance = accBalance - amount;
out.println("<br><center>Balance:"+accBalance+" </center> <br>");
}
else if(action.equals("Balance"))
{
out.println("<br><center>Balance:"+accBalance+" </center> <br>");
}
else
{
out.println("<br><center>Balance:"+accBalance+" </center> <br>");
}
}
else
{
out.println("<br><center>Balance:"+accBalance+" </center> <br>");
}
//Outputs rest of HTML
out.println("<table width=\"35%\" align=\"center\">" +
"<tr><td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Deposit\">" +
"</td>" +
"<td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Withdraw\">" +
"</td>" +
"<td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Balance\">" +
"</td></tr>" +
"</table><br>" +
"</form>" +
"<hr color=\"#80800\">" +
"</body></html>");
}//end of try
catch(NullPointerException e)
{
out.println("<html>");
out.println("<head><title>error</title></head>");
out.println("<body>");
out.println("<h1>NullpointerException error</h1>");
out.println("</body>");
out.println("</html>");
}//end of catch
}
}
Thanks guys, appreciate it!