www.pudn.com > 200513174851152(JSP+wml).zip > 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; 
	 
	public void init() throws ServletException 
	{ 
		try 
		{ 
			System.out.println("trying to get initial context"); 
			ic = (InitialContext) getInitialContext(); 
			System.out.println("Got InitContext"); 
			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"); 
			System.out.println("salesmanid: " + salesman_id); 
			 
			String client_id = (String)session.getAttribute("ClientId"); 
			System.out.println("clientid: " + client_id); 
 
			String quantity = (String)req.getParameter("quant"); 
			System.out.println("quant: " + quantity); 
 
			String item_id = (String)session.getAttribute("ItemId"); 
			System.out.println("itemid: " + item_id); 
 
			String maxOrderId = null; 
			Connection conn = ds.getConnection(); 
			//Get the max order id and then calculate the new 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); 
			} 
			System.out.println("length of maxorderid: " + maxOrderId.length()); 
			maxOrderId = maxOrderId.trim(); 
			System.out.println("length of maxorderid: " + maxOrderId.length()); 
 
			long orderId = Long.parseLong(maxOrderId); 
			orderId++; 
			String newOrderId = "" + orderId; 
			System.out.println("order id: " + newOrderId); 
			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 
   { 
    	Properties h = null; 
    	try { 
      // Get an InitialContext 
      	h = new Properties(); 
      	h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); 
	      h.put(Context.PROVIDER_URL, "t3://localhost:7001"); 
      	 
	       
    	}catch(Exception ne) { 
      	System.out.println("Unable to get an initial context"); 
    	} 
		return new InitialContext(h);    
   } 
}