listentities = gservice.findall(); //通過
findall
方法查詢出所有商品
//輸出顯示所有商品
for(goods entity:entities)
//顯示瀏覽過的商品資訊
//宣告瀏覽過的商品的
value
值string value = null;
cookie cookies = request.getcookies();//獲取
cookie
//遍歷
cookie
資訊for(int i=0;cookies!=null&&i
if("goodshistory".equals(cookies[i].getname()) }
if(value!=null) }
顯示商品詳細資訊servlet中:
string id=request.getparameter("id");//得到傳過來的商品
id;integer numid = integer.parseint(id);//型別轉換
;goods entity = gservice.findbyid(numid);//根據
id查詢出該商品
//輸出顯示商品詳細資訊
out.println(entity.getid()+entity.getname()+entity.getprice());
string value = makecookievalue(request,id);//設定
cookie
資訊//產生乙個
cookie
物件cookie cookie = new cookie("goodshistory",value);
cookie.setmaxage(3600);//設定
cookie
有效期cookie.setpath("/20111024");
response.addcookie(cookie);//響應
//方法設定
cookievalue
值內容private string makecookievalue(httpservletrequest request, string id) }
//第一次訪問 最大三次
goodshistoty = null goodshistory = id goodshistory=1
//第二次訪問
goodshistory=1 id=2 goodshistory=2_1;
//第三次訪問
goodshistory=2-1 io=3 goodshistory=3_2_1;
//第四次訪問
goodshistory=3_2_1 id=2 goodshistory=2_3_1;
//第四次訪問
goodshistory=3_2_1 id=4 goodshistory=4_3_2;
if(goodshisoty==null)elseelseelse }
stringbuffer sb = new stringbuffer();
for(string ids:list)
sb.deletecharat(sb.lenth()-1);
godshistory=sb.tostring(); }
return goodshistory; }
顯示瀏覽過的商品
顯示瀏覽過的商品 在商品列表顯示servlet中 listentities gservice.findall 通過findall方法查詢出所有商品 輸出顯示所有商品 for goodsentity entities 顯示瀏覽過的商品資訊 宣告瀏覽過的商品的value值 stringvalue nul...
顯示上次瀏覽過的商品資訊
宣告返回值變數 string goodshistory null 獲取cookie物件 cookie cookies request.getcookies 遍歷cookie物件 for inti 0 cookies null i 判斷 if goodshistory equals cookies i...
cookie實現商品瀏覽記錄的原理
當我們來瀏覽網頁時,瀏覽過的商品要顯示在乙個指定模組中,最多顯示10條記錄,而且是最後瀏覽的商品在最上面顯示 可以使用linkedhashmap實現 要實現這些功能,第一需要cookie 這裡當使用者瀏覽商品後,伺服器端會根據商品id構建乙個cookie historycookie 然後通過resp...