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

Professional Java Server Programming J2EE Edition

  • Upload
    gari

  • View
    19

  • Download
    4

Embed Size (px)

DESCRIPTION

Professional Java Server Programming J2EE Edition. Chapter 22 : Development and Deployment Roles. Robert David Cowan CS486 April 9, 2001. Model-View-Controller Design. Models. Views View1.jsp View2.jsp View3.jsp. Controller Main.jsp. Browser Client. Manufacturing App Design. - PowerPoint PPT Presentation

Citation preview

Page 1: Professional Java Server Programming J2EE Edition

Professional Java Server Programming J2EE Edition

Chapter 22 : Development and Deployment Roles

Robert David CowanCS486 April 9, 2001

Page 2: Professional Java Server Programming J2EE Edition

Model-View-Controller Design

Model-View-Controller Design

Models

ViewsView1.jsp View2.jsp View3.jsp

ControllerMain.jsp

BrowserClient

Page 3: Professional Java Server Programming J2EE Edition

Manufacturing App DesignManufacturing App Design

ModelsManageOrders Manufacture

ModelManager (proxy)

Viewscreateproduct.jsp createrouting.jsp

….and others

ControllerRequestProcessor

Main.jsp

BrowserClient

Page 4: Professional Java Server Programming J2EE Edition

Choices.jspChoices.jsp

Choose one of the following actions:•Create a sample product •Place a sample order •Manage orders •Manufacture a product for an order  

Page 5: Professional Java Server Programming J2EE Edition

Code for Choices.jsp

Code for Choices.jsp

Choices<html><head><title>Wrox Sample Code / J2EE</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>

<body bgcolor="#FFFFFF"><p>Choose one of the following actions:</p><ul> <li><a href="createproduct">Create a sample product</a></li> <li><a href="placeorder">Place a sample order</a></li> <li><a href="manageorders?ordertype=openorders">Manage orders</a></li> <li><a href="manufacturechoose">Manufacture a product for an order</a></li></ul><p>&nbsp;</p><p>&nbsp; </p></body></html>

Page 6: Professional Java Server Programming J2EE Edition

Code for Main.jspCode for Main.jsp<%-- % The entry point for the manufacturing application --%> <jsp:useBean id="modelManager" class="ModelManager" scope="session" > <% modelManager.init(config.getServletContext(), session); %> </jsp:useBean>

<jsp:useBean id="rp" class="RequestProcessor" scope="session" > <% rp.init(config.getServletContext(), session); %> </jsp:useBean>

Model and request processor @ session scope.

Page 7: Professional Java Server Programming J2EE Edition

Code for Main.jsp (cont.) 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);%>

Page 8: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java

class

Code for RequestProcessor.java

classimport 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);

Page 9: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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

Page 10: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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;

Page 11: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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; }

Page 12: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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 );

Page 13: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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(

Page 14: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

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; } }

Page 15: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

prepareManageOrdersRequest

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

Page 16: Professional Java Server Programming J2EE Edition

Code for RequestProcessor.java (cont.)

prepareManageOrdersRequest

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); } }}

Page 17: Professional Java Server Programming J2EE Edition

Code for ScreenNames.javaclass

Code for ScreenNames.javaclass

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

Page 18: Professional Java Server Programming J2EE Edition

Code for ScreenNames.java (cont.)

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";

Page 19: Professional Java Server Programming J2EE Edition

Code for ScreenNames.java(cont.)

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";

Page 20: Professional Java Server Programming J2EE Edition

Code for ScreenNames.java(cont.)

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";

}

Page 21: Professional Java Server Programming J2EE Edition

Code for ModelManager.javaclass

Code for ModelManager.javaclass 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

Page 22: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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(); }

Page 23: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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; }

Page 24: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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 ); } }

Page 25: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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

Page 26: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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 ); } }

Page 27: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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 ); } }

Page 28: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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 ); } }

Page 29: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

Code for ModelManager.java (cont.)

helper methodsprivate 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); } }

Page 30: Professional Java Server Programming J2EE Edition

Code for ModelManager.java (cont.)

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); } }}

Page 31: Professional Java Server Programming J2EE Edition

View.java

class

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

Page 32: Professional Java Server Programming J2EE Edition

View.java (cont.)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; }}

Page 33: Professional Java Server Programming J2EE Edition

createrouting.jspcreaterouting.jsp

Create a routing for product :Sequence

Routing or you can be finished with creating routings for this product.

Submit

Page 34: Professional Java Server Programming J2EE Edition

Code for createrouting.jspCode for createrouting.jsp

<jsp:useBean id="modelManager" class="ModelManager" scope="session" /><html><head><title>Wrox Sample Code - Create a Routing</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><p>Create a routing for product <%= modelManager.getCurrentProductID()%>:</p><form method="post" action="routing_created"> <p>Sequence <input type="text" name="sequence"></p> <p>Routing <textarea name="routing" cols="75" rows="10"></textarea> </p> <p> <input type="submit" name="Submit" value="Submit"> </p></form><p>or you can be <a href="choices">finished with creating routings for this product</a>.</p></body></html>

Page 35: Professional Java Server Programming J2EE Edition

placeorder.jspplaceorder.jsp

Place an order for a product:Sales division Order number Product Due date  

Submit

Page 36: Professional Java Server Programming J2EE Edition

Code for placeorder.jspCode for placeorder.jsp

<html><head><title>Wrox Sample Code - Place an Order</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><p>Place an order for a product:</p><form method="post" action="order_placed" name="PlaceOrder"> <p>Sales division <input type="text" name="sales_div"></p> <p>Order number <input type="text" name="order_num"></p> <p>Product <input type="text" name="prod"></p> <p>Due date <input type="text" name="due_date"></p> <p><input type="submit" name="Submit" value="Submit"> </p></form><p>&nbsp;</p></body></html>

Page 37: Professional Java Server Programming J2EE Edition

message.jspmessage.jsp

Thank you for placing this order. Return to main menu.

Thank you message

Page 38: Professional Java Server Programming J2EE Edition

Code for message.jspCode for message.jsp

<html><head><title>Wrox Sample Code - Message</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>

<body bgcolor="#FFFFFF"><p><%= request.getAttribute("message")%>. </p>

<p><a href="choices">Return to main menu.</a></p></body></html>

Page 39: Professional Java Server Programming J2EE Edition

manageorders.jspmanageorders.jsp

Manage Your OrdersYou are currently viewing . ">Click here to view .

Sales Division

Order Number

Product Date DueClick to Cancel

">cancel

  Return to main menu

Overdue Order and Open Order

Page 40: Professional Java Server Programming J2EE Edition

Code for manageorder.jspCode for manageorder.jsp<jsp:useBean id="modelManager" class="ModelManager" scope="session" /><%@ page import="java.util.Iterator" %><%@ page import="OrderView" %><html><head><title>Wrox Sample Code - Manage Orders</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><p>Manage Your Orders</p><p>You are currently viewing <%= request.getAttribute("order_view")%>. <a href="manageorders?ordertype=<%= request.getAttribute("order_alt_url")%>">Click here to view <%= request.getAttribute("order_alt_view") %>.</a></p>

Page 41: Professional Java Server Programming J2EE Edition

Code for manageorder.jsp (cont.)

Code for manageorder.jsp (cont.)<table width="87%" border="1">

<tr> <td width="21%">Sales Division</td> <td width="23%">Order Number</td> <td width="19%">Product</td> <td width="16%">Date Due</td> <td width="21%">Click to Cancel</td> </tr> <% String orderView = (String) request.getAttribute("order_view"); Iterator iter = modelManager.getOrders( orderView ); while (iter.hasNext()) { OrderView view = (OrderView) iter.next(); %>

Page 42: Professional Java Server Programming J2EE Edition

Code for manageorder.jsp (cont.)

Code for manageorder.jsp (cont.)<tr>

<td width="21%"><%=view.getSalesDivision()%></td> <td width="23%"><%=view.getOrderNumber()%></td> <td width="19%"><%=view.getProduct()%></td> <td width="16%"><%=view.getDateDue()%></td> <td width="21%"><a href="cancelorder?salesdivision=<%=view. getSalesDivision()%>&ordernumber=<%=view.getOrderNumber() %>&ordertype=<%=request.getAttribute("order_url")%>">cancel</a></td> </tr><% }%></table><p>&nbsp;<a href="choices">Return to main menu</a></p></body></html>

Page 43: Professional Java Server Programming J2EE Edition

manufacturechoose.jspmanufacturechoose.jsp

Choose an order to manufacture:

Sales Division Order Number Product Date Due

Page 44: Professional Java Server Programming J2EE Edition

Code for manufacturechoose.jsp

Code for manufacturechoose.jsp<jsp:useBean

id="modelManager" class="ModelManager" scope="session" /><%@ page import="java.util.Iterator" %><%@ page import="OrderView" %><html><head><title>Wrox Sample Code - Select for Manufacture</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><p>Choose an order to manufacture:</p><table width="87%" border="1">

Page 45: Professional Java Server Programming J2EE Edition

Code for manufacturechoose.jsp (cont.)

Code for manufacturechoose.jsp (cont.)<tr>

<td width="21%">Sales Division</td> <td width="23%">Order Number</td> <td width="19%">Product</td> <td width="16%">Date Due</td> </tr> <% Iterator iter = modelManager.getOrdersToManufacture(); while (iter.hasNext()) { OrderView view = (OrderView) iter.next(); %> <tr> <td width="21%"><%=view.getSalesDivision()%></td> <td width="23%"><a href="order_chosen?salesdivision=<%=view. getSalesDivision()%>&ordernumber=<%=view.getOrderNumber() %>"><%=view.getOrderNumber()%></a></td> <td width="19%"><%=view.getProduct()%></td> <td width="16%"><%=view.getDateDue()%></td> </tr><% }%></table></body></html>

Page 46: Professional Java Server Programming J2EE Edition

cellid.jspcellid.jsp

Login:Enter your current manufacturing cell identification string:   Submit

First time user will asked to log in

Page 47: Professional Java Server Programming J2EE Edition

Code for cellid.jspCode for cellid.jsp<html><head><title>Wrox Sample Code - Enter Your Cell ID</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>

<body bgcolor="#FFFFFF"><p>Login:</p><form method="post" action="cell_chosen"> <p>Enter your current manufacturing cell identification string: <input type="text" name="cell"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p></form><p>&nbsp; </p></body></html>

Page 48: Professional Java Server Programming J2EE Edition

manufactureroute.jspmanufactureroute.jsp

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

Page 49: Professional Java Server Programming J2EE Edition

Code for manufactureroute.jsp

Code for manufactureroute.jsp

<jsp:useBean id="modelManager" class="ModelManager" scope="session" />

<html><head><title>Wrox Sample Code - Routing Step</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>

<body bgcolor="#FFFFFF"><p>Here is the next step in the manufacture of this product:</p><p><jsp:getProperty name="modelManager" property="nextRouting"/></p><p><a href="manufactureroute">Click here when completed.</a></p><p>&nbsp; </p><p>&nbsp; </p></body></html>

Page 50: Professional Java Server Programming J2EE Edition

ship.jspship.jsp

Ship the manufactured product:Shipping company: Loading dock:  

UPS

Submit

Page 51: Professional Java Server Programming J2EE Edition

Code for ship.jspCode for ship.jsp<html><head><title>Wrox Sample Code - Ship the Product</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><p>Ship the manufactured product:</p><form method="post" action="ship_product"> <p>Shipping company: <select name="shipping_company"> <option>UPS</option> <option>Federal Express</option> <option>US Postal Service</option> <option>Private Carrier</option> </select></p> <p>Loading dock: <input type="text" name="loading_dock"></p> <p> <input type="submit" name="Submit" value="Submit"></p></form><p>&nbsp; </p></body></html>

Page 52: Professional Java Server Programming J2EE Edition

index.htmlindex.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

Page 53: Professional Java Server Programming J2EE Edition

Code for index.htmlCode for index.html<html> <head> <title>Wrox Sample Code - Manufacturing Application</title> </head> <body text="#000000" bgcolor="#FFFFFF" link="#0000EE" vlink="#551A8B" alink="#FF0000"> <center> <h1>Factory Demo for JSPs and EJBs</h1> </center> <center><hr width="100%"></center>

<p>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.</p>

<p><a href="control/choices">See the demo...</a> </body></html>

Page 54: Professional Java Server Programming J2EE Edition

Code for web.xmlCode for web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN' 'http://java.sun.com/j2ee/dtds/web-app_2.2.dtd'>

<web-app> <display-name>factoryWeb</display-name> <description>no description</description> <servlet> <servlet-name>entryPoint</servlet-name> <display-name>centralJsp</display-name> <description>no description</description> <jsp-file>main.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>entryPoint</servlet-name> <url-pattern>/control/*</url-pattern> </servlet-mapping>

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

Page 55: Professional Java Server Programming J2EE Edition

Code for web.xml (cont.)Code for web.xml (cont.)

<welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> <ejb-ref> <ejb-ref-name>ejb/ManageOrders</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>factory.manage_orders.ManageOrdersHome</home> <remote>factory.manage_orders.ManageOrders</remote> </ejb-ref> <ejb-ref> <ejb-ref-name>ejb/Manufacture</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>factory.manufacture.ManufactureHome</home> <remote>factory.manufacture.Manufacture</remote> </ejb-ref></web-app>

Page 56: Professional Java Server Programming J2EE Edition

Directory StructureDirectory Structure

cellid.jpschoices.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.classOrderview.classRequestProcessor.classScreenNames.class

Page 57: Professional Java Server Programming J2EE Edition

Code for application.xmlCode for application.xml

<?xml version="1.0"?><!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN" "http://java.sun.com/j2ee/dtds/application_1_2.dtd">

<application><display-name>Factory sample</display-name><module>

<ejb>ejbs</ejb></module><module>

<web><web-uri>jsps</web-uri><context-root>/factory</context-root>

</web></module>

</application>

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”

Page 58: Professional Java Server Programming J2EE Edition

Troubleshooting TipsTroubleshooting 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:

<web-app application="factory" name="jsps" root="/factory" />

4. Start the orion server and browse to http://localhost/factory/

Good Luck with debuggers!

Page 59: Professional Java Server Programming J2EE Edition

Questions ?