Question
Implement an online tool for practicing single-digit additions. Create a Dynamic Web Project in Eclipse, then create a servlet called Addition as follows: In doGet(),
Implement an online tool for practicing single-digit additions.
Create a Dynamic Web Project in Eclipse, then create a servlet called Addition as follows:
In doGet(), generate two random numbers between 1 and 9, and display a form that asks the user to enter the sum of the two numbers. For example, suppose the two random numbers are 7 and 2, the form should look like the following:
7 + 2 = Submit
* The answer is in a textbox and submit button next to it. The randomly generated numbers are not in a textbox
You can use nextInt(int bound) method in Random to generate the random numbers.
In doPost(), display the correct answer, the user's answer, and tell the user if their answer is correct. For example, suppose the user entered the answer 9, the display should be:
7 + 2 = 9
Your answer 9 is incorrect.
Try Again
Clicking on Try Again should take the user back to the form with two new numbers.
The difficult part of this exercise is how to get the two random numbers in doPost() in order to check the user's answer and display the result. You may be tempted to store them in application scope, but that would be a wrong approach. Data in application scope is not only shared by all servlets, it is also shared by all users - a major difference between web applications and desktop applications is that a web application is used by many users at the same time. If you store the numbers in application scope, one user's numbers will be overwritten by another user's, which will cause the application to show wrong result for the first user. You can use two browsers to simulate two users to see the effect of this. The proper way to do this is to embed the two numbers in the form generated by doGet() using two hidden form fields, so they will be submitted to doPost() together with the user's answer.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started