zoukankan      html  css  js  c++  java
  • 商城系统PageBean分页 Cookie存储浏览记录

    封装实体类:PageBean

    public class PageBean<T> {
        private int currentPage;//当前页
        private int currentCount;//页面显示条数
        private int totalCount;//总条数
        private int totalPage;//总页数
        private List<T> list;//当前页
    }

     service分页实现:

            ProductDao dao = new ProductDao();
            //封装一个pageBean
            PageBean<Product> pageBean = new PageBean<Product>();
            //1.封装当前页
            pageBean.setCurrentPage(currentPage);
            //2.封装每页显示条数
            pageBean.setCurrentCount(currentCount);
            //3.封装总条数
            int totalCount =0;
            try {
                totalCount = dao.getCount(cid);
            } catch (SQLException e) {
            }
            pageBean.setTotalCount(totalCount);
            //4.封装总页数
            int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
            pageBean.setTotalPage(totalPage);
            //5.封装当前页显示的数据
            int index= (currentPage-1)*currentCount;//起始索引
            List<Product> list =null;
            try {
                list = dao.findProductByPage(cid,index,currentCount);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            pageBean.setList(list); 
            return pageBean;

    productInfo  Servlet  打开商品页面,写入cookie

    //获得客户端携带cookie---获得名字是pids的cookie
          
         String pid = request.getParameter("pid"); String pids = pid; Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie cookie : cookies){ if("pids".equals(cookie.getName())){ pids = cookie.getValue(); //1-3-2 本次访问商品pid是8----->8-1-3-2 //1-3-2 本次访问商品pid是3----->3-1-2 //1-3-2 本次访问商品pid是2----->2-1-3 //将pids拆成一个数组 String[] split = pids.split("-");//{3,1,2} List<String> asList = Arrays.asList(split); LinkedList<String> list = new LinkedList<String>(asList); //判断集合中是否存在当前pid if(list.contains(pid)){ list.remove(pid); list.addFirst(pid); }else{ list.addFirst(pid); } //将[3,1,2]转成3-1-2字符串 StringBuffer sb = new StringBuffer(); for(int i = 0; i <list.size()&&i<7 ; i++){ sb.append(list.get(i)); sb.append("-"); } //去掉3-1-2-后的- pids = sb.substring(0, sb.length()-1); } } } Cookie cookie_pids = new Cookie("pids",pids); response.addCookie(cookie_pids); //转发前,创建cookie存储pid request.getRequestDispatcher("/product_info.jsp").forward(request, response);

    productListBYCid  Servlet  打开商品列表,读取cookie

    //定义一个记录历史商品信息的集合
            List<Product> historyProductList = new ArrayList<Product>();
            Cookie[] cookies = request.getCookies();
            if(cookies!=null){
                for(Cookie cookie : cookies){
                    if("pids".equals(cookie.getName())){
                        String pids = cookie.getValue();
                        String[] split = pids.split("-");
                        for(String pid : split){
                            Product product = service.findProductByPid(pid);
                            historyProductList.add(product);
                        }    
                    }
                }
            }
            
            request.setAttribute("historyProductList", historyProductList);
            request.getRequestDispatcher("/product_list.jsp").forward(request, response); 
  • 相关阅读:
    [leetcode] #279 Perfect Squares (medium)
    vue-cli3.0配置详解
    babel7 的配置加载逻辑
    webpack-小滴课堂学习笔记
    VUE-cli3使用 svg-sprite-loader
    打包报错,提示UglifyJs Unexpected token: keyword «const»
    UglifyJs报Unexpected token punc «:», expected punc «,» 这类错误的解决办法
    element-ui实现部分引用
    图解 HTTP 缓存
    process.argv
  • 原文地址:https://www.cnblogs.com/zemul/p/10219031.html
Copyright © 2011-2022 走看看