cds_0002ejsp_jsp.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import javax.servlet.jsp.*;
  4. import org.apache.jasper.runtime.*;
  5. import shoppingcart.Item;
  6. public class cds_0002ejsp_jsp extends HttpJspBase {
  7.   private static java.util.Vector _jspx_includes;
  8.   public java.util.List getIncludes() {
  9.     return _jspx_includes;
  10.   }
  11.   public void _jspService(HttpServletRequest request, HttpServletResponse response)
  12.         throws java.io.IOException, ServletException {
  13.     JspFactory _jspxFactory = null;
  14.     javax.servlet.jsp.PageContext pageContext = null;
  15.     HttpSession session = null;
  16.     ServletContext application = null;
  17.     ServletConfig config = null;
  18.     JspWriter out = null;
  19.     Object page = this;
  20.     JspWriter _jspx_out = null;
  21.     try {
  22.       _jspxFactory = JspFactory.getDefaultFactory();
  23.       response.setContentType("text/html;charset=ISO-8859-1");
  24.       pageContext = _jspxFactory.getPageContext(this, request, response,
  25.        null, true, 8192, true);
  26.       application = pageContext.getServletContext();
  27.       config = pageContext.getServletConfig();
  28.       session = pageContext.getSession();
  29.       out = pageContext.getOut();
  30.       _jspx_out = out;
  31.       out.write("<HTML>");
  32.       out.write("<HEAD>");
  33.       out.write("<TITLE>Example of Using Session Scope");
  34.       out.write("</TITLE>");
  35.       out.write("</HEAD>");
  36.       out.write("<BODY>rn");
  37.       shoppingcart.ShoppingCart shoppingCart = null;
  38.       synchronized (session) {
  39.         shoppingCart = (shoppingcart.ShoppingCart) pageContext.getAttribute("shoppingCart", PageContext.SESSION_SCOPE);
  40.         if (shoppingCart == null){
  41.           try {
  42.             shoppingCart = (shoppingcart.ShoppingCart) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "shoppingcart.ShoppingCart");
  43.           } catch (ClassNotFoundException exc) {
  44.             throw new InstantiationException(exc.getMessage());
  45.           } catch (Exception exc) {
  46.             throw new ServletException("Cannot create bean of class " + "shoppingcart.ShoppingCart", exc);
  47.           }
  48.           pageContext.setAttribute("shoppingCart", shoppingCart, PageContext.SESSION_SCOPE);
  49.         }
  50.       }
  51.       out.write("rn");
  52.       out.write("rn[ Shop for CDs ] ");
  53.       out.write("<A HREF="toys.jsp">Shop for Toys");
  54.       out.write("</A>rn");
  55.       out.write("<H1> Online CD Catalog ");
  56.       out.write("</H1>rn");
  57.       out.write("<TABLE>rn");
  58.       out.write("<TR>");
  59.       out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
  60.       out.write("</TD>");
  61.       out.write("<TD BGCOLOR=AAAAAA>SKU");
  62.       out.write("</TD>rn    ");
  63.       out.write("<TD BGCOLOR=AAAAAA>Decription");
  64.       out.write("</TD>");
  65.       out.write("<TD BGCOLOR=AAAAAA>Price");
  66.       out.write("</TD>rn    ");
  67.       out.write("<TD BGCOLOR=AAAAAA>&nbsp;");
  68.       out.write("</TD>");
  69.       out.write("</TR>rn");
  70.  String[] names = {"Tchaikovsky","Mendelssohn","Haydn","Schumann","Bach"};
  71.    String[] SKUs  = {"A111","2B22","33C3","444D","E555"};
  72.    double[] prices = {12.00, 23.50, 34.00, 45.50, 56.00};
  73.    int Quantity=1;
  74.    for(int j=0; j<names.length; j++){ 
  75.       out.write("rn     ");
  76.       out.write("<TR>");
  77.       out.write("<TD BGCOLOR=DDDDDD>");
  78.       out.print(names[j]);
  79.       out.write("  ");
  80.       out.write("</TD>rn         ");
  81.       out.write("<TD BGCOLOR=DDDDDD>");
  82.       out.print(SKUs[j]);
  83.       out.write("   ");
  84.       out.write("</TD>rn         ");
  85.       out.write("<TD BGCOLOR=DDDDDD>Music CD       ");
  86.       out.write("</TD>rn         ");
  87.       out.write("<TD BGCOLOR=DDDDDD>");
  88.       out.print(Quantity);
  89.       out.write("   ");
  90.       out.write("</TD>rn         ");
  91.       out.write("<TD BGCOLOR=DDDDDD>");
  92.       out.print(prices[j]);
  93.       out.write("0");
  94.       out.write("</TD>rn         ");
  95.       out.write("<TD BGCOLOR=DDDDDD>rn             ");
  96.       out.write("<A HREF="cds.jsp?name=");
  97.       out.print(names[j]);
  98.       out.write("&sku=");
  99.       out.print(SKUs[j]);
  100.       out.write("&quantity=");
  101.       out.print(Quantity);
  102.       out.write("&price=");
  103.       out.print(prices[j]);
  104.       out.write("">rn              Add");
  105.       out.write("</A>");
  106.       out.write("</TD>");
  107.       out.write("</TR>rn");
  108.  } 
  109.       out.write("rn");
  110.       out.write("</TABLE>rn");
  111.       out.write("<A HREF=cds.jsp?name=emptyCart>Empty Shopping Cart");
  112.       out.write("</A>rn");
  113.       out.write("<HR>rn");
  114.       out.write("<H1> Content of Shopping Cart ");
  115.       out.write("</H1>rn");
  116.  String name = request.getParameter("name");
  117.    if(name!=null){
  118.      if(name.equals("emptyCart")){
  119.        shoppingCart.emptyCart();
  120.      } else if(name.equals("deleteItem")) {
  121.        String sku = request.getParameter("sku");
  122.        shoppingCart.deleteItem(sku);
  123.      } else {
  124.        String sku = request.getParameter("sku");
  125.        double price = Double.parseDouble(request.getParameter("price"));
  126.        int quantity=1;
  127.        Item newItem = new Item(sku, name, "Music CD",quantity,price);
  128.        shoppingCart.addItem(newItem);
  129.    }} 
  130.       out.write("rn");
  131.       out.write("<TABLE>rn");
  132.       out.write("<TR>");
  133.       out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
  134.       out.write("</TD>");
  135.       out.write("<TD BGCOLOR=AAAAAA>SKU");
  136.       out.write("</TD>rn    ");
  137.       out.write("<TD BGCOLOR=AAAAAA>Decription");
  138.       out.write("</TD>rn    ");
  139.       out.write("<TD BGCOLOR=AAAAAA>Quantity");
  140.       out.write("</TD>rn    ");
  141.       out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Price");
  142.       out.write("</TD>");
  143.       out.write("<TD BGCOLOR=AAAAAA>&nbsp;");
  144.       out.write("</TD>rn    ");
  145.       out.write("</TR>rn");
  146.  java.util.Enumeration items = shoppingCart.getItems();
  147.    while(items.hasMoreElements()){
  148.      Item item = (Item)items.nextElement(); 
  149.       out.write("rn     ");
  150.       out.write("<TR>");
  151.       out.write("<TD BGCOLOR=DDDDDD>");
  152.       out.print(item.getName());
  153.       out.write("              ");
  154.       out.write("</TD>rn         ");
  155.       out.write("<TD BGCOLOR=DDDDDD>");
  156.       out.print(item.getSku());
  157.       out.write("               ");
  158.       out.write("</TD>rn         ");
  159.       out.write("<TD BGCOLOR=DDDDDD>");
  160.       out.print(item.getDescription());
  161.       out.write("       ");
  162.       out.write("</TD>rn         ");
  163.       out.write("<TD BGCOLOR=DDDDDD>");
  164.       out.print(item.getQuantity());
  165.       out.write("          ");
  166.       out.write("</TD>rn         ");
  167.       out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
  168.       out.print(item.getPrice());
  169.       out.write("0");
  170.       out.write("</TD>rnrn         ");
  171.       out.write("<TD BGCOLOR=DDDDDD>rn             ");
  172.       out.write("<A HREF="cds.jsp?name=deleteItem&sku=");
  173.       out.print(item.getSku());
  174.       out.write("">rn             Remove");
  175.       out.write("</A>");
  176.       out.write("</TD>rn");
  177.  }
  178.    double total = shoppingCart.computeTotal(); 
  179.       out.write("rn     ");
  180.       out.write("<TR>");
  181.       out.write("<TD>&nbsp;");
  182.       out.write("</TD>");
  183.       out.write("<TD>&nbsp;");
  184.       out.write("</TD>");
  185.       out.write("<TD BGCOLOR="DDDDDD">Total");
  186.       out.write("</TD>rn     ");
  187.       out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
  188.       out.print(shoppingCart.computeTotal());
  189.       out.write("0rn     ");
  190.       out.write("</TD>");
  191.       out.write("</TR>rn");
  192.       out.write("</TABLE>");
  193.       out.write("</BODY>");
  194.       out.write("</HTML>");
  195.     } catch (Throwable t) {
  196.       out = _jspx_out;
  197.       if (out != null && out.getBufferSize() != 0)
  198.         out.clearBuffer();
  199.       if (pageContext != null) pageContext.handlePageException(t);
  200.     } finally {
  201.       if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
  202.     }
  203.   }
  204. }