会话
:用户打开一个浏览器,点击了很多超链接,访问多个
web
资源,关闭浏览器,这个过程可以称之 为会话;
有状态会话
:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会 话;
一个网站,怎么证明你来过?
客户端
服务端
1.
服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了;
cookie
2.
服务器登记你来过了,下次你来的时候我来匹配你;
seesion
保存会话的两种技术
cookie
客户端技术
(响应,请求)
![](https://img-blog.csdnimg.cn/20210617174232987.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0phdmFfaXNfTk9fMQ==,size_16,color_FFFFFF,t_70)
session
服务器技术,利用这个技术,可以保存用户的会话信息? 我们可以把信息或者数据放在
Session
中
常见常见:网站登录之后,你下次不用再登录了,第二次访问直接就上去了!
Cookie:
1.
从请求中拿到
cookie
信息
2.
服务器响应给客户端
cookie
cookie
:一般会保存在本地的 用户目录下
appdata
;
package com.cc.hello;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import static java.lang.System.out;
public class cookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
PrintWriter xx = resp.getWriter();
Cookie[] cookies = req.getCookies();
if (cookies!=null){
xx.write("你你上一时间:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lastLoginTime")){
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
xx.write(date.toLocaleString());
}
}
}else{
xx.write("这是第一次");
}
Cookie lastLoginTime = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
resp.addCookie(lastLoginTime);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
别忘了去xml配置,配置完成就运行一下可以得到下图:
一个网站
cookie
是否存在上限!
聊聊细节问题
- 一个Cookie只能保存一个信息;
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
-
Cookie 大小有限制 4kb ;
- 300个cookie浏览器上限
删除
Cookie
;
- 不设置有效期,关闭浏览器,自动失效;
- 设置有效期时间为 0 ;
Session(重点):
什么是
Session
:
- 服务器会给每一个用户(浏览器)创建一个Seesion对象;
- 一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
- 用户登录之后,整个网站它都可以访问!--> 保存用户的信息;保存购物车的信息…..
Session和cookie的区别:
- Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个)
- Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
- Session对象由服务创建;
使用场景:
- 保存一个登录用户的信息;
- 购物车信息;
- 在整个网站中经常会使用的数据,我们将它保存在Session中;
package com.kuang.servlet;
import com.kuang.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session = req.getSession();
//给Session中存东西
session.setAttribute("name",new Person("秦疆",1));
//获取Session的ID
String sessionId = session.getId();
//判断Session是不是新创建
if (session.isNew()){
resp.getWriter().write("session创建成功,ID:"+sessionId);
}else {
resp.getWriter().write("session以及在服务器中存在
了,ID:"+sessionId);
}
//Session创建的时候做了什么事情;
// Cookie cookie = new Cookie("JSESSIONID",sessionId);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
//得到Session
HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("name");
System.out.println(person.toString());
HttpSession session = req.getSession();
session.removeAttribute("name");
//手动注销Session
session.invalidate();
会话自动过期:
web.xml
配置
<!--设置Session默认的失效时间-->
<session-config>
<!--15分钟后Session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>