PROG2240: Java Web Tech Assignment 3 Winter 2019

Assignment 3

 

 

PROG2240: Java Web Tech

Assignment 3

 

Winter 2019

 

Assignment Type: Pair Programming

Due Date: See eConestoga Assignment Dropbox (start of class)

 

Before you begin:

 

Read and study sample book applications from Chapters 7, 8 and 9.

 

Do the Individual Exercise 7-2 and 7-3 on page 242.

 

o     If NPE occurs when a CD link is clicked: Find the getRealPath() method and prefix a "/" for the method argument (i.e., "/WEB-INF/products.txt"). See the exercise solution for more details.

 

Then, do Individual Exercise 8-1 on page 267 and demo your exercise solutions to your partner. Keep a copy of your individual exercise solution and be ready to demo, if and when requested.

 

Print A3_CoverPage_Page4.pdf. Let your partner certify completion of individual exercise.

 

You must have the Java source code (i.e., Book.java) provided in A2.

 

Read the Important Notes section of A1 and Additional Requirements of A2.

 

Background:

 

In this assignment, you will continue to use the MVC pattern using servlets and JSPs. You will use the servlets with action pattern for Cart servlet, JSP EL and JSTL to develop the XXYY Computer Programming Club's Loan Cart Web application. You will also use the various scope (i.e., page, request, session and application) of EL when accessing attributes.

 

Additional Requirements:

 

Explicitly specify the scope when accessing attributes using EL (e.g., sessionScope).

 

Do not use <c:import> tag when <jsp:include> is adequate.

 

Do not use sendRedirect() method when forward() method is appropriate.

 

Do not use scripting elements (e.g., JSP scriptlet, JSP expression) when JSP EL is available.

 

 

 

Page 1 of 9


Assignment 3

 

 

Tasks

 

1. Setup XXYYClub NetBeans project a. Add Java classes

 

Download A3Files.zip and copy these two Java classes to the existing club.business package.

 

o      ELoan.java and ECart.java

 

o      Study all the new code provided

 

o      Use the methods provided as part of your solution (see next steps).

 

Important: Do not modify any of these classes. Also, do not use BookIO class for A3.

 

b. Ensure that the JSTL library is available for this application

 

c. Add a new context parameter to web.xml

 

Parameter Name: booksFilePath

 

Parameter Value: /WEB-INF/books.txt

 

Hint: Open web.xml in NetBeans and click General tab

 

 

 

 

2. Modify the Banner JSP page

 

Add two new hyperlinks to the XXYYBanner.jsp.

 

o      eLoan (linked to XXYYLoan servlet -- see Step 3a)

o      eCart (linked to XXYYCart servlet -- see Step 4a)

 

For the screen layout of these hyperlinks, refer to the sample screenshot in Step 3.

  

 

Page 2 of 9


Assignment 3

 

 

3. Write the eLoan servlet and JSP a. Add an eLoan servlet

 

Under the Java source (i.e., "Source Packages") directory, add a new package: club.cart

Add a servlet named XXYYLoanServlet.java

Set URL pattern to /XXYYLoan

Within the processRequest() generated method:

Create a new servlet context object (i.e., application object)

 

Using the booksFilePath context parameter defined in web.xml, prepare the full path name of books.txt file

 

Maintain a loanitems object to store the ArrayList of Book objects available on loan.

Hints:

 

Create/save the loanitems object in the servlet context object If loanitems stored in the servlet context object is null, call

 

the loadItems(string) method of ELoan class. This will populate loanitems object with the list of books from the books.txt file.

Forward the control to XXYYELoan.jsp page.

 

b. Add an eLoan JSP page


 

Add a JSP page named XXYYELoan.jsp.

Add the banner and footer pages.

 

Using JSTL and EL (not scripting elements), add the code to match the design layout shown below.

 

The eLoan JSP page shows a list of loanitems with the quantity-on-hand (QOH) Each time you click the Reserve hyperlink, call the XXYYCart servlet with two parameters


 

o

 

o

 

o


action: set to "reserve

code: set to the code selected (e.g., 101)

 

Note: The XXYYCart servlet must require these two parameters to add items to the cart


 

Page 3 of 9


Assignment 3

  

Note: If the Reserve link is selected, call the XXYYCart servlet (see next step). When items are reserved (i.e., added to the loan cart), the QOH of that loan item is reduced by

 

1. If the QOH becomes zero, the Reserve link becomes "N/A" similar to the screen shot shown below (see Step 6, QOH for code 101).

 

 4. Write the eCart servlet and JSP a. Add the eCart servlet

 

Under the club.cart package, add a servlet named XXYYCartServlet.java Set URL pattern to /XXYYCart

Within the processRequest() generated method:

Access the loanitems object stored in the servlet context.

 

Create/save a loan cart object (instantiated from ECart class) in the session object

If action is "reserve":

 

Using the “code” parameter, get details of the loan item (i.e., Book object) by calling the findItem(ArrayList, String) method

of ELoan class.

 

Call the addItem(Book) method of the loan cart object Reduce the loan item's QOH by 1

 

Hint: Call the subtractFromQOH(ArrayList, String, int) method of the ELoan class.

 

Page 4 of 9


Assignment 3

 

 

Forward the control to XXYYECart.jsp page

 

b. Add the eCart JSP page

 

Add a JSP page named XXYYECart.jsp.

 

This page will display all the loan items added to the cart. Add the banner and footer pages.

 

Using JSTL and EL (not scripting elements), add the code to match the design layout including the title shown below.

Show total quantity in the cart (i.e., reserved).

 

o Hint: Use the <c:set> JSTL tag to declare a variable. o Quantity column should be aligned to the right.

 

Sample screen shot when Reserve link is selected for both code 101 and code 201:

 5. Write the ClearCart servlet

 

Under the club.cart package, add a servlet named XXYYClearCartServlet.java Set URL pattern to /XXYYClearCart

Within the processRequest() generated method:

  

Page 5 of 9


Assignment 3

 

 

Create/save a loan cart object (instantiated from ECart class) in the session object, as appropriate.

 

Use the getItems() method of the cart object to access the list of cart items Add back the quantity reserved from each cart item to the loan items QOH.

 

Hint: Call the addToQOH(String, int) method in the ELoan class. Invalidate the session object

 

Note: This will delete the current session ID and start with an empty cart

Forward the control to XYYECart.jsp

 

6. Continue to Test the Loan Cart Web Application

 

Important Note: If you encounter any errors/issues during testing, you must modify your code to resolve all the errors/issues.

 

a. After adding code 101 and code 201 to the cart, click the Return to eLoan link

 

Result:

 

o      Wrox Beginning JSP book has been reserved and QOH is zero. Action column is shown as "N/A".

 

o      Murach Java Servlet and JSP book has been reserved and QOH is 1.

 

b. Click Reserve link for code 201 again.

 

Page 6 of 9


Assignment 3

 

 

Result: The Cart is displayed with a total quantity of 3 books in the cart.

 

 c. Click the Clear the Cart link

 

Result: All cart items are removed from the cart object. The QOH of the loan items list must be updated accordingly.

 

d. Click the Return to eLoan link

 

Result: All items are now available to be reserved with QOH shown in sample screen shot of Task 3.

 

e. Testing an Empty Cart

 

From the Banner JSP page, click on the eCart link.

 

o      Result: Empty Cart (see sample screenshot below) From the eCart page, click on Clear the Cart link.

o      Result: Empty Cart (see sample screenshot below)

 

 

Page 7 of 9


Assignment 3

 

 

f. Build and exercise your own negative test cases

 

E.g., On the Web browser, directly enter a URL to execute Cart servlet without the action parameter.

 

7. Testing Multiple Sessions

 

While the first session (i.e., Web browser) is still open, start another Web browser session.

 

Start | All Programs | Internet Explorer (or FireFox) Run the eLoan page.

 

o      E.g., http://localhost:8084/XXYYClub/XXYYLoan In the second session, click Reserve link

Result:

 

o      The QOH must show the loan items that are still available. Loan items reserved by the first (previous) session cannot be reserved by the second (new) session.

 

Page 8 of 9


Assignment 3

 

 

o      The cart items in the first session are different from the cart items in the second session. This demonstrates the use of the session implicit object.

 

Tip: To reset all the QOH to the original quantities, undeploy the application or restart the Tomcat server.

 

8. Prepare screenshots of your Web Application

 

Create a new A3_screens.docx. under C:\murach\XXYYClub folder.

 

Add at least 5 screenshots similar to the above screenshots (with the title displayed).

 

o     Each screenshot should have an appropriate title.

 

9. Demo your eLoan Web Application

 

Be ready to demo all the features of your assignment solution. Use the above testing steps only as a starting point.

 

 

Assignment Submissions

 

Important: You must submit your code and screenshots (zip file) and upload your solution (XXYYClub.zip). Do not print your code.

 

Sequence                                                                                          Item


 

 

1

2


Print and submit A3_CoverPage*.pdf (total of 4 pages) with all the relevant sections filled in. Otherwise, you will get a zero mark. 50% penalty if Team Log is missing or incomplete.

 

Zip C:\murach\XXYYClub folder and upload your assignment solution (i.e., XXYYClub.zip) to A3_Dropbox on eConestoga’s Assignments module. Upload using the driver’s eConestoga login.

 

Note: Your zip file must be ready to run on NetBeans 8.2 without modifications when unzipped to C:\murach in any Doon Computer Lab. Otherwise you will get 5% penalty per upload-related error (e.g., wrong folder/project name, wrong zip file extension name, wrong NB version, etc.).


 

After you upload your solution, demo your assignment solution on or before the due date. All team members must be present to

3   demo your assignment. The instructor will randomly decide who will demo your solution. The instructor will also randomly ask any member to demo their individual exercises solution.

 

Page 9 of 9

Answer Preview
@SuppressWarnings("UnusedAssignment")
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext loanitems = getServletContext();
        response.setContentType("text/html;charset=UTF-8");
        ECart cart = new ECart();
        ELoan loan = new ELoan();
        String paramValue = request.getParameter("action");
        String paramValue2 = request.getParameter("code");
        if (!paramValue.equalsIgnoreCase(request.getParameter("reserve")))/* && 
                paramValue2.equals(request.getParameter("101")))*/ {
            ArrayList<Book> all = getBooks(getServletContext().getRealPath("/WEB-INF/books.txt"));
            Book b = loan.findItem(all, paramValue2);
            
            cart.addItem(b);
            loan.subtractFromQOH(all, paramValue2, 1);
            
            HttpSession session = request.getSession();
            
            ArrayList<Book> a = cart.getItems();
            request.setAttribute("a", a);
        } else {
            out.println("Nothing to display");
            request.setAttribute("a", "Nothing to display");
        }
        RequestDispatcher dispatcher = getServletContext()
                .getRequestDispatcher("/SBAAECart.jsp");
        dispatcher.forward(request, response);
    }
This is only a preview of the solution. Please use the purchase button to see the entire solution