Construct a simple web application using servlets that administers an arithmetic quiz. The web application will be driven by a database with one table that stores a list of students and their scores on the quiz and a second table containing a list of questions and answers. The database will be prepared ahead of time with a list of student names and a list of questions and answers preloaded.
The welcome page for the application will be a simple HTML page that displays a simple welcome message and contains a button labeled "Start the quiz". Clicking the button triggers a call to the first servlet in the web application.
The first servlet begins the process of running the quiz. The first thing the servlet will do is to create a Java object that will track the student's progress through the quiz. The object will contain a member variable that records how many problems the student has seen and a member variable that keeps track of how many problems the student got right. The first servlet will store that object in a session for the second servlet to use. Next, the servlet will connect to the database and fetch the list of students who have not yet completed the quiz. The servlet then returns a web page containing a simple form with a drop-down list containing the students' names and a button labeled "Begin". Clicking the button triggers a call to the second servlet in the web application.
The second servlet is the main servlet in the web application. Each time this servlet is invoked it first checks the value of the counter stored in the tracking object. If the value of the counter is 0, the servlet checks the request to find out the student's name and then stores that name in the tracking object. It then increments the counter to 1 and responds by sending back a page containing the first arithmetic problem in the database and a response form. The response form contains a text field where the student can enter the answer to the arithmetic problem and a button labeled "Submit". Clicking the submit button in the form invokes the second servlet again. If the value of the counter is between 1 and 9, the servlet checks the request for an answer to the previous problem and checks the answer the student submitted against a result stored in the database. If the answer is correct the servlet will increment the number correct counter in the tracking object. After completing this processing, the servlet increments the counter and returns a web page containing the next problem and the response form. If the value of the counter is 10, the servlet checks the student's answer to the final problem, records the student's score in the database, and returns a final page that shows the student how they did on the quiz. This final page should be a simple HTML page that shows the student's name and how many problems they got correct out of the 10 problems in the quiz.