KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > javaee > blueprints > autoid > CatalogServlet


1 /*
2  * Copyright 2006 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
3  * http://developer.sun.com/berkeley_license.html
4  * $Id: CatalogServlet.java,v 1.2 2006/06/22 22:18:38 smitha Exp $
5  */

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 /**
22  * This is a simple example of an HTTP Servlet. It responds to the GET
23  * method of the HTTP protocol.
24  */

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 JavaDoc responseURL = null;
47         String JavaDoc fullURL = request.getRequestURI();
48         
49         // get the screen name
50
String JavaDoc 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 JavaDoc desc = request.getParameter("item_desc");
59                 String JavaDoc name = request.getParameter("item_name");
60                 String JavaDoc listPrice = request.getParameter("item_listprice");
61                 String JavaDoc unitCost = request.getParameter("item_unitcost");
62                 Item item = new Item();
63                 item.setName(name);
64                 item.setDescription(desc);
65                 item.setListPrice(new Float JavaDoc(listPrice));
66                 item.setUnitCost(new Float JavaDoc(unitCost));
67                 cf.addItem(item);
68                 request.setAttribute("result", item.getItemID());
69             }
70             if (selectedURL.equals("finditem.do")) {
71                 String JavaDoc id = request.getParameter("item_id");
72                 Item item = cf.getItem(new Integer JavaDoc(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 JavaDoc 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 JavaDoc getResponseURL(String JavaDoc url) {
91         return (String JavaDoc) 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