cookie商品浏览记录
来源:互联网 发布:淘宝卖家旺旺设置 编辑:程序博客网 时间:2024/05/17 02:59
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.MyCookieUtil;
public class ProductServlet extends HttpServlet {
public ProductServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求参数目的:存入到cookie中
String id = request.getParameter("id");
//先获取所有的cookie,查找指定名称的cookie
Cookie[] cookies = request.getCookies();
//查找指定名称的cookie
Cookie cookie = MyCookieUtil.getCookieByName(cookies, "product");
//如果cookie==null,我第一次访问,创建cookie,回写
if(cookie == null){
//我第一次访问,创建cookie,回写
Cookie c = new Cookie("product",id);
//回写
response.addCookie(c);
}else{
String value = cookie.getValue();
//判断,当前的商品的id是否包含在value中
String[] values = value.split(",");
if(!(checkId(values,id))){
//不包含
cookie.setValue(value+","+id);
//回写
response.addCookie(cookie);
}
}
//重定向到商品页面
response.sendRedirect("/day11/cookie/productList.jsp");
}
private boolean checkId(String[] values,String id){
for(String s:values){
if(s.equals(id)){
return true;
}
}
return false;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
// Put your code here
}
}
<%@page import="utils.MyCookieUtil"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'productList.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">
.img1{
width: 160px;
height: 140px;
}
</style>
</head>
<body>
<img class="img1" src="/day11/img/1.jpg"><a href="/day11/product?id=1">11</a>
<img class="img1" src="/day11/img/2.jpg"><a href="/day11/product?id=2">22</a>
<img class="img1" src="/day11/img/3.jpg"><a href="/day11/product?id=3">33</a><br>
<img class="img1" src="/day11/img/4.jpg"><a href="/day11/product?id=4">44</a>
<img class="img1" src="/day11/img/5.jpg"><a href="/day11/product?id=5">55</a>
<img class="img1" src="/day11/img/6.jpg"><a href="/day11/product?id=6">66</a>
<h3>浏览记录</h3>
<%
//获取cookie中value1,2,3循环遍历
Cookie[] cookies = request.getCookies();
Cookie cookie = MyCookieUtil.getCookieByName(cookies, "product");
if(cookie != null){
//获取1,2,3
String value = cookie.getValue();
//分隔
String[] ids = value.split(",");
for(String id : ids){
%>
<img class="img2" src="/day11/img/<%=id%>.jpg">
<% }
}
%>
</body>
</html>
- cookie商品浏览记录
- day14-cookie&商品浏览记录
- 用Cookie实现商品浏览记录
- 利用cookie显示商品的浏览记录
- 利用Cookie实现用户浏览商品记录
- Java Cookie记录商品浏览历史
- 使用Cookie实现保存商品浏览记录
- cookie实现商品浏览记录的原理
- 利用Cookie记录商品浏览信息
- 记录用户的商品浏览记录(Linkedlist,删除cookie)
- 用Cookie实现商品浏览记录(No.39)
- jquery+cookie+json保存浏览商品的记录
- cookie技术案例-用户浏览商品的记录
- Cookie小案例-----记住浏览过的商品记录
- Cookie记录浏览过的商品,商城类网站
- 使用Cookie实现商品的浏览记录功能
- 【JSP开发】利用cookie实现商品浏览记录
- java 使用Cookie实现,浏览过商品记录
- Raphael属性学习--基本
- c 通过指针表达运算结果
- KMP算法
- TabHost+Fragment初始化出现奔溃问题
- QtXlsxWriter 免编译 配置VS2013工程
- cookie商品浏览记录
- shell脚本之文件包含(九)
- Apk加壳实现
- Maven入门1-下载安装
- Chapter01 Java开发入门——Eclipse开发工具
- CSS盒子模型
- hive优化
- EM算法--应用到三个模型: 高斯混合模型 ,混合朴素贝叶斯模型,因子分析模型
- 17. Letter Combinations of a Phone Number Medium