From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../src/examples/shop/web/jsp/catalog.jsp | 44 +++++ .../src/examples/shop/web/jsp/clearQuote.jsp | 15 ++ .../src/examples/shop/web/jsp/emptyQuote.jsp | 15 ++ .../src/examples/shop/web/jsp/error.jsp | 11 ++ .../src/examples/shop/web/jsp/footer.jsp | 5 + .../src/examples/shop/web/jsp/login.jsp | 70 ++++++++ .../src/examples/shop/web/jsp/productInfo.jsp | 32 ++++ .../src/examples/shop/web/jsp/receipt.jsp | 19 ++ .../src/examples/shop/web/jsp/showQuote.jsp | 87 +++++++++ .../src/examples/shop/web/jsp/title.jsp | 8 + .../src/examples/shop/web/jsp/wsf.jsp | 22 +++ .../examples/shop/web/servlet/CatalogServlet.java | 167 +++++++++++++++++ .../examples/shop/web/servlet/LoginServlet.java | 166 +++++++++++++++++ .../shop/web/servlet/ShowQuoteServlet.java | 200 +++++++++++++++++++++ 14 files changed, 861 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/catalog.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/clearQuote.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/emptyQuote.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/error.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/footer.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/login.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/productInfo.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/receipt.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/showQuote.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/title.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/wsf.jsp create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/CatalogServlet.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/LoginServlet.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/ShowQuoteServlet.java (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web') diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/catalog.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/catalog.jsp new file mode 100644 index 0000000..92ccf8d --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/catalog.jsp @@ -0,0 +1,44 @@ +<%-- This JSP Retrieves products vector from HttpSession and displays in table format. + The product details includes name the product and its base price. User can view the + product details by clicking on name of the product. User can also add a product to + the shopping cart by clicking on add to cart link. When user clicks on add to cart + link, item is added to the cart and displays same at the bottom of the screen. + +--%> +<%@ page import="examples.shop.impl.entity.Product" %> + + Jasmine's Catalog page + + +

View Current Shopping Cart

+

Please choose from our selections

+
+ <% + //Retrieves catalog vector from HTTPSession and displays product item details in a table format + session = request.getSession(false); + java.util.Vector products=(java.util.Vector)session.getAttribute("products"); + int size=products.size(); + Product item=null; + for(int i=0; i + + + + + + <%}%> +
<%=item.getName()%><%=item.getBasePrice()%> Add to Cart
+

+ <% + // Displays the name of the product item added to the cart, at the bottom of the page. + String productName = (String) request.getAttribute("ProductPurchased"); + if (productName != null) { + %> + <%=productName%> has been added to your shopping cart. + <% } %> + + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/clearQuote.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/clearQuote.jsp new file mode 100644 index 0000000..0c13d29 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/clearQuote.jsp @@ -0,0 +1,15 @@ +<%-- When user clicks on clear cart button, servlet forwards request to this jsp --%> + + Jasmine's Clear Cart page + + + + You just cleared your Cart! +
  +
+

+ Back to the main page +
+ + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/emptyQuote.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/emptyQuote.jsp new file mode 100644 index 0000000..f0779bd --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/emptyQuote.jsp @@ -0,0 +1,15 @@ +<%-- If shopping cart is empty and user clicks on shopping cart link, servlet forwards request to this jsp --%> + + Jasmine's Empty Cart page + + + Your Cart is empty. +
  +
+
+ Back to the web storefront. +
+ + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/error.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/error.jsp new file mode 100644 index 0000000..47aaca8 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/error.jsp @@ -0,0 +1,11 @@ +<%-- JSP container forwards request to this jsp, if any exceptions in processing --%> + + Jasmine's Error page + + +

Error in processing your request. +

Please contact System Administrator +

Click here to return to the main page. + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/footer.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/footer.jsp new file mode 100644 index 0000000..2df4e29 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/footer.jsp @@ -0,0 +1,5 @@ +<%-- Diplays Footer --%> +


+© 2004-2006, Jasmine's Computer Parts, Inc., All rights reserved. +
+ diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/login.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/login.jsp new file mode 100644 index 0000000..8d773b3 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/login.jsp @@ -0,0 +1,70 @@ +<%-- + This JSP displays a login screen. When the user fills out the login + screen, it will submit it to the Login Servlet, which will verify the + user's credentials by calling EJB components. + + If the verification is unsuccessful, the login servlet will return + the user to this page to re-enter his credentials. + + If the verification is successful, Jasmine's main page will be displayed. +--%> + + + + Jasmine's Login page + + + + +<%-- Include the title, which is "Jasmine's Computer Parts"--%> + + +<%-- Indicate the error page to use if an error occurs --%> + + +<%-- Display the login form --%> +

Please enter login information

+

+

+ + + + + + + + + + + + + +
Name: + +
Password: + +
+ + +
+
+ +<% + // get whether the person logged in successfully + Boolean failed = (Boolean) request.getAttribute("loginFailed"); + if (failed != null) { + if (failed.booleanValue() == true) { +%> +

+ Could not log in! Please try again. +

+<% + } + } +%> + +<%-- Include the page footer --%> + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/productInfo.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/productInfo.jsp new file mode 100644 index 0000000..e18ea8a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/productInfo.jsp @@ -0,0 +1,32 @@ +<%--This JSP displays details of the selected product. + Customer can add this item to the shopping cart by clicking on add to cart link. + Customer has provision to navigate to the catalog as well as to the shopping cart pages. + --%> +<%@ page import="examples.shop.impl.entity.Product" %> + + + Jasmine's Product Info page + + + + + <% + //Retrieves the productItem from request object and displays. + Product item=(Product)request.getAttribute("productItem"); + %> + + <%=item.getName()%> +

Description:

+ <%=item.getDescription()%> +

Base price (before discounts):<%=item.getBasePrice()%>

+ +
+ Add this item to Cart
+ See the Catalog
+ View Current Shopping Cart + + + +
+ + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/receipt.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/receipt.jsp new file mode 100644 index 0000000..448a26b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/receipt.jsp @@ -0,0 +1,19 @@ +<%--This JSP displays order confirmation. When user clicks on order button, + Order will be created in the database and order confirmation number will be + displayed to the customer. + --%> + + Jasmine's Order Confirmation page + + + + <%-- Retrieves Order Id from request object and displays.--%> +

Thank you for shopping with us. +

Your order number is <%=request.getAttribute("orderID")%> +

Please shop with us again soon! +

+

Click here to return to the main page. + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/showQuote.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/showQuote.jsp new file mode 100644 index 0000000..d01abbb --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/showQuote.jsp @@ -0,0 +1,87 @@ +<%-- + This JSP displays details of the products added to the cart. + The details includes the name of the product, quantity, base price, + discount and total price of each line item. It also displays subtotal, + taxes and grand total of the entire cart. Customer has provision to + modify the quantities, order the contents and clear the cart. + +--%> + + Jasmine's Shopping Cart page + + + + <%@ page import="examples.shop.impl.entity.Product" %> + <%@ page import="examples.shop.impl.entity.LineItem" %> + <%@ page import="java.util.List" %> + <%@ page import="java.util.Iterator" %> + <%@ page import="java.text.NumberFormat" %> +

+
+ + + + + + + + <% + // get the lineItems from the request object and displays in a table format. + List lineItems = (List)request.getAttribute("lineItems"); + double subTotal=((Double)request.getAttribute("subTotal")).doubleValue(); + double taxes=((Double)request.getAttribute("taxes")).doubleValue(); + double taxRate=((Double)request.getAttribute("taxRate")).doubleValue(); + double total=((Double)request.getAttribute("total")).doubleValue(); + + for(Iterator iter = lineItems.iterator(); iter.hasNext();) { + LineItem li = iter.next(); + int quantity = li.getQuantity(); + double discount = li.getDiscount(); + Product product = li.getProduct(); + String productID = product.getProductID(); + double basePrice = product.getBasePrice(); + %> + + + + + + + + + <% } %> +
Name Quantity Individual Base Price Discount Total Price
<%=product.getName()%><%=basePrice%><%=discount%><%=basePrice*quantity-discount%>
+
+
+ + + + + + + + + + + + + + + +
Subtotal:<%=subTotal%>
Sales Tax: <%=taxes%>(<%=taxRate%>)%
Grand Total: <%=total%>
+ +

+ See the Catalog +       +       +       + + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/title.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/title.jsp new file mode 100644 index 0000000..afaaea1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/title.jsp @@ -0,0 +1,8 @@ +<%-- Diplays title --%> +


+

+

+

Jasmine's Computer Parts

+
+

+
\ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/wsf.jsp b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/wsf.jsp new file mode 100644 index 0000000..de589f1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/jsp/wsf.jsp @@ -0,0 +1,22 @@ +<%-- + This JSP page displays Jasmine's main screen. + Customers can navigate to the catalog as well as + to the shopping cart page. +--%> + + + + Jasmine's Main page + + + + + +

Catalog

+

Choose from our excellent selection of computer parts.


+

Shopping Cart

+

Look at your shopping cart to see the equipment you've chosen.

+ + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/CatalogServlet.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/CatalogServlet.java new file mode 100644 index 0000000..1a0049a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/CatalogServlet.java @@ -0,0 +1,167 @@ +package examples.shop.web.servlet; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import examples.shop.impl.entity.LineItem; +import examples.shop.impl.entity.Product; +import examples.shop.logic.Cart; +import examples.shop.logic.Catalog; + +/** + * This servlet displays a catalog of products to + * the end-user. + * + * By the time this servlet is called, the user has + * logged in (via the Login servlet), and has a shopping + * cart started (i.e. Cart bean). Since this servlet is + * pooled and re-used for different user requests, the + * servlet code does not store any information specific to + * any user. Rather, we store a reference to the user's + * Cart in the user's HttpSession object, which is + * globally accessible to all servlets. + */ +public class CatalogServlet extends HttpServlet { + + public Catalog catalog; + + /** + * The servlet engine calls this method once to + * initialize a servlet instance. + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + try { + Context ctx = new InitialContext(); + catalog = (Catalog)ctx.lookup(Catalog.class.getName()); + } catch (NamingException e) { + e.printStackTrace(); + } + } + + /** + * The servlet engine calls this method when the user's + * desktop browser sends an HTTP request. + */ + public void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + List products = null; + try { + /* + * Get the user's HttpSession, and from that get + * the user's current cart. + */ + HttpSession session = request.getSession(false); + if (session == null) { + /* + * Redirect user to login page if he + * doesn't have a session. + */ + response.sendRedirect(response + .encodeRedirectURL("/jasmine/login")); + return; + } + + Object obj = session.getAttribute("cart"); + if (obj == null) { + /* + * Redirect user to login page if he + * doesn't have a session. + */ + response.sendRedirect(response + .encodeRedirectURL("/jasmine/login")); + return; + } + Cart cart = (Cart) obj; + String productIDToAdd = request.getParameter("Buy"); + products = (Vector) session.getAttribute("products"); + String productId = (String) request.getParameter("productId"); + + /* + * If user wants to purchase something (via + * the URL parameter 'Buy'), add the desired + * product item to the cart. + */ + + if (productIDToAdd != null) { + /* + * Creates LineItem, and add to the cart. + */ + try { + Product item = getProductItem(products, productIDToAdd); + cart.add(new LineItem(item, 1, 0)); + // Set a flag so that JSP knows which product we purchased + request.setAttribute("ProductPurchased", item.getName()); + // Forwards the request to the catalog JSP + this.getServletContext().getRequestDispatcher( + "/catalog.jsp").forward(request, response); + return; + } catch (Exception e) { + throw new ServletException(e.toString()); + } + } + /* + * If user wants to view the product details (via + * the URL parameter 'productId') + */ + else if (productId != null) { + //Retrieves the product item from the prducts vector and pass it in request object. + request.setAttribute("productItem", getProductItem(products, + productId)); + // Forwards the request to the productInfo JSP + this.getServletContext().getRequestDispatcher( + "/productInfo.jsp").forward(request, response); + return; + } + /* + * If products vector = null,Retrieves productItems vector + * from Catalog stateless session bean and put it in the + * HttpSession. we may need to put this vector in + * application level instead of session. + */ + else { + if (products == null) { + products = catalog.getProductList(); + session.setAttribute("products", products); + } + // Forwards the request to the catalog JSP + this.getServletContext().getRequestDispatcher("/catalog.jsp") + .forward(request, response); + return; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Returns the product for the given product id, if it is in the vector + */ + + private Product getProductItem(List products, String productIDToAdd) { + for (Iterator iter = products.iterator(); iter.hasNext();) { + Product item = iter.next(); + if (item.getProductID().equals(productIDToAdd)) { + return item; + } + } + return null; + } + + public String getServletInfo() { + return "The Catalog servlet adds products to the user's " + + "cart and prints the catalog."; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/LoginServlet.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/LoginServlet.java new file mode 100644 index 0000000..0340fd9 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/LoginServlet.java @@ -0,0 +1,166 @@ +package examples.shop.web.servlet; + +import java.io.IOException; + +import javax.ejb.EJB; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import examples.shop.logic.Cart; +import examples.shop.logic.UserManager; + +/** + * This is the very first servlet the client deals with. It's a Login + * authentication servlet and asks the user for his name and password, + * and pass it to the UserManager stateless session bean for verificatiion. + * + * If the user authenticates properly, a reference to a new Cart is saved + * in his HttpSession object, and the user can begin to add items to his + * cart and shop around. + */ +public class LoginServlet extends HttpServlet { + + /** the user manager used to authenticate the user */ + @EJB + UserManager userManager; + + /** the user's cart object */ + @EJB + Cart cart; + + /** + * The servlet engine calls this method once to initialize a servlet + * instance. + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + try { + /* + * Get the initial context using the above startup params. + */ + + Context ctx = new InitialContext(); + userManager = (UserManager) ctx.lookup(UserManager.class + .getName()); + cart = (Cart) ctx.lookup(Cart.class.getName()); + + } catch (Exception e) { + log(e); + throw new ServletException(e.toString()); + } + } + + /** + * The servlet engine calls this method when the user's desktop browser + * sends an HTTP request. + */ + public void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + /* + * Set up the user's HttpSession + */ + HttpSession session = request.getSession(true); + + System.out.println(request.getAttributeNames().toString()); + /* + * Retrieve the login name / password from the URL string. + */ + String loginName = request.getParameter("Login"); + String password = request.getParameter("Password"); + boolean isLogin = false; + + /* + * If user has not tried to log in yet, present him with the login + * screen. + */ + if ((loginName == null) || (password == null)) { + writeForm(request, response, false); + return; + } else { + /* + * Otherwise, the user has been to this screen already, and has + * entered some information. Verify that information. + */ + try { + isLogin = userManager.validateUser(loginName, password); + } catch (Exception e) { + writeForm(request, response, true); + e.printStackTrace(); + return; + } + /* + * If the passwords match, make a new Cart Session Bean, and add it + * to the user's HttpSession object. When the user navigates to + * other servlets, the other servlets can access the HttpSession to + * get the user's Cart. + */ + if (isLogin) { + try { + cart.setOwner(loginName); + cart.clear(); + session.setAttribute("cart", cart); + + /* + * Call the main page + */ + RequestDispatcher disp = this.getServletContext() + .getRequestDispatcher("/wsf.jsp"); + disp.forward(request, response); + + return; + } catch (Exception e) { + log(e); + throw new ServletException(e.toString()); + } + } else + writeForm(request, response, true); + } + + /* + * If there was no match, the user is not authenticated. Present another + * login screen to him, with an error message indicating that he is not + * authenticated. + */ + writeForm(request, response, true); + } + + /** + * Writes the Login Screen (private use only) + * + * @param showError + * true means show an error b/c client was not authenticated last + * time. + */ + private void writeForm(HttpServletRequest request, + HttpServletResponse response, boolean showError) + throws ServletException, IOException { + + /* + * Set a variable indicating whether or not we failed to log-in. The JSP + * will read this variable. + */ + request.setAttribute("loginFailed", new Boolean(showError)); + + /* + * Forward the request to the login JSP + */ + RequestDispatcher disp = this.getServletContext().getRequestDispatcher( + "/login.jsp"); + disp.forward(request, response); + } + + private void log(Exception e) { + e.printStackTrace(); + } + + public String getServletInfo() { + return "The Login servlet verifies a user."; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/ShowQuoteServlet.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/ShowQuoteServlet.java new file mode 100644 index 0000000..d4449d4 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/shop/web/servlet/ShowQuoteServlet.java @@ -0,0 +1,200 @@ +package examples.shop.web.servlet; + +import java.io.IOException; +import java.util.Enumeration; +import java.util.List; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import examples.shop.impl.entity.LineItem; +import examples.shop.logic.Cart; +import examples.shop.logic.Pricer; + +/** + * This servlet allows the user to view and modify his current selections. + * + * By the time this servlet is called, the user has logged in (via the Login + * servlet), and has a shopping cart started (i.e. cart bean). Since this + * servlet is pooled and re-used for different user requests, the servlet code + * does not store any information specific to any user. Rather, we store a + * reference to the user's cart in the user's HttpSession object, which is + * globally accessible to all servlets. + */ +public class ShowQuoteServlet extends HttpServlet { + + // Pricer for pricing the cart + private Pricer pricer; + + /** + * The servlet engine calls this method once to initialize a servlet + * instance. + * + * In the body of this method, we need to acquire a Pricer EJB Object for + * pricing the carts. + */ + public void init(ServletConfig config) throws ServletException { + /* + * Call parent to store the config object, so that getServletConfig() + * can return it. + */ + super.init(config); + try { + Context ctx = new InitialContext(); + pricer = (Pricer)ctx.lookup(Pricer.class.getName()); + } catch (NamingException e) { + e.printStackTrace(); + } + } + + /** + * The servlet engine calls this method when the user's desktop browser + * sends an HTTP GET request. + */ + public void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + /* + * Get the user's HttpSession, and from that get the user's current + * cart. + */ + HttpSession session = request.getSession(false); + if (session == null) { + /* + * Redirect user to login page if no session + */ + response.sendRedirect(response.encodeRedirectURL("/jasmine/login")); + return; + } + + Object obj = session.getAttribute("cart"); + if (obj == null) { + /* + * Redirect user to login page if no session + */ + response.sendRedirect(response.encodeRedirectURL("/jasmine/login")); + return; + } + + Cart cart = (Cart) obj; + + /* + * If the user clicked the 'Order' button, he wants to purchase his + * selections. We forward the user to the servlet that handles + * purchasing. + */ + if (request.getParameter("Order") != null) { + /* + * First, turn the cart into an order + */ + String orderId = cart.purchase(); + /* + * Stick the orderID in the request so the JSP gets it + */ + request.setAttribute("orderID", orderId); + cart.clear(); + this.getServletContext().getRequestDispatcher("/receipt.jsp") + .forward(request, response); + return; + } + + /* + * Next, we need to figure out what button the user clicked, if any. + * These come to us as form parameters. We need to loop through each + * parameter and interpret it. + */ + Enumeration paramNames = request.getParameterNames(); + while (paramNames.hasMoreElements()) { + String paramName = (String) paramNames.nextElement(); + String paramValue = request.getParameter(paramName); + + /* + * If user clicked 'Update' button, then the user wants to change + * the quantities of each product he is ordering. We'll process + * those quantities below. + */ + if (paramName.equals("Update")) { + } else if (paramName.equals("Clear")) { + /* + * The user wants to clear the form + */ + cart.clear(); + this.getServletContext() + .getRequestDispatcher("/clearQuote.jsp").forward( + request, response); + return; + } else { + /* + * If the parameter represents a quantity of a particular + * product the user is interested in, then we should update that + * product's quantity to reflect this new value. + */ + try { + /* + * Convert the quantity to int format, and set the new + * quantity + */ + int quantity = Integer.parseInt(paramValue); + cart.modify(paramName, quantity); + } catch (NumberFormatException e) { + throw new ServletException("Bad parameter to servlet: " + + paramName + ", " + paramValue); + } catch (Exception e) { + throw new ServletException(e.toString()); + } + } + } + + /* + * Recalculate all totals based upon new quantities + */ + try { + cart.setSubtotal(pricer.priceSubtotal(cart.getOwner(), cart.getAll())); + cart.setTaxes(pricer.priceTaxes(cart.getSubtotal())); + } catch (Exception e) { + log(e); + throw new ServletException(e.toString()); + } + + /* + * Otherwise, show the current cart again + */ + List lineItems = cart.getAll(); + if (lineItems.size() > 0) { + /* + * Stick lineitems, subtotal, taxes and total in request + */ + request.setAttribute("lineItems", lineItems); + request.setAttribute("subTotal", new Double(cart.getSubtotal())); + request.setAttribute("taxes", new Double(cart.getTaxes())); + request.setAttribute("taxRate", new Double(pricer.getTaxRate())); + request.setAttribute("total", new Double(cart.getTotalPrice())); + // Forwards the request to the showQuote JSP. + this.getServletContext().getRequestDispatcher("/showQuote.jsp") + .forward(request, response); + return; + } else { + /* + * If there are no products, print out that the cart is empty. + */ + this.getServletContext().getRequestDispatcher("/emptyQuote.jsp") + .forward(request, response); + return; + } + } + + private void log(Exception e) { + e.printStackTrace(); + } + + public String getServletInfo() { + return "The ShowQuote servlet returns information about" + + "the products that the user is in the process of ordering."; + } +} -- cgit v1.2.3