1 使用Cookie实现显示用户的上次访问时间
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 页面输出 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); // 获取字符输出流对象 PrintWriter out = response.getWriter(); // 获取Cookie数组对象 Cookie [] cookies = request.getCookies(); // 定义一个时间的字符串变量 String date = null; // 定义一个变量存储系统当前日期 Date current_date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 判断是否是第一次登陆 if(cookies != null){ // 直接循环 for(Cookie cookie : cookies){ // 获取Cookie if("lasttime".equals(cookie.getName())){ // 获取上次访问的时间 date = cookie.getValue(); break; }else{ // 获取系统时间 date = format.format(current_date); } } }else{ // 获取系统时间 date = format.format(current_date); } // 显示时间 out.println(date); // 将这次访问的时间写入Cookie Cookie new_cookie = new Cookie("lasttime",format.format(new Date())); new_cookie.setMaxAge(5*60); new_cookie.setPath("/day08/showtime"); // 发送 response.addCookie(new_cookie); }
Cookie细节
- 一个Cookie只能存储一种信息。
- 一个网站可以发送多个Cookie,浏览器可以同时携带多个Cookie。
- 同一个网站最多发送20个Cookie,浏览器最多存储300个Cookie,一个Cookie最多存储数据在4K以内。
- 如果创建了Cookie对象没有指定最大有效时间那么不会存储在浏览器的缓存中。
Session技术
在上面使用Cookie技术存储会话信息的时候发现Cookie存储的数据有限,而且每次需要客户端浏览器携带数据,导致网络的负载过大。因此如果需要存储相对大量的数据,那么可以直接将数据存储在服务器端,这样可以提高数据的访问速度。
HttpSession技术就是将会话的数据存储在服务器端,便于开发者直接进行访问。
1 HttpSession接口
该接口主要定义了一种区分不同用户并通过request对象获取该对象的实例存储与用户相关的信息的方式。
该接口的对象是tomcat服务器帮助开发者创建出来的,当开发者调用request.getSession()方法获取该接口的对象。
2 常用API
获取HttpSession对象
HttpSession getSession() 如果有直接返回,如果没有直接创建并返回HttpSession getSession(boolean create) true同上,false有返回,没有null
操作数据
void setAttribute(String name, Object value) 设置指定名的属性值 Object getAttribute(String name) 获取指定名的属性值Enumeration getAttributeNames() 获取所有属性名的集合迭代器void removeAttribute(String name) 删除指定名的属性
额外的方法
String getId() 获取Session的ID值boolean isNew() 判断Session是否是新的long getCreationTime() 获取Session创建时间的longlong getLastAccessedTime() 获取最后一次访问Session的时间void invalidate() 指定Session无效
3 HttpSession的读和写
1. 写Session数据
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 获取session对象 HttpSession session = request.getSession(); // 放置数据 session.setAttribute("name", "jack"); }
发现响应信息中以Set-Cookie: JSESSIONID=8AA06CD311EC6A38805C73C93B8FCE4F; Path=/day08响应头数据将HttpSession对象的ID值以Cookie的方式发送给浏览器进行存储。
2. 读Session数据
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 获取Session对象 HttpSession session = request.getSession(); // 取Session数据 String name = (String) session.getAttribute("name"); // 输出信息 System.out.println(name);}
请求消息中使用Cookie: JSESSIONID=8AA06CD311EC6A38805C73C93B8FCE4F携带了HttpSession对象的ID值,那么服务器根据该值找到对应的HttpSession对象获取其中的值。
总结:
HttpSession技术底层需要借助Cookie存储服务器端为每一个用户创建的HttpSession对象的ID值,便于后面拿到该值获取服务器指定对象中值。
3 重新审视以上代码
可以将获取数据的servlet中的获取sessiond的代码修改如下
HttpSession session = request.getSession(false);
现在实际的浏览器在启动多个同一个浏览器窗口,那么自动使用同一个Session对象。一旦Session的有效时间超过了半个小时那么Session自动销毁。
4 其他的常用方法
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 获取session对象 HttpSession session = request.getSession(); // 调用常用的方法 System.out.println("getId(): "+session.getId()); System.out.println("getLastAccessedTime(): "+session.getLastAccessedTime()); System.out.println("isNew(): "+session.isNew()); System.out.println("getCreationTime(): "+session.getCreationTime());}
运行结果
getId(): F8A7BC23A0B403CE30A69F8B5F903D6AgetLastAccessedTime(): 1358385915203isNew(): truegetCreationTime(): 1358385915203
如果在以上的代码中使用了 session.invalidate();后继续往session中添加数据,那么服务器报错
java.lang.IllegalStateException: setAttribute: Session already invalidated
总结:
登陆功能分析à 1. 获取用户数据 2. 校验数据 3. 成功,在Session中存储用户登陆的标识
注销功能分析à 1. 清除Session中的用户标识信息 2. 重定向到登陆页面
原标题:java学习笔记—会话(24)
关键词:JAVA