import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import javax.rmi.*; import javax.naming.*; import java.util.*; public class ItemListServlet extends HttpServlet { ItemHome itemHome; public void init() throws ServletException { /* This Servlet functions in a similar manner to the ClientViewServlet. Only in this case it obtains the item list from the Item Entity EJB. Each item is represented by the Item Entity EJB. */ try { System.out.println("trying to get initial context"); Context ic = (InitialContext) getInitialContext(); System.out.println("Got InitContext"); Object objRef = ic.lookup("Item"); System.out.println("Got obj ref"); itemHome = (ItemHome) PortableRemoteObject.narrow(objRef,ItemHome.class); System.out.println("Got Home"); }catch(Exception e) { System.out.println("Error in init"); e.printStackTrace(); } } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try { HttpSession session = req.getSession(false); String clientCode = (String)req.getParameter("client"); /* Store the client code in the session. This code is sent by the ClientDetailsJSP along with the request. */ session.setAttribute("ClientId",clientCode); Collection items = itemHome.findAll(); //find all items. if(items.size() <= 0) //No items for sale { System.out.println("items size: " + items.size()); res.sendRedirect(res.encodeURL("/Status.jsp?code=3")); } Vector itemInfo[] = new Vector[items.size()]; Iterator i = items.iterator(); int cnt=0; /* Structure of each Vector object Item Id - first position in Vector Item Name - second position in Vector Item Description - third position in Vector */ while(i.hasNext()) { Item item = (Item) i.next(); itemInfo[cnt] = new Vector(3); itemInfo[cnt].add(item.getId()); itemInfo[cnt].add(item.getName()); itemInfo[cnt].add(item.getDescription()); cnt++; } session.setAttribute("ItemList",itemInfo); //place the item list in the session. res.sendRedirect(res.encodeURL("/ItemList.jsp")); }catch(Exception e){ System.out.println("Error while obtaining item list:" + e.getMessage()); res.sendRedirect(res.encodeURL("/Status.jsp?code=1")); } } private Context getInitialContext() throws NamingException { Context initial = null; try { /* There are other methods of obtaining the initial context too. Check the J2EE docs and your application server docs for these. */ initial = new InitialContext(); }catch(Exception ne) { System.out.println("Unable to get an initial context"); } return initial; } }