cds_jsp.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:8k
源码类别:
Java编程
开发平台:
Java
- package org.apache.jsp;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.jsp.*;
- import org.apache.jasper.runtime.*;
- import shoppingcart.Item;
- public class cds_jsp extends HttpJspBase {
- private static java.util.Vector _jspx_includes;
- public java.util.List getIncludes() {
- return _jspx_includes;
- }
- public void _jspService(HttpServletRequest request, HttpServletResponse response)
- throws java.io.IOException, ServletException {
- JspFactory _jspxFactory = null;
- javax.servlet.jsp.PageContext pageContext = null;
- HttpSession session = null;
- ServletContext application = null;
- ServletConfig config = null;
- JspWriter out = null;
- Object page = this;
- JspWriter _jspx_out = null;
- try {
- _jspxFactory = JspFactory.getDefaultFactory();
- response.setContentType("text/html;charset=ISO-8859-1");
- pageContext = _jspxFactory.getPageContext(this, request, response,
- null, true, 8192, true);
- application = pageContext.getServletContext();
- config = pageContext.getServletConfig();
- session = pageContext.getSession();
- out = pageContext.getOut();
- _jspx_out = out;
- out.write("<HTML>");
- out.write("<HEAD>");
- out.write("<TITLE>Example of Using Session Scope");
- out.write("</TITLE>");
- out.write("</HEAD>");
- out.write("<BODY>rn");
- shoppingcart.ShoppingCart shoppingCart = null;
- synchronized (session) {
- shoppingCart = (shoppingcart.ShoppingCart) pageContext.getAttribute("shoppingCart", PageContext.SESSION_SCOPE);
- if (shoppingCart == null){
- try {
- shoppingCart = (shoppingcart.ShoppingCart) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "shoppingcart.ShoppingCart");
- } catch (ClassNotFoundException exc) {
- throw new InstantiationException(exc.getMessage());
- } catch (Exception exc) {
- throw new ServletException("Cannot create bean of class " + "shoppingcart.ShoppingCart", exc);
- }
- pageContext.setAttribute("shoppingCart", shoppingCart, PageContext.SESSION_SCOPE);
- }
- }
- out.write("rn");
- out.write("rn[ Shop for CDs ] ");
- out.write("<A HREF="toys.jsp">Shop for Toys");
- out.write("</A>rn");
- out.write("<H1> Online CD Catalog ");
- out.write("</H1>rn");
- out.write("<TABLE>rn");
- out.write("<TR>");
- out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
- out.write("</TD>");
- out.write("<TD BGCOLOR=AAAAAA>SKU");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=AAAAAA>Decription");
- out.write("</TD>");
- out.write("<TD BGCOLOR=AAAAAA>Price");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=AAAAAA> ");
- out.write("</TD>");
- out.write("</TR>rn");
- String[] names = {"Tchaikovsky","Mendelssohn","Haydn","Schumann","Bach"};
- String[] SKUs = {"A111","2B22","33C3","444D","E555"};
- double[] prices = {12.00, 23.50, 34.00, 45.50, 56.00};
- int Quantity=1;
- for(int j=0; j<names.length; j++){
- out.write("rn ");
- out.write("<TR>");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(names[j]);
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(SKUs[j]);
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>Music CD ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(Quantity);
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(prices[j]);
- out.write("0");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>rn ");
- out.write("<A HREF="cds.jsp?name=");
- out.print(names[j]);
- out.write("&sku=");
- out.print(SKUs[j]);
- out.write("&quantity=");
- out.print(Quantity);
- out.write("&price=");
- out.print(prices[j]);
- out.write("">rn Add");
- out.write("</A>");
- out.write("</TD>");
- out.write("</TR>rn");
- }
- out.write("rn");
- out.write("</TABLE>rn");
- out.write("<A HREF=cds.jsp?name=emptyCart>Empty Shopping Cart");
- out.write("</A>rn");
- out.write("<HR>rn");
- out.write("<H1> Content of Shopping Cart ");
- out.write("</H1>rn");
- String name = request.getParameter("name");
- if(name!=null){
- if(name.equals("emptyCart")){
- shoppingCart.emptyCart();
- } else if(name.equals("deleteItem")) {
- String sku = request.getParameter("sku");
- shoppingCart.deleteItem(sku);
- } else {
- String sku = request.getParameter("sku");
- double price = Double.parseDouble(request.getParameter("price"));
- int quantity=1;
- Item newItem = new Item(sku, name, "Music CD",quantity,price);
- shoppingCart.addItem(newItem);
- }}
- out.write("rn");
- out.write("<TABLE>rn");
- out.write("<TR>");
- out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
- out.write("</TD>");
- out.write("<TD BGCOLOR=AAAAAA>SKU");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=AAAAAA>Decription");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=AAAAAA>Quantity");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Price");
- out.write("</TD>");
- out.write("<TD BGCOLOR=AAAAAA> ");
- out.write("</TD>rn ");
- out.write("</TR>rn");
- java.util.Enumeration items = shoppingCart.getItems();
- while(items.hasMoreElements()){
- Item item = (Item)items.nextElement();
- out.write("rn ");
- out.write("<TR>");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(item.getName());
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(item.getSku());
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(item.getDescription());
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD>");
- out.print(item.getQuantity());
- out.write(" ");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
- out.print(item.getPrice());
- out.write("0");
- out.write("</TD>rnrn ");
- out.write("<TD BGCOLOR=DDDDDD>rn ");
- out.write("<A HREF="cds.jsp?name=deleteItem&sku=");
- out.print(item.getSku());
- out.write("">rn Remove");
- out.write("</A>");
- out.write("</TD>rn");
- }
- double total = shoppingCart.computeTotal();
- out.write("rn ");
- out.write("<TR>");
- out.write("<TD> ");
- out.write("</TD>");
- out.write("<TD> ");
- out.write("</TD>");
- out.write("<TD BGCOLOR="DDDDDD">Total");
- out.write("</TD>rn ");
- out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
- out.print(shoppingCart.computeTotal());
- out.write("0rn ");
- out.write("</TD>");
- out.write("</TR>rn");
- out.write("</TABLE>");
- out.write("</BODY>");
- out.write("</HTML>");
- } catch (Throwable t) {
- out = _jspx_out;
- if (out != null && out.getBufferSize() != 0)
- out.clearBuffer();
- if (pageContext != null) pageContext.handlePageException(t);
- } finally {
- if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
- }
- }
- }