2014年1月7日星期二

How to use cookie to save browsing history commodity ?

how to write a java using cookie history to retain six recent history code ?
I will keep the last six commodity browsing history, will be read and displayed on the page, ask how to write ?


------ Solution ------- -------------------------------------
cookie operation
------ Solution -------- ------------------------------------
write in, when read out each landing
------ Solution ---------------------------------------- ----
with stringbuffer to record the user writes viewed item id cookie , gets a cookie browse again , remove the id, id you think why there are ok
------ For reference only ---------------------------------------
upstairs , the goods ID stored cookie, such as the " 1-2-3-4-5-6 " stored this way .
When viewing a product when it reads the cookie, get its string to determine whether the id in which posts a part of the code .

/**
 * 创建id字符串,形如"1-2-3-4-5-6"
 * @param request
 * @param id 商品Id
 * @return
 */
private String buildViewHistory(HttpServletRequest request, Long id) {

String cookieValue = WebUtil.getCookieByName(request,
"productViewHistory");//从cookie读取,此处进行了封装,对该方法关系不大
LinkedList<Long> linkedList = new LinkedList<Long>();
if (cookieValue != null && !"".equals(cookieValue)) {
String[] entityIds = cookieValue.split("-");

for (int i = 0; i < entityIds.length; i++) {
linkedList.add(Long.valueOf(entityIds[i]));
}
// 该商品若在浏览历史里,把该商品排在历史记录首处
if (linkedList.contains(id)) {
linkedList.remove(id);
}
// 用户浏览的商品超过5个,保留最近浏览的商品
if (linkedList.size() >= 5) {
linkedList.remove();
}

}
linkedList.offer(id);
StringBuilder sb = new StringBuilder();
for (Long prodcutID : linkedList) {
sb.append(prodcutID).append("-");
}
if (linkedList.size() != 1) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();

}

With this string ajax request at the front desk , it is easy to get checked items .
------ For reference only -------------------------------------- -
landlord this problem solved? I also do this seeking paste code
------ For reference only --------------------------- ------------

elaborate brother can no longer beg ah

没有评论:

发表评论