Listato 1. Il controller definisce il flusso di operazioni per la gestione del carrello della spesa
package it.html.ejb.session.stateful.controller;
import it.html.ejb.session.stateful.ShoppingCart;
import it.html.ejb.session.stateful.ShoppingCartHome;
import it.html.ejb.session.stateful.data.ProductMaker;
import it.html.shop.Product;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private ShoppingCartHome sch;
private Context ctx;
private ProductMaker prods;
public void init(){
prods=new ProductMaker();
try {
ctx=new InitialContext();
sch = (ShoppingCartHome) PortableRemoteObject.narrow(ctx.lookup(ShoppingCartHome.JNDI_NAME), ShoppingCartHome.class);
} catch (NamingException e) {
e.printStackTrace();
}
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String op=request.getParameter("op");
if (op.equalsIgnoreCase("showAll"))
showAllProducts(request,response);
if (op.equalsIgnoreCase("showCart"))
try {
showCart(request,response);
} catch (CreateException e1) {
e1.printStackTrace();
}
if (op.equalsIgnoreCase("addCart"))
try {
addProduct(request,response);
} catch (CreateException e) {
e.printStackTrace();
}
}
//Mostra la lista di prodotti
private void showAllProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Collection list=prods.getList();
//Inoltro la lista alla JSP
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/allProds.jsp");
request.setAttribute("list",list);
rd.forward(request,response);
}
//Crea un carrello anonimo
private void createNewCart(HttpServletRequest request) throws RemoteException, CreateException{
ShoppingCart sc=sch.create();
request.getSession(true).setAttribute("carrello", sc);
}
//Aggiungi un prodotto al carrello
private void addProduct(HttpServletRequest request, HttpServletResponse response) throws CreateException, IOException{
ShoppingCart sc=(ShoppingCart) request.getSession(true).getAttribute("carrello");
//Se non era stato creato, creiamo il carrello
if (sc==null){
createNewCart(request);
sc=(ShoppingCart) request.getSession(true).getAttribute("carrello");
}
// Recupero il prodotto dal gestore e se esiste lo inserisco nel carrello
Product tmp=prods.getProductById(request.getParameter("ID"));
//Facciamo un controllo sul prodotto: ideale era creare un'eccezione apposita
if (tmp==null){
response.getOutputStream().println("Il prodotto selezionato non esiste!");
}
else{
sc.add(tmp);
response.getOutputStream().println("Aggiunto il prodotto "+tmp.getTitle()+" al carrello!");
}
}
//Mostra il carrello della spesa
private void showCart(HttpServletRequest request, HttpServletResponse response) throws CreateException, IOException, ServletException{
ShoppingCart sc=(ShoppingCart) request.getSession(true).getAttribute("carrello");
if (sc==null){
createNewCart(request);
response.getOutputStream().println("Il carrello è vuoto!");
}
else{
//Inoltro il carrello ad una JSP (la vista)
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/cart.jsp");
request.setAttribute("cart",sc);
rd.forward(request,response);
}
}
}