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

Java编程

开发平台:

Java

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