Web App State Management

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Why is it important to keep each customer's data separate in a web shopping transaction?

  • To comply with legal requirements regarding data storage.
  • To ensure that customer data is isolated and not mixed with other customers' data. (correct)
  • To simplify the process of backing up and restoring customer data.
  • To improve server performance by reducing data redundancy.

In a connectionless model, connections are maintained to each server.

False (B)

What is the primary purpose of using cookies in handling state in web applications?

to uniquely identify the transaction

The getSession method returns a ______ object associated with the cookie.

<p>Session</p> Signup and view all the answers

What does the getSession(true) method do if no cookie has been sent by the user?

<p>Creates a new session object and sends its cookie with the response. (B)</p> Signup and view all the answers

The first argument of the putValue method can be any object.

<p>False (B)</p> Signup and view all the answers

What must happen when a getValue method returns an object?

<p>downcast it</p> Signup and view all the answers

When the transaction can be retrieved from the Session object, it must be ______.

<p>downcast</p> Signup and view all the answers

What is a major disadvantage associated with the use of cookies for state management?

<p>Cookies are stored on the user's machine and can be modified or deleted. (A)</p> Signup and view all the answers

Modern sites use cookies over alternative methods.

<p>False (B)</p> Signup and view all the answers

What is one alternative method to using cookies for state management?

<p>transaction identity material</p> Signup and view all the answers

Transaction identity that is included in a form is located in a ______ control.

<p>hidden</p> Signup and view all the answers

In a JSP page, why is a hidden control used to include transaction identity?

<p>To prevent the user from directly modifying the transaction ID. (C)</p> Signup and view all the answers

The value placed in a hidden control by the JSP code can be changed, which makes the value sent to the server immutable.

<p>False (B)</p> Signup and view all the answers

In shopping basket applications, what approach is preferred over using hidden form data for maintaining state?

<p>JavaBean with session scope</p> Signup and view all the answers

A JavaBean that represents a shopping basket is initialized to be ______ when handling the first request.

<p>empty</p> Signup and view all the answers

Each order in a shopping basket application needs to be:

<p>Given a unique ID. (D)</p> Signup and view all the answers

For a shop with a small number of customers, it would be better to create a connection pool with several connection objects.

<p>False (B)</p> Signup and view all the answers

What type will Product class will be.

<p>double</p> Signup and view all the answers

The shopping basket is implemented as a ______.

<p>JavaBean</p> Signup and view all the answers

Match the method with the purpose.

<p>getsession() = Can be applied to the request object returns a Session object associated with the cookie putValue() = Used to place a reference to it in a newly-created session object getValue() = Returns an object of type Object clearBasket() = Does something to clear the basket</p> Signup and view all the answers

In the context of a shopping basket application implemented with JSP and JavaBeans, which of the following is the primary reason for using a non-generic iterator when displaying the basket's contents?

<p>To avoid the need to reference the Product class directly from the JSP page. (A)</p> Signup and view all the answers

The JSP page handling the shopping basket requires two forms: one to clear the basket and one to place the order, and these two forms are submitted to two separate JSP pages.

<p>True (A)</p> Signup and view all the answers

What is the role of the customName variable in the provided code snippet for the shopping basket application?

<p>requests parameter name</p> Signup and view all the answers

To ensure that a shopping basket item's price member is stored accurately, it is recommended to use the ______ data type.

<p>double</p> Signup and view all the answers

Flashcards

Connectionless Model

Each request is treated as a separate, independent activity, without maintaining continuous connections.

Cookies

A small piece of data sent from a website and stored in a user's web browser while the user is browsing that website.

Session

A way to store information about a user across multiple requests.

getSession()

This method returns a Session object associated with the cookie.

Signup and view all the flashcards

putValue()

Adds a key/value pair to the session object, storing data related to the user's transaction.

Signup and view all the flashcards

getValue()

Retrieves a value from the Session object using a given key, allowing access to transaction data.

Signup and view all the flashcards

Hidden Control State Handling

An approach to maintaining state by embedding transaction-specific information within hidden form fields.

Signup and view all the flashcards

JavaBean for Shopping Basket

Use of JavaBean with session scope is preferred when the user is returning to previously visited pages.

Signup and view all the flashcards

Product Class

A Java class representing a product sold in the application.

Signup and view all the flashcards

getSingleton()

A method to get the current instance of shopDB.

Signup and view all the flashcards

getProductCollection()

A method to collect a list of products.

Signup and view all the flashcards

addItem()

A method to add a product to the shopping basket.

Signup and view all the flashcards

getTotal()

A method to get the total cost items in the basket.

Signup and view all the flashcards

basket.jsp

The page used to add items to the basket.

Signup and view all the flashcards

order.jsp

This page clears a basket.

Signup and view all the flashcards

Study Notes

  • CE212 Web Application Programming Part 6

Handling State in Web Applications

  • Web shopping transactions require tracking data across multiple pages.
  • Customer data must be kept separate on the server.
  • The web uses a connectionless model where each request is a separate activity.
  • State handling involves identifying with which transaction a request is associated.

Cookies for State Management

  • Cookies can uniquely identify a transaction.
  • The server sends a cookie with the first HTTP request's response.
  • The browser sends the cookie back with subsequent requests.
  • The servlet API uses the Session class for this purpose.
  • The getSession() method returns a Session object associated with the cookie.
  • Calling getSession(true) creates a new session object and sends its cookie if none exists.
  • Calling getSession(false) returns null if no cookie has been sent.

Creating a Transaction Object

  • When logging in, user and password values can be retrieved from the request.
  • Code can be used to create a transaction object and store it in a session object.
  • Example code:
if (validPassword(user, password)) {
    Session session = request.getSession(true);
    session.putValue("trans", new Transaction(user));
}
  • The putValue method's first argument must be a string, the second can be any object.
  • Transaction is a written class.

Retrieving Transaction Data

  • Subsequent requests can retrieve the transaction from the session object.
  • The object must be downcast, as getValue returns a generic Object type.
  • Example code:
Session session = request.getSession(false);
if (session != null) {
    Transaction t = (Transaction) session.getValue("trans");
    // Update t with request data
} else {
    // Handle case where user is not logged in
}

Disadvantages of Cookies and Alternatives

  • Cookies are stored on the user's machine.
  • Users can modify, delete, or disable cookies.
  • Modern sites use alternative methods to handle state.
  • One approach is to include transaction identity material in a hidden control within each page.
  • When the form is submitted, the invisible transaction identity is sent to the server.

Hidden Form Control Example in JSP

  • A hidden control can be placed on a form in a JSP page.
  • Example HTML:
<form ...>
    <input type="hidden" name="trans" value="<%=transId%>">
</form>
  • The value in the hidden control cannot be seen or changed by the user.
  • The transId string can be a key for a map with the transaction object associated with the session.

Shopping Basket Applications and JavaBeans

  • In shopping basket applications, users may return to previously-visited pages.
  • The hidden form data approach will not work in such scenarios.
  • The preferred approach is to use a JavaBean with session scope.
  • The bean represents a shopping basket and is initialized to be empty upon the first request.
  • Subsequent requests use the same bean; the shopping basket is not reinitialized.

Shopping Basket Example: Implementation Details

  • A Product class with instance variables should correspond to the product table in the shop database.
  • A class can be created to communicate with the database, shared by all bean objects.
  • A static method creates an object only if one does not already exist, otherwise returns the existing object.
  • A single connection object can be created when the object is created.
  • Creating a connection pool is better for a shop with a large number of customers

Order Handling in the Shopping Basket

  • Each order needs a unique id.
  • The customer ID can be stored within the row containing the ordered item.
  • A more realistic database would use a separate table mapping order IDs to customer IDs.
  • The order method returns a boolean indicating if the order was successfully stored.

Java Code for Database Interaction

  • The ShopDB class is responsible for database communication.
  • It uses JDBC to connect to the database.
  • It has a singleton pattern to ensure only one instance exists.
  • It includes methods to retrieve product information from the database.

Retrieving Products

  • The getProduct method retrieves product information from the database.
  • It uses a SQL query to select the product based on its ID.
  • The getProductCollection method executes the query and returns a list of Product objects.

Implementation of getProductCollection

  • The getProductCollection method queries the database.
  • CreatesProduct objects from the row results.
  • Returns Java LinkedList product objects.

Java Bean: Shopping Basket

  • The shopping basket is a Java Bean.
  • The items in the basket are stored as a collection of Product objects.
  • The price member of the Product class is of type double.
  • The shopping basket class includes methods to:
    • Add items
    • Get the total value of the basket
    • Clear the basket

JSP Pages for Shopping Cart

  • basket.jsp adds an item to the basket and displays the updated basket.
  • Employs a non-generic iterator to avoid referencing the Product class in the page.
  • Contains two forms
    • Clear the basket, which submits to the same JSP.
    • Place order which submits to order.jsp
  • Should include a link back to the product listing.

JSP page code

  • Page imports collections and iterates over the Basket object.
<%@ page import="java.util.Collection, java.util.Iterator"%>
<jsp:useBean id='basket' scope='session' class='shop.Basket'/>
  • The basket adds products with the following method.
basket.addItem(request.getParameter("addItem"));

Adding items to the basket

  • Users can "empty basket" or "place order"
  • Includes basic html and form objects

Generating the "order" page

  • Generates the order with custName to persist users details
  • Uses beans to order the database
<jsp:useBean id='basket' scope='session' class='shop.Basket'/>
<jsp:useBean id='db' scope='page' class='shop.ShopDB' />

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

ReactJS State Management
6 questions

ReactJS State Management

AdulatoryHummingbird avatar
AdulatoryHummingbird
State Management in Flutter
10 questions

State Management in Flutter

RespectfulHyperbole avatar
RespectfulHyperbole
Cookies in ASP.NET Quiz
5 questions
Use Quizgecko on...
Browser
Browser