问答详情
源自:8-8 使用Cookie实现保存商品浏览记录

cookie出问题了

org.apache.jasper.JasperException: An exception occurred processing JSP page /details.jsp at line 104

101:                   }
102:               }
103:               Cookie cookie = new Cookie("ListViewCookie",list);
104:               response.addCookie(cookie);
105:           %>
106:   
107:           <td width="30%" bgcolor="#EEE" align="center">

//想知道这是为什么?求解



 <!-- 商品详情 -->
          <%
             ItemsDAO itemDao = new ItemsDAO();
             Items item = itemDao.getItemsById(Integer.parseInt(request.getParameter("id")));
             if(item!=null)
             {
          %>
          <td width="70%" valign="top">
             <table>
               <tr>
                 <td rowspan="4"><img src="001.jpg" width="200" height="160"/></td>
               </tr>
               <tr>
                 <td><B><%=item.getName() %></B></td>
               </tr>
               <tr>
                 <td>产地:<%=item.getCity()%></td>
               </tr>
               <tr>
                 <td>价格:<%=item.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="30%" bgcolor="#EEE" align="center">
             <br>
             <b>您浏览过的商品</b><br>
             <!-- 循环开始 -->
             <%
                ArrayList<Items> itemlist = itemDao.getViewList(list);
                if(itemlist!=null&&itemlist.size()>0 )
                {
                   System.out.println("itemlist.size="+itemlist.size());
                   for(Items i:itemlist)
                   {
                        
             %>
             <div>
             <dl>
               <dt>
                 <a href="details.jsp?id=<%=i.getId()%>"><img src="001.jpg" width="120" height="90" border="1"/></a>
               </dt>
               <dd class="dd_name"><%=i.getName() %></dd>
               <dd class="dd_city">产地:<%=i.getCity() %>&nbsp;&nbsp;价格:<%=i.getPrice() %> ¥ </dd>
             </dl>
             </div>
             <%
                   }
                }
             %>
             <!-- 循环结束 -->
          </td>
        </tr>




提问者:西昌 2017-02-26 10:43

个回答

  • _浓雾不消残酒_0
    2017-04-30 17:35:39

    感谢,这个回答解决了我的问题!

  • laoLangLoveProgram
    2017-03-01 20:24:43

    //1.保存list到cookie中时需要转码

    Cookie cookie = new Cookie("ListViewCookie", URLEncoder.encode(list, "UTF-8"));

    response.addCookie(cookie);

    //2.读取cookie,遍历这个Cookies集合的时候需要解码

    if(cookies!=null && cookies.length>0) {

        for(Cookie c : cookies) {

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

                //list = c.getValue();

                //由于cookie保存list的时候,经过转码,所以提取的时候也要解码

                list = URLDecoder.decode(c.getValue(), "UTF-8");

            }

        }

    }