1. Trang chủ
  2. » Công Nghệ Thông Tin

Advanced Java 2 Platform HOW TO PROGRAM phần 7 pps

187 465 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 187
Dung lượng 1,95 MB

Nội dung

1060 Enterprise Java Case Study: Presentation and Controller Logic Chapter 18 LoginServlet uses the Customer EJB to validate the userID and password that the customer entered. Lines 39–44 obtain a reference to the CustomerHome interface. Lines 47–48 invoke CustomerHome method findByLogin, which returns a remote reference to the Customer with the userID and password that the user provided. Once the Customer is found, lines 59–69 build a simple XML document that indicates the customer has successfully logged into the store. Lines 74–82 catch a NamingException if the Customer EJB cannot be found in the JNDI directory. If no Customer is found that matches the userID and password the user entered, lines 85–93 catch a FinderException. Each catch block builds an XML error message for the client to display. Lines 96–98 present the content to the client. 18.5.3 ViewOrderHistoryServlet Registered customers may want to see information about orders they have placed in the past. ViewOrderHistoryServlet (Fig. 18.19) allows customers to see orders they have placed, along with the dates the orders were taken, the total costs of the orders and whether or not the orders were shipped from the warehouse. Fig. 18.18 Fig. 18.18Fig. 18.18 Fig. 18.18 LoginServlet for authenticating registered Customers (part 4 of 4).(Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic 1061 1 // ViewOrderHistoryServlet.java 2 // ViewOrderHistoryServlet presents a list of previous Orders 3 // to the Customer. 4 package com.deitel.advjhtp1.bookstore.servlets; 5 6 // Java core packages 7 import java.io.*; 8 import java.util.*; 9 10 // Java extension packages 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.naming.*; 14 import javax.rmi.*; 15 import javax.ejb.*; 16 17 // third-party packages 18 import org.w3c.dom.*; 19 20 // Deitel packages 21 import com.deitel.advjhtp1.bookstore.model.*; 22 import com.deitel.advjhtp1.bookstore.ejb.*; 23 import com.deitel.advjhtp1.bookstore.exceptions.*; 24 25 public class ViewOrderHistoryServlet extends XMLServlet { 26 27 // respond to HTTP get requests 28 public void doGet( HttpServletRequest request, 29 HttpServletResponse response ) 30 throws ServletException, IOException 31 { 32 Document document = getDocumentBuilder().newDocument(); 33 34 HttpSession session = request.getSession(); 35 String userID = ( String ) 36 session.getAttribute( "userID" ); 37 38 // build order history using Customer EJB 39 try { 40 InitialContext context = new InitialContext(); 41 42 // look up Customer EJB 43 Object object = 44 context.lookup( "java:comp/env/ejb/Customer" ); 45 46 CustomerHome customerHome = ( CustomerHome ) 47 PortableRemoteObject.narrow( 48 object, CustomerHome.class ); 49 50 // find Customer with given userID 51 Customer customer = Fig. 18.19 Fig. 18.19Fig. 18.19 Fig. 18.19 ViewOrderHistoryServlet for viewing customer’s previously placed Orders (part 1 of 4). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) 1062 Enterprise Java Case Study: Presentation and Controller Logic Chapter 18 52 customerHome.findByUserID( userID ); 53 54 // create orderHistory element 55 Element rootNode = ( Element ) document.appendChild( 56 document.createElement( "orderHistory" ) ); 57 58 // get Customer's Order history 59 Iterator orderHistory = 60 customer.getOrderHistory().iterator(); 61 62 // loop through Order history and add XML elements 63 // to XML document for each Order 64 while ( orderHistory.hasNext() ) { 65 OrderModel orderModel = 66 ( OrderModel ) orderHistory.next(); 67 68 rootNode.appendChild( 69 orderModel.getXML( document ) ); 70 } 71 } // end try 72 73 // handle exception when Customer has no Order history 74 catch ( NoOrderHistoryException historyException ) { 75 historyException.printStackTrace(); 76 77 document.appendChild( buildErrorMessage( document, 78 historyException.getMessage() ) ); 79 } 80 81 // handle exception when looking up Customer EJB 82 catch ( NamingException namingException ) { 83 namingException.printStackTrace(); 84 85 String error = "The Customer EJB was not found in " + 86 "the JNDI directory."; 87 88 document.appendChild( buildErrorMessage( 89 document, error ) ); 90 } 91 92 // handle exception when Customer is not found 93 catch ( FinderException finderException ) { 94 finderException.printStackTrace(); 95 96 String error = "The Customer with userID " + userID + 97 " was not found."; 98 99 document.appendChild( buildErrorMessage( 100 document, error ) ); 101 } 102 Fig. 18.19 Fig. 18.19Fig. 18.19 Fig. 18.19 ViewOrderHistoryServlet for viewing customer’s previously placed Orders (part 2 of 4). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic 1063 103 // ensure content is written to client 104 finally { 105 writeXML( request, response, document ); 106 } 107 108 } // end method doGet 109 } Fig. 18.19 Fig. 18.19Fig. 18.19 Fig. 18.19 ViewOrderHistoryServlet for viewing customer’s previously placed Orders (part 3 of 4). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) 1064 Enterprise Java Case Study: Presentation and Controller Logic Chapter 18 Customer EJB method getOrderHistory returns a Collection of the cus- tomer’s previous orders. Lines 51–52 obtain the Customer EJB for the Customer, who must be logged into the bookstore. Lines 59–60 retrieve an Iterator for the customer’s order history. Lines 64–70 loop through the order history and build the XML document to present to the client. If the customer has not placed any orders in our on-line store, method getOrder- History throws a NoOrderHistoryException. Lines 74–79 catch this exception and build an error message to display to the customer. If the CustomerHome interface could not be found or the Customer could not be found in the database, a NamingEx- ception or FinderException is thrown, respectively. Lines 82–101 catch each of these exceptions and construct an error message, using method buildErrorMessage. Line 105 presents the content to the client, using method writeXML. 18.5.4 ViewOrderServlet ViewOrderServlet (Fig. 18.20) displays the details of an order. CheckoutServ- let forwards clients to ViewOrderServlet when a customer places an order. ViewOrderHistoryServlet forwards clients to ViewOrderServlet to present the details of an order that has already been placed. Fig. 18.19 Fig. 18.19Fig. 18.19 Fig. 18.19 ViewOrderHistoryServlet for viewing customer’s previously placed Orders (part 4 of 4). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic 1065 Lines 39–44 obtain a reference to interface OrderHome. Lines 47–48 retrieve the orderID parameter from the request object. Line 51 invokes OrderHome method findByPrimaryKey to obtain a remote reference to the Order with the given orderID. Lines 54–58 get the OrderModel for the Order and append its XML repre- sentation to the XML document. Lines 63–71 catch a NamingException, which is thrown from method lookup if the Order EJB cannot be found in the JNDI directory. Lines 74–82 catch a Finder- Exception, which is thrown by method findByPrimaryKey if an Order with the given orderID is not found. Line 86 presents the XML document to the client, using method writeXML. 1 // ViewOrderServlet.java 2 // ViewOrderServlet presents the contents of a Customer's 3 // Order. 4 package com.deitel.advjhtp1.bookstore.servlets; 5 6 // Java core packages 7 import java.io.*; 8 9 // Java extension packages 10 import javax.servlet.*; 11 import javax.servlet.http.*; 12 import javax.naming.*; 13 import javax.ejb.*; 14 import javax.rmi.*; 15 16 // third-party packages 17 import org.w3c.dom.*; 18 19 // Deitel packages 20 import com.deitel.advjhtp1.bookstore.model.*; 21 import com.deitel.advjhtp1.bookstore.ejb.*; 22 23 public class ViewOrderServlet extends XMLServlet { 24 25 // respond to HTTP get requests 26 public void doGet( HttpServletRequest request, 27 HttpServletResponse response ) 28 throws ServletException, IOException 29 { 30 Document document = getDocumentBuilder().newDocument(); 31 Integer orderID = null; 32 33 // look up Order EJB and get details of Order with 34 // given orderID 35 try { 36 InitialContext context = new InitialContext(); 37 38 // look up Order EJB Fig. 18.20 Fig. 18.20Fig. 18.20 Fig. 18.20 ViewOrderServlet for viewing details of an order (part 1 of 3). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) 1066 Enterprise Java Case Study: Presentation and Controller Logic Chapter 18 39 Object object = 40 context.lookup( "java:comp/env/ejb/Order" ); 41 42 OrderHome orderHome = ( OrderHome ) 43 PortableRemoteObject.narrow( 44 object, OrderHome.class ); 45 46 // get orderID from request object 47 orderID = new Integer( 48 request.getParameter( "orderID" ) ); 49 50 // find Order with given orderID 51 Order order = orderHome.findByPrimaryKey( orderID ); 52 53 // get Order details as an OrderModel 54 OrderModel orderModel = order.getOrderModel(); 55 56 // add Order details to XML document 57 document.appendChild( 58 orderModel.getXML( document ) ); 59 60 } // end try 61 62 // handle exception when looking up Order EJB 63 catch ( NamingException namingException ) { 64 namingException.printStackTrace(); 65 66 String error = "The Order EJB was not found in " + 67 "the JNDI directory."; 68 69 document.appendChild( buildErrorMessage( 70 document, error ) ); 71 } 72 73 // handle exception when Order is not found 74 catch ( FinderException finderException ) { 75 finderException.printStackTrace(); 76 77 String error = "An Order with orderID " + orderID + 78 " was not found."; 79 80 document.appendChild( buildErrorMessage( 81 document, error ) ); 82 } 83 84 // ensure content is written to client 85 finally { 86 writeXML( request, response, document ); 87 } 88 89 } // end method doGet 90 } Fig. 18.20 Fig. 18.20Fig. 18.20 Fig. 18.20 ViewOrderServlet for viewing details of an order (part 2 of 3). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic 1067 18.5.5 GetPasswordHintServlet Registered Customers occasionally forget their passwords. GetPasswordHint- Servlet (Fig. 18.21) provides hints to help Customers remember their passwords. The customer supplies the password hint as part of the registration process. Fig. 18.20 Fig. 18.20Fig. 18.20 Fig. 18.20 ViewOrderServlet for viewing details of an order (part 3 of 3). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) 1068 Enterprise Java Case Study: Presentation and Controller Logic Chapter 18 The hint is stored with other customer registration information in the Customer EJB. Lines 38–47 look up the CustomerHome interface and retrieve the Customer EJB remote reference. Customer EJB method getPasswordHint (line 55) returns the hint the user entered when registering on the site. Line 58 adds the hint to the XML document. In this chapter, we presented the controller and presentation logic for the Deitel Book- store. This controller logic provides an HTTP interface to the business logic objects we present in Chapters 18 and 19. Java servlets provide a robust and flexible controller logic implementation. XSLT presentation logic allows the Deitel Bookstore application to sup- port many different client types without a need for changes in controller logic implementa- tions. In Chapters 19 and 20, we present the business logic for the Deitel Bookstore, using Enterprise JavaBeans. 1 // GetPasswordHintServlet.java 2 // GetPasswordHintServlet allows a customer to retrieve a 3 // lost password. 4 package com.deitel.advjhtp1.bookstore.servlets; 5 6 // Java core packages 7 import java.io.*; 8 9 // Java extension packages 10 import javax.servlet.*; 11 import javax.servlet.http.*; 12 import javax.naming.*; 13 import javax.ejb.*; 14 import javax.rmi.*; 15 16 // third-party packages 17 import org.w3c.dom.*; 18 19 // Deitel packages 20 import com.deitel.advjhtp1.bookstore.model.*; 21 import com.deitel.advjhtp1.bookstore.ejb.*; 22 23 public class GetPasswordHintServlet extends XMLServlet { 24 25 // respond to HTTP get requests 26 public void doGet( HttpServletRequest request, 27 HttpServletResponse response ) 28 throws ServletException, IOException 29 { 30 Document document = getDocumentBuilder().newDocument(); 31 String userID = request.getParameter( "userID" ); 32 33 // get password hint from Customer EJB 34 try { 35 InitialContext context = new InitialContext(); 36 37 // look up Customer EJB 38 Object object = Fig. 18.21 Fig. 18.21Fig. 18.21 Fig. 18.21 GetPasswordHintServlet for viewing a Customer’s password hint (part 1 of 3). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic 1069 39 context.lookup( "java:comp/env/ejb/Customer" ); 40 41 CustomerHome customerHome = ( CustomerHome ) 42 PortableRemoteObject.narrow( object, 43 CustomerHome.class ); 44 45 // find Customer with given userID 46 Customer customer = 47 customerHome.findByUserID( userID ); 48 49 // create passwordHint element in XML document 50 Element hintElement = 51 document.createElement( "passwordHint" ); 52 53 // add text of passwordHint to XML element 54 hintElement.appendChild( document.createTextNode( 55 customer.getPasswordHint() ) ); 56 57 // append passwordHint element to XML document 58 document.appendChild( hintElement ); 59 60 } // end try 61 62 // handle exception when looking up Customer EJB 63 catch ( NamingException namingException ) { 64 namingException.printStackTrace(); 65 66 String error = "The Customer EJB was not found in " + 67 "the JNDI directory."; 68 69 document.appendChild( buildErrorMessage( 70 document, error ) ); 71 } 72 73 // handle exception when Customer is not found 74 catch ( FinderException finderException ) { 75 finderException.printStackTrace(); 76 77 String error = "No customer was found with userID " + 78 userID + "."; 79 80 document.appendChild( buildErrorMessage( 81 document, error ) ); 82 } 83 84 // ensure content is written to client 85 finally { 86 writeXML( request, response, document ); 87 } 88 89 } // end method doGet 90 } Fig. 18.21 Fig. 18.21Fig. 18.21 Fig. 18.21 GetPasswordHintServlet for viewing a Customer’s password hint (part 2 of 3). (Images courtesy Pixo, Inc. or © 2001 Nokia Mobile Phones.) [...]... create new Order using OrderModel and // Customer's userID ShoppingCartEJB implementation of ShoppingCart remote interface (part 5 of 7) 10 82 2 17 21 8 21 9 22 0 22 1 22 2 22 3 22 4 22 5 22 6 22 7 22 8 22 9 23 0 23 1 23 2 23 3 23 4 23 5 23 6 23 7 23 8 23 9 24 0 24 1 24 2 24 3 24 4 24 5 24 6 24 7 24 8 24 9 25 0 25 1 25 2 25 3 25 4 25 5 25 6 25 7 25 8 25 9 26 0 26 1 26 2 26 3 26 4 26 5 26 6 26 7 Fig 19.3 Enterprise Java Case Study: Business Logic Part 1... ShoppingCart remote interface (part 1 of 7) 1 078 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Enterprise Java Case Study: Business Logic Part 1 Chapter 19 import java. text.DateFormat; // Java extension packages import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; //... remote interface (part 3 of 7) 1080 113 114 115 116 1 17 118 119 120 121 122 123 124 125 126 1 27 128 129 130 131 1 32 133 134 135 136 1 37 138 139 140 141 1 42 143 144 145 146 1 47 148 149 150 151 1 52 153 154 155 156 1 57 158 159 160 161 1 62 163 164 Fig 19.3 Enterprise Java Case Study: Business Logic Part 1 Chapter 19 { Iterator iterator = orderProductModels.iterator(); while ( iterator.hasNext() ) { // get... remote interface (part 2 of 7) Chapter 19 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1 02 103 104 105 106 1 07 108 109 110 111 1 12 Fig 19.3 Enterprise Java Case Study: Business Logic Part 1 1 079 } // end while // if Product is not in ShoppingCart, find Product with // given ISBN and add OrderProductModel to ShoppingCart try... remote interface (part 4 of 7) Chapter 19 165 166 1 67 168 169 170 171 1 72 173 174 175 176 177 178 179 180 181 1 82 183 184 185 186 1 87 188 189 190 191 1 92 193 194 195 196 1 97 198 199 20 0 20 1 20 2 20 3 20 4 20 5 20 6 20 7 20 8 20 9 21 0 21 1 21 2 21 3 21 4 21 5 21 6 Fig 19.3 Enterprise Java Case Study: Business Logic Part 1 1081 orderProductModel.getProductModel(); // set quantity for Product with given ISBN if ( productModel.getISBN().equals(... serializing Product data (part 1 of 4) 1090 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 Enterprise Java Case Study: Business Logic Part 1 Chapter 19 // third-party packages import org.w3c.dom.*; public class ProductModel implements Serializable, XMLGenerator { Fig 19.10 // ProductModel properties... String getAuthor() { return author; } ProductModel class for serializing Product data (part 2 of 4) Chapter 19 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1 02 103 104 105 106 1 07 108 109 110 111 1 12 113 114 115 Fig 19.10 Enterprise Java Case Study: Business Logic Part 1 // set title public void setTitle( String productTitle... document.createElement( "product" ); ProductModel class for serializing Product data (part 3 of 4) 1091 10 92 116 1 17 118 119 120 121 122 123 124 125 126 1 27 128 129 130 131 1 32 133 134 135 136 1 37 138 139 140 141 1 42 143 144 145 146 1 47 148 149 150 151 1 52 153 154 155 156 1 57 158 159 160 161 1 62 163 164 165 } Fig 19.10 Enterprise Java Case Study: Business Logic Part 1 Chapter 19 // create ISBN Element Element temp =... calculating the Order’s total cost (part 1 of 2) Chapter 19 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 Enterprise Java Case Study: Business Logic Part 1 1 077 public interface ShoppingCart extends EJBObject { // get contents of ShoppingCart public Collection getContents() throws RemoteException; // add Product with given ISBN to ShoppingCart public void addProduct( String... return; } Iterator iterator = orderProductModels.iterator(); while ( iterator.hasNext() ) { // get next OrderProduct in ShoppingCart OrderProductModel orderProductModel = ( OrderProductModel ) iterator.next(); ProductModel productModel = ShoppingCartEJB implementation of ShoppingCart remote interface (part 4 of 7) Chapter 19 165 166 1 67 168 169 170 171 1 72 173 174 175 176 177 178 179 180 181 1 82 183 184 . "orderHistory" ) ); 57 58 // get Customer's Order history 59 Iterator orderHistory = 60 customer.getOrderHistory().iterator(); 61 62 // loop through Order history and add XML elements 63 // to XML document. Customer has no Order history 74 catch ( NoOrderHistoryException historyException ) { 75 historyException.printStackTrace(); 76 77 document.appendChild( buildErrorMessage( document, 78 historyException.getMessage(). org.w3c.dom.*; 19 20 // Deitel packages 21 import com.deitel.advjhtp1.bookstore.model.*; 22 import com.deitel.advjhtp1.bookstore.ejb.*; 23 import com.deitel.advjhtp1.bookstore.exceptions.*; 24 25 public

Ngày đăng: 09/08/2014, 12:23

TỪ KHÓA LIÊN QUAN