I have a basic HTML calculator that asks the user to input 2 integers and select from a dropdown their desired math operation:
The servlet will process the calculation and counts how many times a certain operation has been used:
The counter for Multiplication has incremented accordingly.
Question: However, when I press "Go Back" and process a new computation, all counters reset to 0. Is there a way without using HttpSession to persist the counter values?
(It has not been taught to us, so we are not allowed to use it yet.)
For example, going back and having below input...
First Operand: 4
Second Operand: 5
...will result to...
Multiplication: 1 (counter from previous computation maintained)
Is there a way without using HttpSession to persist the counter values?
You can add 4 hidden type elements inside the form of the servlet(to store addCtr,subCtr,mulCtr,divCtr) that you pass to index page which in turn will return them to your servlet. But then you will have to turn your index.html to index.jsp.
I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
You can use the browser (have no idea which one you are using -- recommend Chrome as its debugging tools are extensive) to "view source" which will show you the HTML that your servlet is sending to the browser. If the values in the hidden inputs aren't set correctly, then you know that you need to work on getting them right. If they are set correctly, then you know that the problem comes later than that.
Learning to debug is an essential part of coding. It's not something you put off to learn "later".
@Varun Selva: I scratched everything and restarted with my original code (first post). Your solution worked! I did not have the reinit() method though, I just moved the counter variables from doPost() as instance variables.
@Bear Bibeault: While it is clear this is not good practice, may you kindly explain why it worked? I don't mean to be sarcastic, just plain curious.
(I'm pretty sure our professor will teach us security, browser debugging, and the proper way to do things in our future Java EE classes, just that, we are basically at the HelloWorld phase of servlets as of the moment.)
It worked because you are building a faux session using a user id as a key. If I were your instructor and you passed this in you would fail. Just because you can get something to work doesn't make it good.
That is one reason I would fail this assignment. You should be using conventional techniques, such as the hidden input approach, that you would end up using later in real applications. This map technique is not something that would ever be done in real code where an actual session can be used.
The hidden input approach would involve JSP, and things I don't understand as of yet. True, it is the proper way, but I can't defend something I don't understand when I get questioned for it. I'd rather get a lower score for a known mistake rather than get a good score from something I didn't fully grasp.
I know folks are keen on the proper way to do things, I get that and again, we will get to the conventions as we go along our lessons. We all started at some point, just that, you can't expect everything to be taught during the first exercise.
I understand your intentions and it's appreciated, but please also understand such "student" situations. Thanks.