Java EE 7 Application Developer Exam - Question 1Z0-900

Associate Information with Users in Java EE Shopping Cart Application

Question

Your web application requires logic to remember items that a user placed into a shopping cart.

Which two mechanisms should you use to associate that information with the user? (Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

BC.

To remember the items that a user placed into a shopping cart, you need to associate that information with the user in a persistent way. In Java web applications, there are several mechanisms available to achieve this, but the two most common ones are using HttpSession objects or a database.

  1. HttpSession objects: A HttpSession is an object that represents a user-specific context between the web client and the server. It is a server-side mechanism that allows you to store and retrieve information about a user's interaction with your web application across multiple requests. You can use HttpSession objects to store information about the items that a user places into a shopping cart, and retrieve that information when the user revisits your web application.

Here's how you can use HttpSession objects to remember the items in a shopping cart:

  • When the user adds an item to the shopping cart, store the item's information in the user's HttpSession object.
  • When the user visits the shopping cart page, retrieve the items' information from the user's HttpSession object and display it to the user.
  1. Database: A database is a persistent storage mechanism that allows you to store and retrieve data across different sessions of your web application. You can use a database to store information about the items that a user places into a shopping cart, and retrieve that information when the user revisits your web application.

Here's how you can use a database to remember the items in a shopping cart:

  • When the user adds an item to the shopping cart, store the item's information in the database, associating it with the user's identifier.
  • When the user visits the shopping cart page, retrieve the items' information from the database using the user's identifier and display it to the user.

Using HttpSession objects is a simpler approach and doesn't require a separate database. However, it has limitations on the amount of data that can be stored, and the data is lost when the HttpSession expires. Using a database provides a more robust and scalable solution, but it requires more setup and maintenance.