// Purpose. Servlet package article.servlet; import java.util.*; import java.io.*; import javax.rmi.*; // PortableRemoteObject import javax.naming.*; // InitialContext import javax.servlet.*; import javax.servlet.http.*; public class StoreClientServlet extends javax.servlet.http.HttpServlet { private RIStore theStore; private ArrayList theItems; public void init( ServletConfig sc ) { try { super.init( sc ); InitialContext ic = new InitialContext(); Object obj = ic.lookup( "StoreServer" ); theStore = (RIStore) PortableRemoteObject.narrow( obj, RIStore.class ); theItems = theStore.getItems(); } catch (ServletException ex) { ex.printStackTrace(); } catch (NamingException ex ) { ex.printStackTrace(); } catch (java.rmi.RemoteException ex ) { ex.printStackTrace(); } } public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { PrintWriter pw = resp.getWriter(); pw.println( "StoreClientServlet" ); HttpSession session = req.getSession(); // get the array of choices stored in the session object ArrayList theChoices = (ArrayList) session.getAttribute( "theChoices" ); // get the text input element named "choice" String choice = req.getParameter( "choice" ); // if this is the first request for this page if (theChoices == null) { theChoices = new ArrayList(); session.setAttribute( "theChoices", theChoices ); // remember the current selection } else if ( ! choice.equals("0")) { theChoices.add( new Integer( Integer.parseInt(choice) - 1 ) ); session.setAttribute( "theChoices", theChoices ); // time to place the order } else { StringBuffer response = new StringBuffer( theStore.placeOrder( theChoices ) ); formatOrderConfirmation( response ); pw.println( response.toString() ); pw.println( "

Top" ); session.invalidate(); return; } // always output the base form pw.println( "

" ); pw.println( "Select from (0 when done):
    " ); Iterator it = theItems.iterator(); for (int i=1; it.hasNext(); i++) pw.println( "
  1. " + it.next() ); pw.println( "
Choice:" ); pw.println( "" ); pw.println( "" ); // add an extra