session | cookie |
服务器端 | 客户端 |
大量 | 少量 |
安全 | 不安全 |
效率高 | 效率低 |
session跟踪机制中需要cookie来保存和传递sessionId。
HttpSession是将http状态信息保存在服务器端的技术。
取得:HttpSession session = reques.getSession(); 如果session已经被创建,则返回已创建的,如果未被创建,则创建新的session。
作用域:被属于同一个会话的请求共享。
其他方法:session.getId()返回相关联的session的id;session.setMaxInactiveInterval()设置最大的有效时间,负数表示永不失效(除非服务器重启),0表示立即失效。可以通过服务器文件web.xml设置默认时间。session.invalidate()马上失效。session.setAttibute();session.getAttribute();session.removeAttribute()。
服务器文件web.xml中设置session响应时间的代码如下:
<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly -->
<!-- created sessions by modifying the value below. -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
欢迎大家关注公众号:技术交流,生活感悟分享