您的位置:首页 > 其它

cookie保存最近浏览的商品

2018-01-29 15:21 357 查看
    @WebServlet(urlPatterns={"/index"})  (代替了在web中的配置)

public class ProductServlet extends HttpServlet{

        ProductDaoImpl productDaoImpl= new ProductDaoImpl();

        @Override

        protected void doGet(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

                String action=request.getParameter("action");

                //显示所有的产品信息

                if("list".equals(action)){

                        

                        

                        //从cookie中读取最近浏览的记录

                        String cookieValue="";

                        cookieValue=selectcookieValue(request);

                        //写一个得到cookieValue的方法

                        Cookie [] arr= request.getCookies();

                        if(arr!=null){

                                for(Cookie c:arr){

                                        if("recentView".equals(c.getName())){

                                                cookieValue=c.getValue();

                                                break;

                                        }

                                }

                        }

                        List<ProductBean> products=new ArrayList<ProductBean>();

                        if(!"".equals(cookieValue)){

                                String [] ids=cookieValue.split(",");

                                for(int i=ids.length-1;i>=0;i--){ //让商品倒着实现

                                        

                            ProductBean product=productDaoImpl.productById(Integer.parseInt(ids[i]));

                             products.add(product);

                                }

                        }

                        request.setAttribute("retents", products);

                        

                        

                        

                        List<ProductBean> productlist=productDaoImpl.proList();

                        request.setAttribute("lists", productlist);

                        request.getRequestDispatcher("index.jsp").forward(request, response);

                } 

                //显示产品的详细信息(再点击商品浏览详细信息时,把id存到cookie中)

                if("view".equals(action)){

                        String idstr=request.getParameter("id");

                        

                        addCookie(idstr,response,request);

                        

                        //获取数据库中的产品详细信息

                        ProductBean product=productDaoImpl.productById(Integer.parseInt(idstr));

                        request.setAttribute("product", product);

                        request.getRequestDispatcher("product-view.jsp").forward(request, response);

                }

        }

        private void addCookie(String idstr,HttpServletResponse response,HttpServletRequest request){

                String cookieValue="";

                boolean flage=true; //是否是一个新的浏览  是的话为true 否则为false

                

                cookieValue=selectcookieValue(request);

                String [] ids=cookieValue.split(",");

                //cookieValue为空时 说明是第一个浏览

                if("".equals(cookieValue)){

                        cookieValue=idstr;

                }else{

                        

                        //id不一样就不重新newcookie

                        for(String id:ids){

                                if(id.equals(idstr)){

                                        flage=false;

                                        break;

                                }

                        }

                        if(flage==true){

                                cookieValue=cookieValue+","+idstr;

                        }

                        

                        //最近浏览只显示5条

                        

                        if(ids.length>5){

                                

                                cookieValue.substring(cookieValue.indexOf(",", 1)+1);

                        }

                }

                

                //向客户端发送一个cookie

                Cookie cook=new Cookie("recentView",cookieValue);

                response.addCookie(cook);

        }

        private String selectcookieValue(HttpServletRequest request){

                String cookieValue="";

                Cookie [] arr= request.getCookies();

                

                //得到cookieValue 

                                if(arr!=null){

                                        for(Cookie c:arr){

                                                if("recentView".equals(c.getName())){

                                                        cookieValue=c.getValue();

                                                        break;

                                                }

                                        }

                                }

                                return cookieValue;

        }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: