• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet handling its own outputted HTML form  RSS feed

 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still new to Java and I find that by far the aspect taking longest to understand is servlets.

I am busy writing a servlet that must initially show an HTML form (if a FORM is actually needed here), in which
the user enters a monetary amount into a text field.

There are 3 buttons underneath the textfield: Balance, Deposit and Withdraw.

Deposit: adds the amount in the textfield to the total "bank account" of the user.
Withdraw: subtracts the amount in the textfield from the total "bank account" of the user.
Balance: displays the current remaining total of the "bank account" just under the textfield.

What is unusual is that in this task, they require me to only use a servlet to address all these things.

So the servlet HTMLBank.java must do the following:

- Display form with textfield for entering an amount, a line for showing the current remaining total of the bank
account, and the abovementioned buttons doing the said actions.

- Add entered amount to the account total, via appropriate button's input.

- Subtract entered amount from the total, via appropriate button's input.

- Show to the screen the current remaining total of bank account, via appropriate button's input.

So, where it's common to use a separate HTML document as the form page, and have its submission redirect the
browser to the separate servlet file for data processing, here I have to make the servlet write that form, as well
as take its input and then show the described output - all on the same Web page.

And this is where I am confused as to how to achieve this. Firstly, these buttons I coded and assigned methods to,
do not work. I click them but nothing happens. Help on the buttons would be also appreciated.

The following is the code of my HTMLBank.java servlet (this is nothing confidential as it's not actually linked
to any real bank account. Just a textbook task.):



The part I'm unsure of is how to code the same servlet to create a form, then use its own form's input and respond
back to that same Web page. The operations - deposit, withdraw, balance display - are to run as many times as the
user desires.

Any fixes, help and suggestions are hugely appreciated! Thanks.
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming this has to be a single servlet, first off remove the state from your current servlet.  That is the balance and formattedAmount members.
Servlets should not have state (generally).  They aren't threadsafe, so you'll end up with weird bugs when you have multiple users hitting it at the same time.

You only actually need to save the balance, which can be stuck in the session.
Obviously it'll reset itself each time a new session starts up, but until you learn about persisting data that's just something you'll have to put up with.

The formattedAmount is calculated on each request anyway, so no need to store it.

Since each request will result in a new page being sent to the client, move the code that does that job into its own method.
That way each type of request can simply call that method when it's done whatever processing it needs to do.

Do not do this:

A GET is not a POST.

Indeed, your GET ought to simply handle your Balance functionality.
The other two can be handled by POST, with some attribute.
 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the rapid response, Dave.

By avoiding the servlet to have state - removing declared variables that have the class scope - I still need to set an initial money amount though: in this case a logical $0.

Instead of totally removing the "balance" variable, can I declare and initiate it in the init() method? Not sure if that is what you meant. Surely init() is the very start of servlet's session and destroy() the end, so anything to be participating in the session must be coded from init() or between it and destroy()?
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet's lifespan is not session based.  In general there will be a single instance of a servlet for your webapp.

So you would initialise the balance when you first need it.
I'd add a getBalance method that retrieves the balance from the session and, if null, would create a new balance and add that to the session for later use.
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the points Dave made, you really should move your presentation out of the servlet. You can use JSP or another templating language to get rid of all those out.println() statements.
 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, thank you.

Stephan, unfortunately (while I also wish this), my assignment instructs, as I also stated in the post, that I must create ONE servlet program to do everything. Else, I would likely use a separate HTML file for the form at the beginning, etc.
 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I worked on the servlet today and below is the updated code. I also describe after it how the servlet is required to perform. Please point me in the right direction.



For now ignore all custom-defined methods and only look at addToBalance() (and if needed, the methods it calls), as I
am first trying to get this one function to work as needed. When I succeed, I'll code other functions similarly,
except the difference in the arithmetic operations.

To explain what needs to happen, using the function I'm trying to get to work right now:

1) Initially form appears with "Balance: $0" displayed and and empty textbox labeled "Amount:".
2) The user enters money amount and clicks "Deposit". The servlet must redisplay the form, but
the "$0" and the textfield must be cleared. Invisible to user, the entered amount is added to the balance.
3) If the user clicks "Balance", the page must redisplay, showing the current balance as "Balance: $xxx" and the
textfield must once again be cleared.

The problem is that when I run the servlet per above code, enter an amount and click "Deposit", the only thing that
happens is the textfield clears, but the "$0" remains shown. I.e. the button does not work as intended.

I appreciate your help a lot! Thanks.
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first off, I suggested earlier that the display code ought to be in their own methods, since you will need to call them from three different places, one for each button.
This includes the error version of the page as well.

That would get all that display code out of the way and allow you to see the actual calculation code a little easier.

You've still got the balance as part of the servlet, but I suppose that can be put to one side for now.  Just be aware that this is wrong and would cause major issues in the "wild".

The issue you are currently having is that the form does a GET.  Now, I still say these should be a GET and two POSTS, however I doubt you want to be faffing with Javascript, or even can faff with Javascript for this exercise.
So I take back the bit where I suggested using both doPost and doGet.
You'll have to stick the logic in doGet.

But make it look something like:

Something like that anyway.

That way the doGet is only farming out the work to methods that you can then follow in isolation.
Your current structure is almost like that as it stands now, so the above shouldn't be too much of a shuffle.
 
Yevgeni Duvenage
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Dave.

1) Why do you assign the "success" variable, which is obviously boolean true/false, to methods doing numerical calculations? I understood the "success" condition is the amount verification process.
2) I agree that it's best to methodise each separate function of the servlet and then simply call the required methods in the doGet(), it be much neater that way. I'll do so with future programs. Will sort them out now.
3) As to the "Depost" button not working, and how to go back to the initial page/form after user clicks any button, could you please suggest?

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!