📄 shopping_cart_doc_1.txt
字号:
**************shopping_cart项目*****************1.打开jbuilder.2.新建一个shopping_cart_project项目。 选择菜单file->new->project,然后点击[ok]按钮,在出现的界面中输入如下: Name:shopping_cart_project Directory:.....(自己选择合适的目录) 然后点击[next],再[next],[finish]。3.新建web应用 选择菜单file->new->web->web application,然后点击[ok]按钮,在出现的界面中输入如下: Name:cart Directory:cart JSP/Servlet frameworks:选中struts1.1 (前提:在tools->configure libraries中配置了struts1.1),然后点击[ok]按钮4.创建产品表create table cart( id integer primary key, name varchar2(32) not null, description varchar2(64), price float not null);insert into cart values(1,'惠普HP PHOTOSART 245','惠普HP PHOTOSART245',2140.00);insert into cart values(2,'惠普HP OFFICEJET 5510','惠普HP OFFICEJET 5510',2260.00);insert into cart values(3,'惠普HP PHOTOSART 7960','惠普HP PHOTOSART 7960',3130.00);insert into cart values(4,'惠普HP PSC 1350','惠普HP PSC 1350',1499.00);insert into cart values(5,'爱普生 PHOTO 1290 喷墨打印机','爱普生 PHOTO 1290 喷墨打印机',4450.00);insert into cart values(6,'EPSON 925 喷墨打印机','EPSON 925 喷墨打印机',2855.00);insert into cart values(7,'惠普HP LJ2300D','惠普HP LJ2300D',6800.00);insert into cart values(8,'佳能LBP-1210打印机(办公首选)','佳能LBP-1210打印机(办公首选)',2290.00);insert into cart values(9,'佳能MP730打印一体机','佳能MP730打印一体机',4290.00);insert into cart values(10,'佳能Canon i560极速打印机','佳能Canon i560极速打印机',1780.00);insert into cart values(11,'佳能Canon LBP3200商用激光打印机','佳能Canon LBP3200商用激光打印机',2700.00);insert into cart values(12,'联想LJ2500激光打印机','联想LJ2500激光打印机',2440.00);insert into cart values(13,'联想激光打印机LJ2800W','联想激光打印机LJ2800W',3590.00);insert into cart values(14,'联想激光打印机LJ5500','联想激光打印机LJ5500',5440.00);insert into cart values(15,'联想激光打印机C8000','联想激光打印机C8000',6490.00);insert into cart values(16,'联想激光打印机LJ7500E','联想激光打印机LJ7500E',7990.00);5.创建包cart.action,cart.actionform,cart.bean,cart.dao,cart.exception,cart.listener。 将鼠标放在界面左上方的<Project Source>上点击右键,选择new->package,然后输入相应的包名。6. 创建业务bean: 在cart.bean包中创建,Product,CartItem,Cart类 创建Product:将鼠标放在界面左上方的<cart.bean>上点击右键,选择new->class,然后输入如下: package:cart.bean class name:Product 然后点击[OK]. 同样的方式创建CartItem和Cart,类的源代码如下:-------------------------------------------------Product.java------------------------------------------------------------package cart.bean;public class Product { private Integer id; private String name; private String description; private double price; public Product(Integer id, String name,String description ,double price) { this.id = id; this.name = name; this.description = description; this.price = price; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; }}编译源文件--------------------------------------------------------------------CartItem.java------------------------------------------------------------package cart.bean;public class CartItem { private Product product; private int number; private double cost; public CartItem(Product product ,int number) { this.product = product; this.number = number; setCost(); } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; setCost(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; setCost(); } private void setCost(){ if(product == null || number <= 0){ cost = 0; return; } cost = product.getPrice() * number; } public double getCost(){ return cost; }}编译源文件---------------------------------------------------------------Cart.java--------------------------------------package cart.bean;import java.util.HashMap;import java.util.Iterator;import java.util.Collection;public class Cart { private HashMap cartItem; private double cost; public Cart() { cartItem = new HashMap(); setCost(); } public boolean isEmpty(){ if(cartItem.size()==0) return true; return false; } public int getItemNumber(){ return cartItem.size(); } public void clear(){ cartItem.clear(); setCost(); } public void deleteItemByProductId(Integer productId){ if(productId == null) return ; cartItem.remove(productId); setCost(); } public void deleteItemsByProductId(Integer[] productId){ if(productId == null) return ; for(int i=0;i<productId.length;i++){ cartItem.remove(productId[i]); } setCost(); } public void addItem(Product product,int number){ if(number <= 0) return; CartItem ci = null; if(cartItem.containsKey(product.getId())){ ci = (CartItem)cartItem.get(product.getId()); System.out.println("addnumber==>"+number); System.out.println("oldNumber===>"+ci.getNumber()); int new_number = ci.getNumber() + number; ci.setNumber(new_number); System.out.println("new number===>"+ci.getNumber()); cartItem.remove(product.getId()); cartItem.put(product.getId(),ci); }else{ ci = new CartItem(product, number); cartItem.put(product.getId(), ci); } setCost(); } public void modifyNumberByProductId(Integer productId,int number){ if(!cartItem.containsKey(productId)) return; CartItem ci = (CartItem)cartItem.get(productId); ci.setNumber(number); cartItem.remove(productId); cartItem.put(productId,ci); setCost(); } public void setCost(){ double cost = 0; Collection coll = cartItem.values(); Iterator iter = coll.iterator(); CartItem ci = null; while(iter.hasNext()){ ci = (CartItem)iter.next(); cost += ci.getCost(); } this.cost = cost; } public double getCost(){ return cost; } public HashMap getCartItem() { return cartItem; }}编译源文件7.在包cart.exception下创建DBConnException,源文件如下:package cart.exception;public class DBConnException extends Exception{ public DBConnException() { super("数据库异常"); } public DBConnException(String message){ super(message); }}编译源文件8.在包cart.dao下创建ShoppingCartDAO.java,源文件如下:package cart.dao;import java.sql.*;import cart.exception.DBConnException;import java.util.HashMap;import cart.bean.Product;public class ShoppingCartDAO { private Connection conn; private PreparedStatement pstm; private ResultSet rs; public ShoppingCartDAO() throws DBConnException{ try{ Class.forName("com.pointbase.jdbc.jdbcUniversalDriver"); }catch(Exception e){ e.printStackTrace(); throw new DBConnException(); } } public HashMap getAllProduct() throws SQLException{ HashMap hm = new HashMap(); String sql = "select id,name,description,price from cart"; Product product = null; try{ conn = DriverManager.getConnection( "jdbc:pointbase:server://localhost:9092/ec_port", "cart", "cart"); pstm = conn.prepareStatement(sql); rs = pstm.executeQuery(); while (rs.next()) { product = new Product( new Integer(rs.getInt("id")), rs.getString("name"), rs.getString("description"), rs.getDouble("price")); hm.put(new Integer(rs.getInt("id")), product); } return hm; }catch(SQLException e){ e.printStackTrace(); throw e; }finally{ if(rs!=null){ try{ rs.close(); }catch(Exception exce){exce.printStackTrace();} } if(pstm!=null){ try{ pstm.close(); }catch(Exception exce){exce.printStackTrace();} } if(conn!=null){ try{ conn.close(); }catch(Exception exce){exce.printStackTrace();} } } }}编译源文件 9.创建SCServletContextListener. 选择file->new->web->servlet,在出现的界面中输入如下: package:cart.listener class name:SCServletContextListener webapp:cart 在最下面的单选框中选中:listener servlet 然后点击[next],在下一个界面中只选中ServletContextListener,然后[next]->[finish] 该类的源文件如下:package cart.listener;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import cart.dao.ShoppingCartDAO;import java.util.HashMap;public class SCServletContextListener extends HttpServlet implements ServletContextListener { //Notification that the web application is ready to process requests public void contextInitialized(ServletContextEvent sce) { System.out.println("Shopping cart application start."); ShoppingCartDAO dao = null; HashMap hm = null; try{ dao = new ShoppingCartDAO(); hm = dao.getAllProduct(); sce.getServletContext().setAttribute("allProduct",hm); }catch(Exception e){ e.printStackTrace(); } } //Notification that the servlet context is about to be shut down public void contextDestroyed(ServletContextEvent sce) { sce.getServletContext().removeAttribute("allProduct"); System.out.println("Shopping cart application stop."); }}10.创建SCSessionListener 选择file->new->web->servlet,在出现的界面中输入如下: package:cart.listener class name:SCSessionListener webapp:cart 在最下面的单选框中选中:listener servlet 然后点击[next],在下一个界面中只选中HttpSessionListener,然后[next]->[finish] 该类的源文件如下:package cart.listener;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;import cart.bean.Cart;public class SCSessionListener extends HttpServlet implements HttpSessionListener { //Notification that a session was created public void sessionCreated(HttpSessionEvent se) { System.out.println("a user created."); Cart cart = new Cart(); se.getSession().setAttribute("cart",cart); } //Notification that a session was invalidated public void sessionDestroyed(HttpSessionEvent se) { se.getSession().removeAttribute("cart"); System.out.println("a user destroy."); }}编译
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -