1 6 7 package com.sun.javaee.blueprints.autoid; 8 9 import java.io.*; 10 import java.util.*; 11 import java.text.*; 12 13 import javax.servlet.*; 14 import javax.servlet.http.*; 15 import javax.annotation.*; 16 import javax.persistence.*; 17 18 import com.sun.javaee.blueprints.autoid.model.*; 19 20 21 25 public class CatalogServlet extends HttpServlet { 26 private CatalogFacade cf; 27 private ServletContext context; 28 private Map nameSpace; 29 30 public void init(ServletConfig config) throws ServletException { 31 32 context = config.getServletContext(); 33 cf = (CatalogFacade)context.getAttribute("CatalogFacade"); 34 initPathMapping(); 35 } 36 37 public void destroy() { 38 cf = null; 39 } 40 41 42 public void doGet(HttpServletRequest request, 43 HttpServletResponse response) 44 throws ServletException, IOException { 45 response.setContentType("text/html"); 46 String responseURL = null; 47 String fullURL = request.getRequestURI(); 48 49 String selectedURL = null; 51 int lastPathSeparator = fullURL.lastIndexOf("/") + 1; 52 if (lastPathSeparator != -1) { 53 selectedURL = fullURL.substring(lastPathSeparator, fullURL.length()); 54 } 55 responseURL = getResponseURL(selectedURL); 56 try{ 57 if (selectedURL.equals("additem.do")) { 58 String desc = request.getParameter("item_desc"); 59 String name = request.getParameter("item_name"); 60 String listPrice = request.getParameter("item_listprice"); 61 String unitCost = request.getParameter("item_unitcost"); 62 Item item = new Item(); 63 item.setName(name); 64 item.setDescription(desc); 65 item.setListPrice(new Float (listPrice)); 66 item.setUnitCost(new Float (unitCost)); 67 cf.addItem(item); 68 request.setAttribute("result", item.getItemID()); 69 } 70 if (selectedURL.equals("finditem.do")) { 71 String id = request.getParameter("item_id"); 72 Item item = cf.getItem(new Integer (id)); 73 request.setAttribute("item", item); 74 } 75 if (selectedURL.equals("findallitems.do")) { 76 List items = cf.getAllItems(); 77 request.setAttribute("items", items); 78 } 79 } catch (InvalidItemException ixe) { 80 System.err.println("CatalogServlet error: "+ixe); 81 request.setAttribute("error_message", ixe); 82 responseURL = getResponseURL("error.do"); 83 } catch(NumberFormatException nfe){ 84 System.err.println("CatalogServlet Number format exception: "+nfe); 85 request.setAttribute("error_message", nfe); 86 responseURL = getResponseURL("error.do"); 87 } 88 context.getRequestDispatcher(responseURL).forward(request, response); 89 } 90 protected String getResponseURL(String url) { 91 return (String ) nameSpace.get(url); 92 } 93 94 protected void initPathMapping() { 95 nameSpace = new HashMap(); 96 nameSpace.put("enteritemdetails.do", "/add.jsp"); 97 nameSpace.put("enteritemid.do", "/search.jsp"); 98 nameSpace.put("additem.do", "/result.jsp"); 99 nameSpace.put("finditem.do", "/item.jsp"); 100 nameSpace.put("findallitems.do", "/items.jsp"); 101 nameSpace.put("error.do", "/error.jsp"); 102 nameSpace.put("index.do", "/index.jsp"); 103 } 104 } 105 | Popular Tags |