File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Coding Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding Problem" Watch "Coding Problem" New topic

Coding Problem

Janice Caselman

Joined: Nov 17, 2004
Posts: 1
I am in an Into to Java class and this is my last week. Up until now, I have been able to keep up to some degree by using the code in my book and altering it. However, for my final project, I have to do something that I am unable to locate any similar code for in the book...any help would be appreciated. Up to this point, the java assignments have only been writing Java applications in TextPad and running them. I have not even had any classes in HTML coding yet, I have had one class in QBasic and am comfortable with procedural programming, and one class in SQL. This is my first OOP class and I am really struggling trying to move from the the concept of procedural code to objects.

I am not wanting someone to write the code for me. I just would like some directions of what I need to write an applet for something then a class for something else....whatever it is.

Write a Java servlet,, that creates a HTML form that contains: A text box for entering an amount, a text field that displays a formatted balance, and three buttons ("Deposit" to add the amount to the balance, "Withdraw" to subtract the amount from the balance, and "Balance" to display the balance) Use an instance variable for the balance. If no amount is entered in the text box or if it is a zero or negative number, output a simple HTML page that displays an error message. Also, be sure that an amount was entered before converting the string variable for amount to a double.

I have to be able to run this on my Tomcat server with my web browser. I have no idea where to even start.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Java Servlets are part of the J2EE api's. You can read about them in this tutorial.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Janice,

For Tomcat setup, you can check out this thread.

You can also download free online version of Core Servlets & JSP, 1st Edition at the

Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
I know the lost feeling of not knowing where to start so I'll tell you what I'd try.

You could have a single servlet with
* A method (1) to handle buttons being pushed on the normal html page (this method will check for input errors and decide whether to call method 2 or method 3 for further action
* A method (2) to update the balance and display the normal page
* A method (3) to display the error page (from the error page you return to normal page with last "good" values for balance, withdrawl and deposit.
* Variables to hold balance and the amount to withdraw or deposit, maybe booleans such as isWithdrawl and isDeposit (obviously one will do but maybe 2 is easier to understand).

Long streches of html that are used time and again can be static string variables of the servlet.

When the servlet starts up you give balance and withdrawl/deposit amount default values (presumably zeros) and display the normal page.
Apparently the same text field that originally displays the balance will be used to enter the amt. of a deposit or withdrawl, this means you should maintain your start balance in the html code as a hidden value.

When processing input (1) read the balance from the hidden value and (2) make sure you do not change the balance until deposit/withdrawl input is verified as OK.
After making a withdrawl or deposit and thereby changing your balance value remember to reset the withdrawl/deposit amt. to zero (this might later help with finding errors).

-There is a lot I might have missed but start working and you should begin to see for yourself how the project is fleshing out.

[ November 17, 2004: Message edited by: Elouise Kivineva ]
[ November 17, 2004: Message edited by: Elouise Kivineva ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
However, for my final project, I have to do something that I am unable to locate any similar code for in the book...any help would be appreciated.
Did your teacher recommend any books on Servlet? If not, you might want to consider getting this book Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition by Marty Hall. The examples given in the book can be downloaded here.
It is sorta covered in the JavaRanch Style Guide.
subject: Coding Problem
It's not a secret anymore!