Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professional Java Server Programming J2EE Edition Chapter 22 : Development and Deployment Roles Robert David Cowan CS486 April 9, 2001.

Similar presentations


Presentation on theme: "Professional Java Server Programming J2EE Edition Chapter 22 : Development and Deployment Roles Robert David Cowan CS486 April 9, 2001."— Presentation transcript:

1 Professional Java Server Programming J2EE Edition Chapter 22 : Development and Deployment Roles Robert David Cowan CS486 April 9, 2001

2 Model-View-Controller Design Models Views View1.jsp View2.jsp View3.jsp Controller Main.jsp Browser Client

3 Manufacturing App Design Models ManageOrdersManufacture ModelManager (proxy) Views createproduct.jsp createrouting.jsp ….and others Controller RequestProcessor Main.jsp Browser Client

4 Choices.jsp Choose one of the following actions: Create a sample product Place a sample order Manage orders Manufacture a product for an order

5 Code for Choices.jsp Choices Wrox Sample Code / J2EE Choose one of the following actions: Create a sample product Place a sample order Manage orders Manufacture a product for an order

6 Code for Main.jsp <jsp:useBean id="modelManager" class="ModelManager" scope="session" > <% modelManager.init(config.getServletContext(), session); %> <jsp:useBean id="rp" class="RequestProcessor" scope="session" > <% rp.init(config.getServletContext(), session); %> Model and request processor @ session scope.

7 Code for Main.jsp (cont.) Forward request to RequestProcessor.java (class) = update model and return next view. <% String targetView = rp.processRequest(request); Dispatch the request to the appropriate view. getServletConfig().getServletContext(). getRequestDispatcher(targetView).forward(request, response); %>

8 Code for RequestProcessor.java class import java.text.DateFormat; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import factory.manage_orders.NoSuchOrderException; import factory.manage_orders.NoSuchProductException; import factory.manage_orders.DuplicateOrderException; import factory.manufacture.BadStatusException; import factory.order.OrderNotCancelableException; public class RequestProcessor { private ModelManager mm; private HttpSession session; private ServletContext context; private String stackURL; private DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);

9 Code for RequestProcessor.java (cont.) public void init(ServletContext context, HttpSession session) { this.session = session; this.context = context; mm = (ModelManager)session.getAttribute("modelManager"); } public String processRequest(HttpServletRequest req) { String selectedURL = req.getPathInfo(); if ((selectedURL == null) || selectedURL.equals( ScreenNames.CHOICES )) { return ScreenNames.CHOICES_URL; } else if (selectedURL.equals( ScreenNames.CHOOSE_FOR_MANUFACTURE )) { String cellName = mm.getCurrentCell(); if (cellName == null) { // requires "log on" stackURL = ScreenNames.CHOOSE_FOR_MANUFACTURE_URL; return ScreenNames.CHOOSE_CELL_URL; } Copy model proxy Processing request

10 Code for RequestProcessor.java (cont.) } else if (selectedURL.equals( ScreenNames.CELL_CHOSEN )) { String cellName = req.getParameter( ScreenNames.CELL_PARAM ); mm.setCurrentCell( cellName ); return stackURL; } else if (selectedURL.equals( ScreenNames.PRODUCT_CREATED )) { String prodID = req.getParameter( ScreenNames.PRODUCT_ID_PARAM ); String prodName = req.getParameter( ScreenNames.PRODUCT_NAME_PARAM ); mm.createProduct( prodID, prodName ); return ScreenNames.CREATE_ROUTING_URL; } else if (selectedURL.equals( ScreenNames.CREATE_ROUTING )) { return ScreenNames.CREATE_ROUTING_URL; } else if (selectedURL.equals( ScreenNames.ROUTING_CREATED )) { String sequence = req.getParameter( ScreenNames.ROUTING_SEQUENCE_PARAM ); String action = req.getParameter( ScreenNames.ROUTING_ACTION_STEP_PARAM ); mm.addRouting( Integer.parseInt(sequence), action ); return ScreenNames.CREATE_ROUTING_URL;

11 Code for RequestProcessor.java (cont.) } else if (selectedURL.equals( ScreenNames.CANCEL_ORDER )) { String salesDivision = req.getParameter( ScreenNames.SALES_DIVISION_PARAM ); String orderNumber = req.getParameter( ScreenNames.ORDER_NUMBER_PARAM ); String orderType = (req.getParameter(ScreenNames.ORDER_TYPE_PARAM)); try { System.out.println("Request Processor cancel order"); mm.cancelOrder( Integer.parseInt(salesDivision), Integer.parseInt(orderNumber) ); prepareManageOrdersRequest( orderType, req ); return ScreenNames.MANAGE_ORDERS_URL; } catch (OrderNotCancelableException once) { req.setAttribute( ScreenNames.MESSAGE_ATTRIB, "This order is not cancelable." ); return ScreenNames.MESSAGE_URL; } catch (NoSuchOrderException nsoe) { req.setAttribute( ScreenNames.MESSAGE_ATTRIB, "This order does not exist." ); return ScreenNames.MESSAGE_URL; }

12 Code for RequestProcessor.java (cont.) } else if (selectedURL.equals( ScreenNames.MANAGE_ORDERS )) { String orderType = (req.getParameter(ScreenNames.ORDER_TYPE_PARAM)); prepareManageOrdersRequest( orderType, req ); return ScreenNames.MANAGE_ORDERS_URL; } else if (selectedURL.equals( ScreenNames.PLACE_ORDER )) { return ScreenNames.PLACE_ORDER_URL; } else if (selectedURL.equals( ScreenNames.ORDER_PLACED )) { try { String salesDiv = req.getParameter( ScreenNames.ORDER_SALES_DIV_PARAM ); String orderNum = req.getParameter( ScreenNames.ORDER_NUM_PARAM ); String productID = req.getParameter( ScreenNames.ORDER_PROD_PARAM ); String dateDueString = req.getParameter( ScreenNames.ORDER_DUE_DATE_PARAM ); Date dateDue = dateFormat.parse( dateDueString ); mm.placeOrder(Integer.parseInt(salesDiv), Integer.parseInt(orderNum), productID, dateDue );

13 Code for RequestProcessor.java (cont.) req.setAttribute(ScreenNames.MESSAGE_ATTRIB, "Thank you for placing this order." ); } catch (NoSuchProductException nspe) { req.setAttribute( ScreenNames.MESSAGE_ATTRIB, "There is no such product." ); } catch (DuplicateOrderException doe) { req.setAttribute( ScreenNames.MESSAGE_ATTRIB, "There is already an order in that sales division with that number." ); } catch (java.text.ParseException pe) { req.setAttribute( ScreenNames.MESSAGE_ATTRIB, "That is not a valid date." ); } return ScreenNames.MESSAGE_URL; } else if (selectedURL.equals( ScreenNames.ROUTE_FOR_MANUFACTURE )) { if (mm.hasNextRouting()) return ScreenNames.ROUTE_FOR_MANUFACTURE_URL; else return ScreenNames.SHIP_URL; } else if (selectedURL.equals( ScreenNames.SHIP_PRODUCT )) { String loadingDock = req.getParameter(

14 Code for RequestProcessor.java (cont.) String loadingDock = req.getParameter( ScreenNames.SHIP_LOADING_DOCK_PARAM ); String carrier = req.getParameter(ScreenNames.SHIP_METHOD_PARAM ); mm.shipProduct( carrier, Integer.parseInt(loadingDock) ); return ScreenNames.CHOICES_URL; } else { return ScreenNames.CHOICES_URL; }

15 Code for RequestProcessor.java (cont.) prepareManageOrdersRequest private void prepareManageOrdersRequest(String orderType, HttpServletRequest req) { if (orderType.equals(ScreenNames.ORDER_TYPE_OVERDUE)) { req.setAttribute(ScreenNames.ORDER_URL_ATTRIB, ScreenNames.ORDER_TYPE_OVERDUE); req.setAttribute(ScreenNames.ORDER_ALT_URL_ATTRIB, ScreenNames.ORDER_TYPE_OPEN); req.setAttribute(ScreenNames.ORDER_ALT_VIEW_ATTRIB, ScreenNames.ORDER_TYPE_OPEN_TEXT); req.setAttribute(ScreenNames.ORDER_VIEW_ATTRIB, ScreenNames.ORDER_TYPE_OVERDUE_TEXT); Helper method to abstract out some functionality used twice

16 Code for RequestProcessor.java (cont.) prepareManageOrdersRequest } else // orderType.equals(ScreenNames.ORDER_TYPE_OPEN) ) { req.setAttribute(ScreenNames.ORDER_URL_ATTRIB, ScreenNames.ORDER_TYPE_OPEN); req.setAttribute(ScreenNames.ORDER_ALT_URL_ATTRIB, ScreenNames.ORDER_TYPE_OVERDUE); req.setAttribute(ScreenNames.ORDER_ALT_VIEW_ATTRIB, ScreenNames.ORDER_TYPE_OVERDUE_TEXT); req.setAttribute(ScreenNames.ORDER_VIEW_ATTRIB, ScreenNames.ORDER_TYPE_OPEN_TEXT); }

17 Code for ScreenNames.java class public interface ScreenNames { // paths public static final String CHOICES = "/choices"; public static final String CREATE_PRODUCT = "/createproduct"; public static final String CREATE_ROUTING = "/createrouting"; public static final String MANAGE_ORDERS = "/manageorders"; public static final String CHOOSE_FOR_MANUFACTURE = "/manufacturechoose"; public static final String ROUTE_FOR_MANUFACTURE = "/manufactureroute"; public static final String PLACE_ORDER = "/placeorder"; public static final String ORDER_PLACED = "/order_placed"; public static final String PRODUCT_CREATED = "/product_created"; public static final String ROUTING_CREATED = "/routing_created"; public static final String ORDER_CHOSEN = "/order_chosen"; public static final String CANCEL_ORDER = "/cancelorder"; public static final String CELL_CHOSEN = "/cell_chosen"; public static final String SHIP_PRODUCT = "/ship_product"; Constants defined

18 Code for ScreenNames.java (cont.) // jsps public static final String CHOICES_URL = "/choices.jsp"; public static final String CREATE_PRODUCT_URL = "/createproduct.jsp"; public static final String CREATE_ROUTING_URL = "/createrouting.jsp"; public static final String MANAGE_ORDERS_URL = "/manageorders.jsp"; public static final String CHOOSE_FOR_MANUFACTURE_URL = "/manufacturechoose.jsp"; public static final String ROUTE_FOR_MANUFACTURE_URL = "/manufactureroute.jsp"; public static final String PLACE_ORDER_URL = "/placeorder.jsp"; public static final String MESSAGE_URL = "/message.jsp"; public static final String CHOOSE_CELL_URL = "/cellid.jsp"; public static final String SHIP_URL = "/ship.jsp";

19 Code for ScreenNames.java (cont.) // parameters public static final String ORDER_TYPE_PARAM = "ordertype"; public static final String ORDER_VIEW_ATTRIB = "order_view"; public static final String ORDER_ALT_VIEW_ATTRIB = "order_alt_view"; public static final String ORDER_ALT_URL_ATTRIB = "order_alt_url"; public static final String ORDER_URL_ATTRIB = "order_url"; public static final String ORDER_TYPE_OPEN = "openorders"; public static final String ORDER_TYPE_OVERDUE = "overdueorders"; public static final String ORDER_TYPE_OPEN_TEXT = "open orders"; public static final String ORDER_TYPE_OVERDUE_TEXT = "overdue orders"; public static final String SALES_DIVISION_PARAM = "salesdivision"; public static final String ORDER_NUMBER_PARAM = "ordernumber"; public static final String MESSAGE_ATTRIB = "message";

20 Code for ScreenNames.java (cont.) public static final String PRODUCT_ID_PARAM = "product_id"; public static final String PRODUCT_NAME_PARAM = "product_name"; public static final String ROUTING_SEQUENCE_PARAM = "sequence"; public static final String ROUTING_ACTION_STEP_PARAM = "routing"; public static final String ORDER_SALES_DIV_PARAM = "sales_div"; public static final String ORDER_NUM_PARAM = "order_num"; public static final String ORDER_PROD_PARAM = "prod"; public static final String ORDER_DUE_DATE_PARAM = "due_date"; public static final String CELL_PARAM = "cell"; public static final String SHIP_METHOD_PARAM = "shipping_company"; public static final String SHIP_LOADING_DOCK_PARAM = "loading_dock"; }

21 Code for ModelManager.java class import javax.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import javax.servlet.http.HttpSession; import javax.servlet.ServletContext; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import factory.manage_orders.DuplicateOrderException; import factory.manage_orders.OpenOrderView; import factory.manage_orders.OverdueOrderView; import factory.manage_orders.ManageOrders; import factory.manage_orders.ManageOrdersHome; import factory.manage_orders.NoSuchOrderException; import factory.manage_orders.NoSuchProductException; import factory.manufacture.BadStatusException; import factory.manufacture.Manufacture; import factory.manufacture.ManufactureHome; import factory.order.OrderNotCancelableException; Web-tier proxy for EJB-tier

22 Code for ModelManager.java (cont.) public class ModelManager { private ServletContext context; private HttpSession session; Maintain references to session bean façade. Save persistent Manufacture (stateful) ManageOrder could be reaquired each time for load balancing. private ManageOrders manageOrders; private Manufacture manufacture; private String currentCellID; private String currentProductID; public void init(ServletContext context, HttpSession session) { this.session = session; this.context = context; manageOrders = getManageOrdersEJB(); }

23 Code for ModelManager.java (cont.) public void createProduct( String productID, String productName ) { try { manageOrders.createProduct( productID, productName ); currentProductID = productID; } catch (java.rmi.RemoteException re ) { throw new EJBException( re ); } public String getCurrentCell() { return currentCellID; } public void setCurrentCell( String currentCell ) { currentCellID = currentCell; } public String getCurrentProductID() { return currentProductID; }

24 Code for ModelManager.java (cont.) public void addRouting( int sequence, String action ) { try { manageOrders.addRoutingInstruction( currentProductID, sequence, action ); } catch (java.rmi.RemoteException re ) { throw new EJBException( re ); } public void placeOrder(int salesDivision, int orderNumber, String product, Date dateDue ) throws NoSuchProductException, DuplicateOrderException { try { manageOrders.placeOrder( salesDivision, orderNumber, product, dateDue ); } catch (java.rmi.RemoteException re ) { throw new EJBException( re ); }

25 Code for ModelManager.java (cont.) public void cancelOrder( int salesDivision, int orderNumber ) throws NoSuchOrderException, OrderNotCancelableException { try { manageOrders.cancelOrder( salesDivision, orderNumber ); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); } public synchronized Iterator getOrdersToManufacture() { try { LinkedList list = new LinkedList(); manufacture = getManufactureEJB(); OpenOrderView[] openOrders = manufacture.getOpenOrders(); for (int iter=0; iter<openOrders.length; iter++) { list.add( new OrderView(openOrders[iter]) ); } return list.iterator(); } catch (java.rmi.RemoteException re ) { throw new EJBException( re ); } synchronization of stateful session bean else illegal multiple concurrent access, stateless does not need it as each access would be directed to a different EJB instance

26 Code for ModelManager.java (cont.) public synchronized void selectForManufacture(int salesDiv, int orderNum ) throws NoSuchOrderException, BadStatusException { try { manufacture.selectForManufacture( salesDiv, orderNum ); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); } public synchronized boolean hasNextRouting() { try { return manufacture.hasNextRouting(); } catch (factory.manufacture.NoSelectionException nse) { throw new EJBException( nse ); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); }

27 Code for ModelManager.java (cont.) public synchronized String getNextRouting() { try { return manufacture.getNextRouting(); } catch (factory.manufacture.NoSelectionException nse) { throw new EJBException( nse ); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); } public synchronized void shipProduct( String carrier, int loadingDock ) { try { manufacture.ship( carrier, loadingDock ); } catch (factory.manufacture.NoSelectionException nse) { throw new EJBException( nse ); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); }

28 Code for ModelManager.java (cont.) public Iterator getOrders(String type) { try { LinkedList list = new LinkedList(); if (type.equals( ScreenNames.ORDER_TYPE_OPEN_TEXT )) { OpenOrderView[] openOrders = manageOrders.getSchedulableOrders(); for (int iter=0; iter<openOrders.length; iter++) { list.add( new OrderView(openOrders[iter]) ); } } else if (type.equals( ScreenNames.ORDER_TYPE_OVERDUE_TEXT )) { OverdueOrderView[] overdueOrders = manageOrders.getOverdueOrders(); for (int iter=0; iter<overdueOrders.length; iter++) { list.add( new OrderView(overdueOrders[iter]) ); } } else throw new IllegalStateException(); return list.iterator(); } catch (java.rmi.RemoteException re) { throw new EJBException( re ); }

29 Code for ModelManager.java (cont.) helper methods private ManageOrders getManageOrdersEJB() { try { InitialContext initial = new InitialContext(); Object objref = initial.lookup( "java:comp/env/ejb/ManageOrders" ); ManageOrdersHome home = (ManageOrdersHome) PortableRemoteObject.narrow( objref, ManageOrdersHome.class ); return home.create(); } catch (NamingException ne) { throw new EJBException(ne); } catch (java.rmi.RemoteException re) { throw new EJBException(re); } catch (javax.ejb.CreateException ce) { throw new EJBException(ce); }

30 Code for ModelManager.java (cont.) private Manufacture getManufactureEJB() { try { InitialContext initial = new InitialContext(); Object objref = initial.lookup( "java:comp/env/ejb/Manufacture" ); ManufactureHome home = (ManufactureHome) PortableRemoteObject.narrow( objref, ManufactureHome.class ); return home.create(currentCellID); } catch (NamingException ne) { throw new EJBException(ne); } catch (java.rmi.RemoteException re) { throw new EJBException(re); } catch (javax.ejb.CreateException ce) { throw new EJBException(ce); }

31 View.java class import java.util.Date; import factory.manage_orders.OpenOrderView; import factory.manage_orders.OverdueOrderView; public class OrderView { private int salesDivision; private int orderNumber; private String product; private String status; private Date dateDue; public OrderView(int salesDivision, int orderNumber, String product, String status, Date dateDue ) { this.salesDivision = salesDivision; this.orderNumber = orderNumber; this.product = product; this.status = status; this.dateDue = dateDue; } public OrderView(OpenOrderView view) { this(view.salesDivision, view.orderNumber, view.product, "open", view.dateDue ); } public OrderView(OverdueOrderView view) { this( view.salesDivision, view.orderNumber, view.product, view.status, view.dateDue ); } Returns information from the model

32 View.java (cont.) public OrderView() {} public int getSalesDivision() { return salesDivision; } public int getOrderNumber() { return orderNumber; } public String getProduct() { return product; } public String getStatus() { return status; } public Date getDateDue() { return dateDue; }

33 createrouting.jsp Create a routing for product : Sequence Routing or you can be finished with creating routings for this product.finished with creating routings for this product

34 Code for createrouting.jsp <jsp:useBean id="modelManager" class="ModelManager" scope="session" /> Wrox Sample Code - Create a Routing Create a routing for product : Sequence Routing or you can be finished with creating routings for this product.

35 placeorder.jsp Place an order for a product: Sales division Order number Product Due date

36 Code for placeorder.jsp Wrox Sample Code - Place an Order Place an order for a product: Sales division Order number Product Due date

37 message.jsp Thank you for placing this order. Return to main menu. Thank you message

38 Code for message.jsp Wrox Sample Code - Message. Return to main menu.

39 manageorders.jsp Manage Your Orders You are currently viewing. ">Click here to view. Sales Division Order Number ProductDate Due Click to Cancel ">cancel Return to main menu Overdue Order and Open Order

40 Code for manageorder.jsp <jsp:useBean id="modelManager" class="ModelManager" scope="session" /> Wrox Sample Code - Manage Orders Manage Your Orders You are currently viewing. ">Click here to view.

41 Code for manageorder.jsp (cont.) Sales Division Order Number Product Date Due Click to Cancel <% String orderView = (String) request.getAttribute("order_view"); Iterator iter = modelManager.getOrders( orderView ); while (iter.hasNext()) { OrderView view = (OrderView) iter.next(); %>

42 Code for manageorder.jsp (cont.) <a href="cancelorder?salesdivision=<%=view. getSalesDivision()%>&ordernumber=<%=view.getOrderNumber() %>&ordertype= ">cancel Return to main menu

43 manufacturechoose.jsp Choose an order to manufacture: Sales DivisionOrder NumberProductDate Due

44 Code for manufacturechoose.jsp <jsp:useBean id="modelManager" class="ModelManager" scope="session" /> Wrox Sample Code - Select for Manufacture Choose an order to manufacture:

45 Code for manufacturechoose.jsp (cont.) Sales Division Order Number Product Date Due <% Iterator iter = modelManager.getOrdersToManufacture(); while (iter.hasNext()) { OrderView view = (OrderView) iter.next(); %> <a href="order_chosen?salesdivision=<%=view. getSalesDivision()%>&ordernumber=<%=view.getOrderNumber() %>">

46 cellid.jsp Login: Enter your current manufacturing cell identification string: First time user will asked to log in

47 Code for cellid.jsp Wrox Sample Code - Enter Your Cell ID Login: Enter your current manufacturing cell identification string:

48 manufactureroute.jsp Here is the next step in the manufacture of this product: Solder the lid on the product. Click here when completed.

49 Code for manufactureroute.jsp <jsp:useBean id="modelManager" class="ModelManager" scope="session" /> Wrox Sample Code - Routing Step Here is the next step in the manufacture of this product: Click here when completed.

50 ship.jsp Ship the manufactured product: Shipping company: Loading dock:

51 Code for ship.jsp Wrox Sample Code - Ship the Product Ship the manufactured product: Shipping company: UPS Federal Express US Postal Service Private Carrier Loading dock:

52 index.html Factory Demo for JSPs and EJBs This web site provides a simple interface to the manufacturing example that we developed for the chapters on Enterprise JavaBeans in the Wrox Server Side Java book. See the demo... Welcome screen

53 Code for index.html Wrox Sample Code - Manufacturing Application <body text="#000000" bgcolor="#FFFFFF" link="#0000EE" vlink="#551A8B" alink="#FF0000"> Factory Demo for JSPs and EJBs This web site provides a simple interface to the manufacturing example that we developed for the chapters on Enterprise JavaBeans in the Wrox Server Side Java book. See the demo...

54 Code for web.xml factoryWeb no description entryPoint centralJsp no description main.jsp entryPoint /control/* Web components collected into a JAR with.war (web archive) Archive has web.xml in WEB-INFO directory with welcome, serverlets, mappings and references to EJB

55 Code for web.xml (cont.) /index.html ejb/ManageOrders Session factory.manage_orders.ManageOrdersHome factory.manage_orders.ManageOrders ejb/Manufacture Session factory.manufacture.ManufactureHome factory.manufacture.Manufacture

56 Directory Structure cellid.jps choices.jsp createproduct.jsp createrouting.jsp index.html main.jsp manageorders.jsp manufacturechoose.jsp message.jsp placeorder.jsp ship.jsp WEB-INF/ web.xml classes/ ModelManager.class Orderview.class RequestProcessor.class ScreenNames.class

57 Code for application.xml Factory sample ejbs jsps /factory Web archive added at same level as EJB achive in EAR file. Added to application.xml, assuming EJB JAR in “ejbs” directory and web archive in “jsps”

58 Troubleshooting Tips 1. Place the jsps folder under the orion\applications\factory directory 2. Replace the application.xml file in orion\applications\factory\META-INF 3. Modify the default-web-site.xml file in orion\config to include a new entry for a web app: 4. Start the orion server and browse to http://localhost/factory/ Good Luck with debuggers!

59 Questions ?


Download ppt "Professional Java Server Programming J2EE Edition Chapter 22 : Development and Deployment Roles Robert David Cowan CS486 April 9, 2001."

Similar presentations


Ads by Google