X

由于无法提供服务器的高昂费用,博客即将迁移到github pages...

点击右上的X可以临时关闭此框并阻止跳转^_^

现在带您过去 秒后跳转...

[作业练习]带数据库的简易购物车

    在前一次作业的基础上([作业]JavaBean+Jsp简易购物车实现),加上数据库来管理商品和购买记录。
    最后结果展示:http://demo.dshui.wang/tomcat/ShopCartDemo/
    首先,建立数据表:

    #用户表
    CREATE TABLE `cart_users` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(80) NOT NULL DEFAULT '',
      `password` varchar(220) NOT NULL DEFAULT '',
      PRIMARY KEY (`id`),
      UNIQUE KEY `username` (`username`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
     (...继续阅读这篇文章...)

[作业]JavaBean+Jsp简易购物车实现

    本次作业要求使用到JavaBean+纯Jsp,带有登陆功能的购物车系统。
    登陆功能这里就不多讲了,前面的聊天室已经做过,这边直接拷贝就能使用了。
    另外,本次作业中我加入了BootStrap来做一个小小风格美化。
    不多说,直接上代码!
    index.jsp 首页,也是登陆界面

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%
        Cookie[] cookies = request.getCookies();
        String uname = "";
        String upwd = "";
        if(cookies!=null){
            for(int i=0;i
    
    
        
            
            
            
            请登录ShopCartDemo
            
        
        
            

    dologin.jsp 登陆信息处理页面

    <%-- 
        Document   : dologin
        Created on : 2015-10-9, 11:28:29
        Author     : Anthony
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import="java.util.*"%>
    
    
        
            
            正在登录...
        
        
            <%
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String remember = (String) request.getParameter("remember");
            if(username == null)
                username="";
            if(password == null)
                password="";
            if(remember == null)
                remember = "false";
            if(username.equals("201321092028")&&password.equals("123456") || username.equals("201321092027")&&password.equals("123456"))
            {
                if(remember != null && remember.equals("true"))
                {
                    Cookie cookie1 = new Cookie("remname",username);
                    Cookie cookie2 = new Cookie("rempwd",password);
                    cookie1.setMaxAge(60*60*24*5);
                    cookie2.setMaxAge(60*60*24*5);
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                }
                session.setAttribute("UserName",username);
                session.setAttribute("IsLogin","true");
                response.sendRedirect("goods.jsp");
            }else{
                response.sendRedirect("index.jsp");
            }
            %>
        
    
    

    goods.jsp 商品列表页面

    <%-- 
        Document   : goods
        Created on : 2015-10-15, 17:11:28
        Author     : anthony
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    
        
            
            
            
            商品列表
            
        
        
            
    编号商品名单价(元/斤)购买数量
    1苹果13
    2橘子8
    3西瓜2
    4火龙果15

    order.jsp 订单信息页面

    <%-- 
        Document   : order
        Created on : 2015-10-15, 18:28:05
        Author     : anthony
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    
        
            
            
            
            订单信息
            
        
        
            
                
                
                
                
            
    编号商品名单价(元/斤)数量价格
    1苹果13
    2橘子8
    3西瓜2
    4火龙果15

    另外,还需要一个JavaBean CountPrice.java

    package Cart;
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author anthony
     */
    public class CountPrice {
        private double n_apple;
        private double n_orange;
        private double n_watermalon;
        private double n_fires;
        private double p_apple;
        private double p_orange;
        private double p_watermalon;
        private double p_fires;
        private double totalprice;
    
        /**
         * @return the n_apple
         */
        public double getN_apple() {
            return n_apple;
        }
    
        /**
         * @param n_apple the n_apple to set
         */
        public void setN_apple(double n_apple) {
            this.n_apple = n_apple;
            this.setP_apple(n_apple*13);
        }
    
        /**
         * @return the n_orange
         */
        public double getN_orange() {
            return n_orange;
        }
    
        /**
         * @param n_orange the n_orange to set
         */
        public void setN_orange(double n_orange) {
            this.n_orange = n_orange;
            this.setP_orange(n_orange*8);
        }
    
        /**
         * @return the n_watermalon
         */
        public double getN_watermalon() {
            return n_watermalon;
        }
    
        /**
         * @param n_watermalon the n_watermalon to set
         */
        public void setN_watermalon(double n_watermalon) {
            this.n_watermalon = n_watermalon;
            this.setP_watermalon(n_watermalon*2);
        }
    
        /**
         * @return the n_fires
         */
        public double getN_fires() {
            return n_fires;
        }
    
        /**
         * @param n_fires the n_fires to set
         */
        public void setN_fires(double n_fires) {
            this.n_fires = n_fires;
            this.setP_fires(n_fires*15);
        }
    
        /**
         * @return the totalprice
         */
        public double getTotalprice() {
            this.totalprice = this.getP_apple()+this.getP_fires()+this.getP_orange()+this.getP_watermalon();
            return totalprice;
        }
    
        /**
         * @return the p_apple
         */
        public double getP_apple() {
            return p_apple;
        }
    
        /**
         * @return the p_orange
         */
        public double getP_orange() {
            return p_orange;
        }
    
        /**
         * @return the p_watermalon
         */
        public double getP_watermalon() {
            return p_watermalon;
        }
    
        /**
         * @return the p_fires
         */
        public double getP_fires() {
            return p_fires;
        }
    
        /**
         * @param p_apple the p_apple to set
         */
        public void setP_apple(double p_apple) {
            this.p_apple = p_apple;
        }
    
        /**
         * @param p_orange the p_orange to set
         */
        public void setP_orange(double p_orange) {
            this.p_orange = p_orange;
        }
    
        /**
         * @param p_watermalon the p_watermalon to set
         */
        public void setP_watermalon(double p_watermalon) {
            this.p_watermalon = p_watermalon;
        }
    
        /**
         * @param p_fires the p_fires to set
         */
        public void setP_fires(double p_fires) {
            this.p_fires = p_fires;
        }
        
    }

    附上NetBean项目工程:
    链接: http://pan.baidu.com/s/1o66DrjO 密码: yksy