<%@ page contentType="text/vnd.wap.wml" session="true" import="java.util.Vector"%> <% /* The place order obtains the Item code from the index passed to it. It then places this in the session. */ int index = Integer.parseInt(request.getParameter("ind")); Vector itemInfo[] = (Vector[])session.getAttribute("ItemList"); session.setAttribute("ItemId",itemInfo[index].get(1)); %>


Quantity:

PlaceOrder.jsp accepts from the user via an input field the number of items that the salesperson wants to order. It then places an order by calling PlaceOrderServlet. PlaceOrderServlet.java import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import javax.rmi.*; import javax.naming.*; import java.util.*; import java.sql.*; import javax.sql.*; public class PlaceOrderServlet extends HttpServlet { Context ic; OrderHome home; javax.sql.DataSource ds; /* The PlaceOrder servlet first contacts the data source directly and obtains the highest primary key value(i.e. the order id). It then increments this value to obtain the next primary key value. It then creates a new Order Entity EJB, which corresponds to the new order placed. The order consists of the order id, salesman id, client id, item id and the quantity of items ordered. */ public void init() throws ServletException { try { System.out.println("trying to get initial context"); ic = (InitialContext) getInitialContext(); System.out.println("Got InitContext"); /* Lookup the datasource of this application. The name 'wapDB' is recognized by the application server from the configuration files. */ ds = (javax.sql.DataSource)ic.lookup("wapDB"); System.out.println("lookup for ds succeeded"); Object objRef = ic.lookup("Order"); home = (OrderHome)PortableRemoteObject.narrow(objRef,OrderHome.class); System.out.println("Got Home"); }catch(Exception e){ System.out.println("Error in init of PlaceOrderServlet: " + e.getMessage()); e.printStackTrace(); } } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try { HttpSession session = req.getSession(false); String salesman_id = (String)session.getAttribute("SalesmanId"); String client_id = (String)session.getAttribute("ClientId"); String quantity = (String)req.getParameter("quant"); String item_id = (String)session.getAttribute("ItemId"); String maxOrderId = null; Connection conn = ds.getConnection(); //Get the max order id PreparedStatement s = conn.prepareStatement("SELECT MAX(ID) FROM ORDER_INFO"); s.executeQuery(); ResultSet rs = s.getResultSet(); while(rs.next()) { maxOrderId = rs.getString(1); } maxOrderId = maxOrderId.trim(); long orderId = Long.parseLong(maxOrderId); //increment the old max id to obtain the new one. orderId++; String newOrderId = "" + orderId; //create a new Order EJB corresponding to the order just placed. Order order = home.create(newOrderId,quantity,item_id,salesman_id,client_id); String time = order.getTime(); session.setAttribute("OrderId",order.getPrimaryKey()); session.setAttribute("OrderTime",time); res.sendRedirect(res.encodeURL("/Confirm.jsp")); }catch(Exception e) { System.out.println("Error in doGet of PlaceOrderServlet: " + e.getMessage()); e.printStackTrace(); } } 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; } }