cds_jsp.java
来自「100多M的J2EE培训内容」· Java 代码 · 共 216 行
JAVA
216 行
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>\r\n");
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("\r\n");
out.write("\r\n[ Shop for CDs ] ");
out.write("<A HREF=\"toys.jsp\">Shop for Toys");
out.write("</A>\r\n");
out.write("<H1> Online CD Catalog ");
out.write("</H1>\r\n");
out.write("<TABLE>\r\n");
out.write("<TR>");
out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
out.write("</TD>");
out.write("<TD BGCOLOR=AAAAAA>SKU");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=AAAAAA>Decription");
out.write("</TD>");
out.write("<TD BGCOLOR=AAAAAA>Price");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=AAAAAA> ");
out.write("</TD>");
out.write("</TR>\r\n");
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("\r\n ");
out.write("<TR>");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(names[j]);
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(SKUs[j]);
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>Music CD ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(Quantity);
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(prices[j]);
out.write("0");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>\r\n ");
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("\">\r\n Add");
out.write("</A>");
out.write("</TD>");
out.write("</TR>\r\n");
}
out.write("\r\n");
out.write("</TABLE>\r\n");
out.write("<A HREF=cds.jsp?name=emptyCart>Empty Shopping Cart");
out.write("</A>\r\n");
out.write("<HR>\r\n");
out.write("<H1> Content of Shopping Cart ");
out.write("</H1>\r\n");
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("\r\n");
out.write("<TABLE>\r\n");
out.write("<TR>");
out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Name");
out.write("</TD>");
out.write("<TD BGCOLOR=AAAAAA>SKU");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=AAAAAA>Decription");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=AAAAAA>Quantity");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=AAAAAA ALIGN=CENTER>Price");
out.write("</TD>");
out.write("<TD BGCOLOR=AAAAAA> ");
out.write("</TD>\r\n ");
out.write("</TR>\r\n");
java.util.Enumeration items = shoppingCart.getItems();
while(items.hasMoreElements()){
Item item = (Item)items.nextElement();
out.write("\r\n ");
out.write("<TR>");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(item.getName());
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(item.getSku());
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(item.getDescription());
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD>");
out.print(item.getQuantity());
out.write(" ");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
out.print(item.getPrice());
out.write("0");
out.write("</TD>\r\n\r\n ");
out.write("<TD BGCOLOR=DDDDDD>\r\n ");
out.write("<A HREF=\"cds.jsp?name=deleteItem&sku=");
out.print(item.getSku());
out.write("\">\r\n Remove");
out.write("</A>");
out.write("</TD>\r\n");
}
double total = shoppingCart.computeTotal();
out.write("\r\n ");
out.write("<TR>");
out.write("<TD> ");
out.write("</TD>");
out.write("<TD> ");
out.write("</TD>");
out.write("<TD BGCOLOR=\"DDDDDD\">Total");
out.write("</TD>\r\n ");
out.write("<TD BGCOLOR=DDDDDD ALIGN=RIGHT>");
out.print(shoppingCart.computeTotal());
out.write("0\r\n ");
out.write("</TD>");
out.write("</TR>\r\n");
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);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?