Please deploy your application on CS3, and upload all source files to Canvas. The source files should include all the source code, and an HTML file lab8.html which contains a hyperlink to your application on the CS3 server.


Problem Description

Please download the file DrivingTest.txt and put it under the /WEB-INF folder of your project. This file contains a number of questions for a driving test. The format of the file is as follows:

Note that you may not change the content of this file.

For this lab, develop a web application that allows a user to browse the driving test one question at a time. For example:

When parking your vehicle parallel to the curb on a level street.
1. Your front wheels must be turned toward the street.
2. Your wheels must be within 18 inches of the curb.
3. One of your rear wheels must touch the curb.
Correct answer: 2

Next

A use may click on the Next link to see the next question, and when the last question is shown, clicking on Next should take the user back to the first question. The difficult part of this is to keep track of the index of the current question in order to know which question to display when the user clicks Next. There are two acceptable approaches:

For the same reason as discussed in Week 6 Lab, keeping track of the current question using application scope is not an acceptable solution.

Implementation

Your implementation must use the MVC architecture discussed in class. In particular:

1. (10pt) Create a model class Question to hold the information about a question. The class should have the following properties:

2. (60pt) Create a controller servlet DrivingTestBrowser as follows:

HINTS:

Because the questions are already stored in application scope, you don't need to pass questions to the JSP because JSP can access all application scope variables.

To access a question at a certain index in JSP, you can use something like ${questions[index]} (assuming the application scope variable for the question list is questions).

3. (30pt) Create a JSP that displays the question and the Next link using EL.

Notes

In the servlet you can get the path to the file DrivingTest.txt using getServletContext().getRealPath("/WEB-INF/DrivingTest.txt"), and this file should also be uploaded to CS3 when you deploy your application.

There are two common ways to read a text file in Java:

a) Using Scanner. For example:

        Scanner in = new Scanner(new File(filename));
        while( in.hasNextLine() )
            System.out.println(in.nextLine());
        in.close();

b) Using BufferedReader. For example:

        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); 
        String line;
        while( (line = in.readLine()) != null )
            System.out.println(line);
        in.close();

Please also read To Catch or Not To Catch regarding exception handling in Java.

Last Updated: 10/24/2020 22:22 Views: 700