1、会话
会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会话;
一个网站,怎么证明你来过?
客户端 服务端
- 服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了; Cookie
- 服务器登记你来过了,下次你来的时候我来匹配你; Session
2、保存会话的两种技术
cookie
- 客户端技术 (响应,请求)
session
- 服务器技术,利用这个技术,可以保存用户的会话信息? 我们可以把信息或者数据放在Session中!
常见常见:网站登录之后,你下次不用再登录了,第二次访问直接就上去了!
3、Cookie
- 一个Cookie只能保存一个信息;
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
- Cookie大小有限制4kb;
- 300个cookie浏览器上限
(1)获得Cookie
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(""utf-8"");
resp.setCharacterEncoding(""utf-8"");
// 保存用户上一次访问的时间
PrintWriter out = resp.getWriter();
// Cookie 服务端从客户端获取
// getCookie 方法返回的是一个数组,说明 Cookie 可能存在多个
Cookie[] cookies = req.getCookies();
// 判断 Cookie 是否存在
if (cookies != null){
// 如果存在
out.write(""您上一次访问的时间是:"");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
// 获取 cookie 的名字
if(cookie.getName().equals(""lastLoginTime"")){
// 获取 cookie 的值
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else {
out.write(""这是您第一次访问本站"");
}
// 服务器给客户端响应一个 cookie
Cookie cookie = new Cookie(""lastLoginTime"", System.currentTimeMillis() + """");
// 设置 cookie 的有效期
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(2)删除Cookie
public class CookieDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(""utf-8"");
resp.setCharacterEncoding(""utf-8"");
// 删除 Cookie
Cookie cookie = new Cookie(""lastLoginTime"", System.currentTimeMillis() + """");
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(3)中文传递数据
// 中文数据传递
public class CookieDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(""utf-8"");
resp.setCharacterEncoding(""utf-8"");
Cookie[] cookies = req.getCookies();
PrintWriter out = resp.getWriter();
if (cookies != null){
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(""name"")){
System.out.println(cookies[i].getValue());
out.write(URLDecoder.decode(cookies[i].getValue(),""utf-8""));
}
}
}else{
out.write(""小屿是大恐龙"");
}
Cookie cookie = new Cookie(""name"", URLEncoder.encode(""大恐龙"",""uft-8""));
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(4)xml文件配置
<?xml version=""1.0"" encoding=""UTF-8""?>
<web-app
xmlns=""http://xmlns.jcp.org/xml/ns/javaee""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xsi:schemaLocation=""http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd""
version=""4.0""
metadata-complete=""true"">
<!-- 访问时间 c1-->
<servlet>
<servlet-name>CookieDemo01</servlet-name>
<servlet-class>com.yv.servlet.CookieDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieDemo01</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
<!-- 删除Cookie c2-->
<servlet>
<servlet-name>CookieDemo02</servlet-name>
<servlet-class>com.yv.servlet.CookieDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieDemo02</servlet-name>
<url-pattern>/c2</url-pattern>
</servlet-mapping>
<!-- 中文数据传递 c3-->
<servlet>
<servlet-name>CookieDemo03</servlet-name>
<servlet-class>com.yv.servlet.CookieDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieDemo03</servlet-name>
<url-pattern>/c3</url-pattern>
</servlet-mapping>
</web-app>
4、Session(重点)
(1)什么是Session
- 服务器会给每一个用户(浏览器)创建一个Session对象;
- 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
- 用户登录之后,整个网站它都可以访问!–> 保存用户的信息;保存购物车的信息……
(2)Session和cookie的区别
- Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个)
- Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
- Session对象由服务创建;
(3)使用场景
- 保存一个登录用户的信息;
- 购物车信息;
- 在整个网站中经常会使用的数据,我们将它保存在Session中;
(4)创建Session
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决乱码问题
resp.setCharacterEncoding(""utf-8"");
req.setCharacterEncoding(""utf-8"");
resp.setContentType(""text/html;charset=utf-8"");
// 得到 Session
HttpSession session = req.getSession();
// 给 Session 中存东西
//session.setAttribute(""name"",""大恐龙"");
session.setAttribute(""person"",new Person(""大恐龙"",99));
// 获取 Session 的 id
String id = session.getId();
// 判断 Session 是不是新创建的
if (session.isNew()){
resp.getWriter().write(""Session 创建成功,id:"" + id);
}else{
resp.getWriter().write(""Session已经在服务器中存在了,id:"" + id);
}
// Session 创建的时候做的什么事情
// Cookie cookie = new Cookie(""JSESSIONID"",sessionId);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(5)获得Session
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决乱码问题
resp.setCharacterEncoding(""utf-8"");
req.setCharacterEncoding(""utf-8"");
resp.setContentType(""text/html;charset=utf-8"");
// 得到 Session
HttpSession session = req.getSession();
//String name = (String) session.getAttribute(""name"");
//resp.getWriter().write(name);
// 存一个对象
Person person = (Person) session.getAttribute(""person"");
resp.getWriter().write(person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(6)删除Session
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute(""person"");
// 手动注销 Session
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml配置
<?xml version=""1.0"" encoding=""UTF-8""?>
<web-app
xmlns=""http://xmlns.jcp.org/xml/ns/javaee""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xsi:schemaLocation=""http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd""
version=""4.0""
metadata-complete=""true"">
<!-- 创建Session s1-->
<servlet>
<servlet-name>SessionDemo01</servlet-name>
<servlet-class>com.yv.servlet.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo01</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<!-- 获得Session s2-->
<servlet>
<servlet-name>SessionDemo02</servlet-name>
<servlet-class>com.yv.servlet.SessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo02</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
<!-- 注销Session s3-->
<servlet>
<servlet-name>SessionDemo03</servlet-name>
<servlet-class>com.yv.servlet.SessionDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo03</servlet-name>
<url-pattern>/s3</url-pattern>
</servlet-mapping>
<!-- 设置 Session默认失效时间-->
<session-config>
<!-- 15分钟后 Session 自动失效-->
<!-- 以分钟为单位 -->
<session-timeout>15</session-timeout>
</session-config>
</web-app>