一、显示最近访问的时间
- 判断账号是否正确
- 如果正确,则获取cookie。 但是得到的cookie是一个数组, 我们要从数组里面找到我们想要的对象。
- 如果找到的对象为空,表明是第一次登录。那么要添加cookie
- 如果找到的对象不为空, 表明不是第一次登录。 获取上次登录的时间并且更新登录时间
前端页面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");
}