使用cookie显示最近访问的时间和商品浏览记录

一、显示最近访问的时间

  1. 判断账号是否正确
  2. 如果正确,则获取cookie。 但是得到的cookie是一个数组, 我们要从数组里面找到我们想要的对象。
  3. 如果找到的对象为空,表明是第一次登录。那么要添加cookie
  4. 如果找到的对象不为空, 表明不是第一次登录。 获取上次登录的时间并且更新登录时间

前端页面login.html:

	<form action="loginServlet" method="post">
		用户名:<input type="text" name="username" /><br>
		密码:<input type="text" name="password" /><br>
		<input type="submit" value="登录"/>
	</form>

获取特定Cookie的工具类:

public class CookieUtil {
	public static Cookie findCookie(Cookie [] cookies,String cookieName) {
		if(cookies!=null) {
			for(Cookie cookie:cookies) {
				if(cookie.getName().equals(cookieName)) {
					return cookie;
				}
			}
		}
		return null;
	}
}

loginServlet:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=UTF-8");
		//获取登录的信息
		String name=req.getParameter("username");
		String pass=req.getParameter("password");
		if("admin".equals(name) && "123".equals(pass)) {//登录成功
			//获取Cookie
			Cookie[] cookies = req.getCookies();
			Cookie cookie = CookieUtil.findCookie(cookies, "history");
			if(cookies==null) {//如果为空,则表示是第一次登录
				Cookie c=new Cookie("history", System.currentTimeMillis()+"");
				resp.addCookie(c);
				resp.getWriter().write("欢迎你"+name);
			}else {//不为空,表不是第一次登录
				long lastTime = Long.parseLong(cookie.getValue());	//记录上次登录事件
				resp.getWriter().write("欢迎你!"+name+",您上次登录的时间是:"+new Date(lastTime));
				
				//重置登录时间
				cookie.setValue(System.currentTimeMillis()+"");
				resp.addCookie(cookie);
			}
			
		}else {//登录失败
			resp.getWriter().write("登录失败");
		}
	}

二、显示商品浏览记录

1、分析
在这里插入图片描述

2、Servlet操作:HistoryServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取product_list.jsp页面传过来的参数
		String id=request.getParameter("id");
		//2.获取cookie的数组
		Cookie[] cookies = request.getCookies();
		//3.得到我想要的,存储历史浏览记录的cookie
		Cookie cookie = CookieUtil.findCookie(cookies, "history");
		//4.判断cookie是否为空
		if (cookie==null) {//表示第一次来到该服务器
			Cookie c=new Cookie("history", id);
			response.addCookie(c);
		}else {//表示不是第一次访问
			String ids=cookie.getValue();
			cookie.setValue(id+"#"+ids);
			response.addCookie(cookie);
		}
		//跳转界面
		response.sendRedirect("product_info.html");
	}

4、jsp显示商品浏览记录

<!--商品浏览记录:-->
	<div style="width:1210px;margin:0 auto; padding: 0 9px;border: 1px solid #ddd;border-top: 2px solid #999;height: 246px;">
			<h4 style="width: 50%;float: left;font: 14px/30px " 微软雅黑 ";">浏览记录</h4>
		<div style="width: 50%;float: right;text-align: right;"><a href="">more</a></div>
			<div style="clear: both;"></div>

			<div style="overflow: hidden;">
				<ul style="list-style: none;">
					<%
					Cookie[] cookies=request.getCookies();
					Cookie cookie = CookieUtil.findCookie(cookies, "history");
					if (cookie==null) {//还没有浏览记录
					%>
						<h3>还没有浏览任何商品</h3>
					<%
					}else {
						String ids[]=cookie.getValue().split("#");
						for(String id:ids){
					%>
					<li style="width: 150px;height: 216;float: left;margin: 0 8px 0 0;padding: 0 18px 15px;text-align: center;"><img src="products/1/cs1000<%=id %>.jpg" width="130px" height="130px" /></li>
					<%
						}
					}
					%>
				</ul>
				<h3><a href="ClearServlet">清空浏览记录</a></h3>
			</div>
	</div>

5、清除历史记录

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Cookie cookie=new Cookie("history", "");
		cookie.setMaxAge(0);	//立即清除cookie
		resp.addCookie(cookie);
		resp.sendRedirect("product_list.jsp");
	}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值