记录JAVA中Cookie和Session使用

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               //设置UTF-8编码格式
		request.setCharacterEncoding("UTF-8");
               //设置输出编码格式
		response.setHeader("Content-type","text/html;charset=UTF-8");
		//获取前端number中值
		String number = request.getParameter("number");
                // 设置session 
                request.getSession().setAttribute("user", u);
                // 清除session
		request.getSession().removeAttribute("user");
                // 获取 session 创建时间
                Date createTime = new Date(session.getCreationTime());
                // 获取该网页的最后一次访问时间
                Date lastAccessTime = new Date(session.getLastAccessedTime());
                //设置日期输出的格式  
                SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			 // 设置cookie
			Cookie c = new Cookie("userCookie", "name-password");
			//设置Cookie缓存到所有页面
			c.setPath("/");
			//设置Cookie有效时间(当设置Cookie为0时则是删除Cookie。-1 表示 cookie 将持续下去,直到浏览器关闭)
			c.setMaxAge(2000);
			//输出Cookie到前端浏览器
			response.addCookie(c);
			//重定向到main页面
			response.sendRedirect("main");
                        //提交调用get方法
                        doGet(request, response);
	}
转载请注明出处凌夕博客 »漫小猫原文地址《记录JAVA中Cookie和Session使用

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)