Java Reference
In-Depth Information
The
EmptyCart
Service
The
EmptyCart
class, like all other services, is also an implementation of the
Service.execute()
method. Its
execute()
method starts its execution when a user selects
the
Empty Cart
link from the
navigation.jsp
page. It then gets a reference to the user's
ShoppingCart
found in the
HttpSession
and calls the
empty()
method. The process is then
forwarded to the named target on the request, which in this case is the previously listed
index.jsp
. The source for the
EmptyCart
service is in Listing 20.11.
L
ISTING
20.11
EmptyCart.java
import Service;
import ShoppingCart;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
public class EmptyCart implements Service {
public EmptyCart() {
}
/*
implemented method from Service interface
*/
public void execute(HttpServletRequest request,
HttpServletResponse response,
ServletContext context) throws Exception {
// Check the HttpSession for an exist ShoppingCart
HttpSession session = request.getSession();
ShoppingCart cart =
(ShoppingCart)session.getAttribute(“cart”);
if ( cart != null ) {
// If there was an existing cart empty it
cart.empty();
}
}
20
}
The
CheckOut
Service
The
CheckOut
service acts exactly like the
EmptyCart
service, except the result is forwarded to
the
CheckOut.jsp
page. This is where you would complete the process by sending the results