这是什么情况??

来源:8-8 使用Cookie实现保存商品浏览记录

weixin_慕仙1176837

2020-10-24 11:02

http://img.mukewang.com/5f9398c10001319e08080677.jpg

就是删掉response.addCookie(cookie)就不会了,不过不循环了

写回答 关注

1回答

  • weixin_慕仙1176837
    2020-10-24 11:09:22
    这是index.jsp的
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="list.entity.Items" %>
    <%@ page import="list.dao.ItemsDAO" %><%--
      Created by IntelliJ IDEA.
      User: zz
      Date: 2020/10/21
      Time: 10:50
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>商品展示</title>
        <style type="text/css">
            div{
                float:left;
                margin:10px;
            }
            div dd{
                margin:0px;
                font-siez:10pt;
            }
            div dd.dd_name{
                color:blue;
            }
            div dd.dd_city{
                color: #000;
            }
        </style>
    </head>
    <body>
    <h1>商品展示</h1>
    <hr>
    <center>
        <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td>
                    <!--商品循环开始-->
                    <%
                        ItemsDAO itemsDAO=new ItemsDAO();
                        ArrayList<Items>list=itemsDAO.getAllItems();
                        if(list!=null&&list.size()>0){
                        for(int i=0;i<list.size();i++){
                        Items items=list.get(i);
                    %>
                    <div>
                        <dl>
                            <dt>
                                <a href="details.jsp?id=<%=items.getId()%>"><img src="images/<%=items.getPicture()%>" width="120" height="80"></a>
                            </dt>
                            <dd class="dd_name"><%=items.getName()%></dd>
                            <dd class="dd_city">产地:<%=items.getCity()%>&nbsp;&nbsp;价格:¥<%=items.getPrice()%></dd>
                        </dl>
                    </div>
                    <!--商品循环结束-->
                    <%
                            }
                        }
                    %>
                </td>
            </tr>
        </table>
    </center>
    </body>
    </html>
    这是ItemsDAO的
    package list.dao;
    
    import list.entity.Items;
    import list.util.DBHelper;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    
    //商品的业务逻辑类
    public class ItemsDAO {
        //获得所有的商品信息
        public ArrayList<Items> getAllItems(){
            Connection conn=null;
            PreparedStatement stmt=null;
            ResultSet rs=null;
            ArrayList<Items> list=new ArrayList<Items>();
            try{
                conn= DBHelper.getConnection();
                String sql="SELECT * from items";
                stmt=conn.prepareStatement(sql);
                rs=stmt.executeQuery();
                while (rs.next()){
                    Items item=new Items();
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setNumber(rs.getInt("number"));
                    item.setPrice(rs.getInt("price"));
                    item.setPicture(rs.getString("picture"));
                    list.add(item);//把一个对象加入集合
                }
                return list;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }finally {
                //释放数据集对象
                if(rs!=null){
                    try{
                        rs.close();
                        rs=null;
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                //释放语句对象
                if(stmt!=null){
                    try{
                        stmt.close();
                        stmt=null;
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
            //根据商品编号获得商品资料
        public Items getItemsById(int id){
            Connection conn=null;
            PreparedStatement stmt=null;
            ResultSet rs=null;
            try{
                conn= DBHelper.getConnection();
                String sql="SELECT * from items where id=?";
                stmt=conn.prepareStatement(sql);
                stmt.setInt(1,id);
                rs=stmt.executeQuery();
                if (rs.next()){
                    Items item=new Items();
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setNumber(rs.getInt("number"));
                    item.setPrice(rs.getInt("price"));
                    item.setPicture(rs.getString("picture"));
                    return  item;
                }else{
                    return null;
                }
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }finally {
                //释放数据集对象
                if(rs!=null) {
                    try {
                        rs.close();
                        rs = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                //释放语句对象
                if (stmt != null) {
                    try {
                        stmt.close();
                        stmt = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        //获取最近浏览的签5条商品信息
        public ArrayList<Items> getViewList(String list){
            ArrayList<Items> itemlist=new ArrayList<Items>();
            int iCount=5;//每次返回前5条记录
            if(list!=null&&list.length()>0) {
                String[] arr = list.split(",");
                //如果商品记录大于等于5条
                if (arr.length >= 5) {
                    for(int i=arr.length-1;i>=arr.length-iCount;i--){
                        itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }else{
                    for(int i=arr.length-1;i>=0;i--){
                        itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }
                    return itemlist;
                } else {
                    return null;
                }
            }
        }
        这是detals.jsp的
        <%@ page import="list.dao.ItemsDAO" %>
    <%@ page import="list.entity.Items" %>
    <%@ page import="java.util.ArrayList" %><%--
      Created by IntelliJ IDEA.
      User: zz
      Date: 2020/10/21
      Time: 10:01
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>商品详情</title>
        <style type="text/css">
            div{
                float:left;
                margin-left:30px;
                margin-right:30px;
                margin-top:5px;
                margin-bottom:5px;
            }
            div dd{
                margin:0px;
                font-size:10pt;
            }
            div dd.dd_name{
                color:blue;
            }
            div dd.dd_city{
                color:#000;
            }
        </style>
    </head>
    <body>
    <h1>商品详情</h1>
    <hr>
    <center>
        <table width="750" height="60" cellpadding="0" cellspacing="0"border="0">
            <tr>
                <!--商品详情-->
                <%
                    Items items=new Items();
                    ItemsDAO itemsDAO=new ItemsDAO();
                    items=itemsDAO.getItemsById(Integer.parseInt(request.getParameter("id")));
                    if(items!=null){
                %>
                <td width="70%" valign="top">
                    <table>
                        <tr>
                            <td rowspan="4"><img src="images/<%=items.getPicture()%>" width="200" height="160"/> </td>
                        </tr>
                        <tr>
                            <td><B><%=items.getName()%></B></td>
                        </tr>
                        <tr>
                            <td>产地:<%=items.getCity()%></td>
                        </tr>
                        <tr>
                            <td>价格:<%=items.getPrice()%>¥</td>
                        </tr>
                    </table>
                </td>
                <%
                    }
                %>
                <%
                    String list="";
                    //从客户端获得Cookies集合
                    Cookie[] cookies=request.getCookies();
                    //遍历这个Cookies集合
                    if(cookies!=null&&cookies.length>0) {
                        for (Cookie c : cookies) {
                            if (c.getName().equals("ListViewCookie")) {
                                list = c.getValue();
                            }
                        }
                    }
                    list+=request.getParameter("id")+",";
                    //如果浏览记录超过1000条,清零
                    String [] arr=list.split(",");
                    if(arr!=null&&arr.length>0){
                        if(arr.length>=1000){
                            list="";
                        }
                    }
                    Cookie cookie=new Cookie("ListViewCookie",list);
                    response.addCookie(cookie);
    
                %>
                <!--浏览过的商品-->
                <td width="308" bgcolor="#EEE" align="center">
                    <br>
                    <b>您浏览过的商品</b><br/>
                    <!--循环开始-->
                    <%
                        ArrayList<Items> itemlist=itemsDAO.getViewList(list);
                        if(itemlist!=null&&itemlist.size()>0){
                            for(Items i:itemlist){
                    %>
                    <div>
                        <dl>
                            <dt>
                                <a href="details.jsp?id=<%=i.getId()%>"><img src="images/<%=i.getPicture()%>" width="120" height="120"></a>
                            </dt>
                            <dd class="dd_name"><%=i.getName()%></dd>
                            <dd class="dd_city">产地:<%=i.getCity()%>&nbsp;&nbsp;价格:<%=i.getPrice()%>¥</dd>
                        </dl>
                    </div>
                    <%
                            }
                        }
                    %>
                    <!--循环结束-->
                </td>
            </tr>
        </table>
    </center>
    </body>
    </html>


JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248279 学习 · 3071 问题

查看课程

相似问题