为了账号安全,请及时绑定邮箱和手机立即绑定

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>




正在回答

2 回答

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

0 回复 有任何疑惑可以回复我~

//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");

        }

    }

}


5 回复 有任何疑惑可以回复我~
#1

Nick_Bryant

谢谢这个回答,我解决了我的问题,谢谢
2017-04-10 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JAVA遇见HTML——JSP篇
  • 参与学习       248282    人
  • 解答问题       3071    个

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

进入课程

cookie出问题了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信