Podcast
Questions and Answers
Why is it important to keep each customer's data separate in a web shopping transaction?
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.
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?
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.
The getSession
method returns a ______ object associated with the cookie.
What does the getSession(true)
method do if no cookie has been sent by the user?
What does the getSession(true)
method do if no cookie has been sent by the user?
The first argument of the putValue
method can be any object.
The first argument of the putValue
method can be any object.
What must happen when a getValue method returns an object?
What must happen when a getValue method returns an object?
When the transaction can be retrieved from the Session object, it must be ______.
When the transaction can be retrieved from the Session object, it must be ______.
What is a major disadvantage associated with the use of cookies for state management?
What is a major disadvantage associated with the use of cookies for state management?
Modern sites use cookies over alternative methods.
Modern sites use cookies over alternative methods.
What is one alternative method to using cookies for state management?
What is one alternative method to using cookies for state management?
Transaction identity that is included in a form is located in a ______ control.
Transaction identity that is included in a form is located in a ______ control.
In a JSP page, why is a hidden control used to include transaction identity?
In a JSP page, why is a hidden control used to include transaction identity?
The value placed in a hidden control by the JSP code can be changed, which makes the value sent to the server immutable.
The value placed in a hidden control by the JSP code can be changed, which makes the value sent to the server immutable.
In shopping basket applications, what approach is preferred over using hidden form data for maintaining state?
In shopping basket applications, what approach is preferred over using hidden form data for maintaining state?
A JavaBean that represents a shopping basket is initialized to be ______ when handling the first request.
A JavaBean that represents a shopping basket is initialized to be ______ when handling the first request.
Each order in a shopping basket application needs to be:
Each order in a shopping basket application needs to be:
For a shop with a small number of customers, it would be better to create a connection pool with several connection objects.
For a shop with a small number of customers, it would be better to create a connection pool with several connection objects.
What type will Product
class will be.
What type will Product
class will be.
The shopping basket is implemented as a ______.
The shopping basket is implemented as a ______.
Match the method with the purpose.
Match the method with the purpose.
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?
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?
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.
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.
What is the role of the customName variable in the provided code snippet for the shopping basket application?
What is the role of the customName variable in the provided code snippet for the shopping basket application?
To ensure that a shopping basket item's price member is stored accurately, it is recommended to use the ______ data type.
To ensure that a shopping basket item's price member is stored accurately, it is recommended to use the ______ data type.
Flashcards
Connectionless Model
Connectionless Model
Each request is treated as a separate, independent activity, without maintaining continuous connections.
Cookies
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
Session
A way to store information about a user across multiple requests.
getSession()
getSession()
This method returns a Session object associated with the cookie.
Signup and view all the flashcards
putValue()
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()
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
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
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
Product Class
A Java class representing a product sold in the application.
Signup and view all the flashcards
getSingleton()
getSingleton()
A method to get the current instance of shopDB.
Signup and view all the flashcards
getProductCollection()
getProductCollection()
A method to collect a list of products.
Signup and view all the flashcards
addItem()
addItem()
A method to add a product to the shopping basket.
Signup and view all the flashcards
getTotal()
getTotal()
A method to get the total cost items in the basket.
Signup and view all the flashcards
basket.jsp
basket.jsp
The page used to add items to the basket.
Signup and view all the flashcards
order.jsp
order.jsp
This page clears a basket.
Signup and view all the flashcardsStudy 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)
returnsnull
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. - Creates
Product
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.