0

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!

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by martin5211
0

Well at the moment it's a instance variable..
Will give the it a try as a class variable tonight, hope it works.

It does make sense though doesn't it? Instance variables have separate values for each instance of a class. Class variables maintain a single shared value for all instances of the class.

Will let you know if it works,
Thanks for the tip!

0

So, turns out the actualy problem was that I should've initialized the accBalance under class level and not under the doPost method..

Stupid mistakes like this that just gives me stomach aches! Anyway, it's solved now..

0

It's a common mistake that everyone makes. Other issue that could happen are related with thread safety when using attributes, could get the exact same symptoms.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.