HKINQ 2015-08-21 06:14 采纳率: 0%
浏览 1601

java web关于cookie问题

本人写一个简单的商品浏览且具备记录商品浏览历史的功能的页面。记录商品浏览历史是用cookie实现的。但是每次用request.getCookies()获取我之前保存的cookie时都是获取不了。不是为何,请看代码。

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%@ page import="entity.Items"%>
<%@ page import="dao.ItemsDAO"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'details.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <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>
          <!-- 商品详情 -->
          <% 
             ItemsDAO itemsDAO = new ItemsDAO();
             Items item = itemsDAO.getItemsById(Integer.valueOf(request.getParameter("id")) - 1);
             if(item != null){

          %>
          <td width="70%" valign="top">
             <table>
               <tr>
                 <td rowspan="4"><img src="images/<%= item.getPicture() %>" 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>
          <% 

                }
          %>
                <!-- 获取并更新cookies -->
          <% 
                String list = "";
                Cookie[] cookies = request.getCookies();
                if(cookies != null && cookies.length > 0){
                    for(Cookie cookie: cookies){
                        if(cookie.getName().equals("HistoryItemList")){
                            list = cookie.getValue();
                            break;
                        }
                    }
                }
                System.out.println("前:" + list);

                list += request.getParameter("id") + ";;";

                System.out.println("后:" + list);

                Cookie cookie = new Cookie("HistoryItemList", list);
                response.addCookie(cookie);


          %>
          <!-- 浏览过的商品 -->
          <td width="30%" bgcolor="#EEE" align="center">
             <br>
             <b>您浏览过的商品</b><br>
             <!-- 循环开始 -->
             <% 
                ArrayList<Items> historyItems = itemsDAO.getViewList(list);
                if(historyItems!= null && historyItems.size() > 0){
                    for(Items items: historyItems){

             %>
             <div>
             <dl>
               <dt>
                 <a href="details.jsp?id=<%= items.getId() %>"><img src="images/<%= items.getPicture() %>" width="120" height="90" border="1"/></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>


  • 写回答

4条回答 默认 最新

  • JPF1024 2015-08-21 06:26
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64