Exercise

5.1. Use EL and JSTL to refactor mini MVC project in Chapter 2.


5.2 EL is null-friendly. If there is not an attribute named "foo", what will the following output?

${foo}
${7 + foo}
${7 < foo}
${7 == foo}
${7 != foo}
${true and foo}
${not foo}

If there is an attribute named "bar", but that "bar" does not have a property or key named "foo", what will the following output?

${bar[foo]}
${bar.foo}
${foo.bar}

Do you have any conclusion based on the output above?


5.3. How to iterate nested arrays or lists using JSTL?

List<String> programmings = Arrays.asList("C", "C++", "Java", "Python");
List<String> sports = Arrays.asList("Running", "Basketball", "Football");
List<List<String>> skills = new ArrayList<>();
skills.add(programmings);
skills.add(sports);
request.setAttribute("skills", skills);

5.4. What does varStatus means in <c:forEach> tag?

<c:forEach var="book" items="${bookArray}" varStatus="idx">
</c:forEach>

5.5. Develop a book-selling website.

  • Users can log in, and log out.
  • Users information is kept in a file, including name, password, and card.
  • Books information (including ISBN, title, author, cover image, description, and price) are stored in the models.
  • Users who do not log in can only view books.
  • Users who have logged in can add books into, and remove books from their shopping carts.
  • Users can check out their shopping carts by inputting the card number. The website shall display the total price.

In this exercise, both users and books are considered static. Hint: Users and books information can be stored in attributes of context scope for global sharing.


5.6. Based on Exercise 5.5, here are more requirements:

  • There is an admin, whose name and password are stored in the init parameters.
  • A book has the quantity property, and users' purchases can decrease it.
  • Admin can add new books.

Note that it is fine to ignore the thread-safety issue in this exercise.