Cookie小案例

Cookie小案例:
	1记住用户最后一次的访问时间
	2记住用户登录时的用户名

// 1利用Cookie技术,记住用户上次访问的时间
public class ShowLastAccessTimeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;character=utf-8");
		PrintWriter out = response.getWriter();
		out.write("您上次访问的时间是:");

		// 显示上次记录的时间
		Cookie[] cookies = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if ("lastAccessTime".equals(cookie.getName())) {
				String value = cookie.getName();
				long time = Long.parseLong(value);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				out.write(dateFormat.format(time));
				// out.write(new Date(time).toLocaleString()); // 过时了
			}
		}
		
		// 向客户端写一个cookie,取当前的访问时间为它的值
		Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis() + "");
		// 设置maxAge
		cookie.setMaxAge(Integer.MAX_VALUE);
		cookie.setPath(request.getContextPath());
		response.addCookie(cookie);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

----------------------------------------------------------

// 2.1展示登录界面,表单自动读取指定的cookie
public class ShowLoginFormServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;character=utf-8");
		PrintWriter out = response.getWriter();

		// 读取指定的cookie,准备回显的数据
		String username = "";
		String checked = "";
		Cookie[] cookies = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if ("username".equals(cookie.getName())) {
				username = cookie.getName();
				checked = "checked='checked'";
				break;
			}
		}
		
		// 展示登录界面
		out.write("<form action='"+ request.getContextPath() + "/RememberUserServlet' method='post' >");
		out.write("username:<input type='text' name='username' value='" + username + "' /><br/>");
		out.write("password:<input type='password' name='password' value='' /><br/>");
		out.write("remember:<input type='checkbox' name='remember' value='" + checked + "' /><br/>");
		out.write("<input type='submit' /><br/>");
		out.write("</form>");
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

// 2.2完成登录,并且向浏览器写入cookie
public class RememberUserServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;character=utf-8");
		PrintWriter out = response.getWriter();

		String username = request.getParameter("username");
		String remember = request.getParameter("remember");

		Cookie cookie = new Cookie("username", username);
		cookie.setPath(request.getContextPath());
		if(remember != null){
			cookie.setMaxAge(Integer.MAX_VALUE);
		}else {
			cookie.setMaxAge(0);
		}
		response.addCookie(cookie);
		out.write("登录成功!");
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

----------------------------------------------------------
补充:

购物车的信息在关闭了浏览器后,再重新打开浏览器时还依然存在的解决方法:

	//自己写名为"JSESSIONID"的Cookie,覆盖掉原来的
	Cookie c = new Cookie("JSESSIONID",session.getId());
	c.setPath(request.getContextPath());
	c.setMaxAge(Integer.MAX_VALUE);
	response.addCookie(c);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值