Session
1.会话
会话:指用户打开了一个浏览器,点击了很多超链接,访问了多个Web资源,后关闭浏览器,这个过程可以称之为“会话”。
有状态的会话:例如一个学生来过咱们班的教室,下次再来的时候,我们就能记住他来过。这个就是有状态的会话。
Eg:
一个网站如何证明你来过?
客户端<——>服务端
1.服务端会给客户一个 信件,客户端下次访问的时候,带上这个信件,就可以被认出来了。(Cookie)。
2.服务器登记你来过了,下次再访问的时候,会自动匹配你(Session)。
2.保存会话的两种技术:
Cookie:
- 客户端技术(具有响应,请求)
Session:
- 服务器技术,利用此技术,我们可以保存,用户的会话信息,我们可以把信息或者数据放在Session中。
常见场景:
网站登录后,关闭浏览器再打开此网页,一般情况下第二次进入都不用再次登录了,账户信息会被记住。
3.Session(重点!!!)
什么是Session:
-
服务器会给每一个用户创建一个Session对象。
-
一个Session独占一个浏览器,只要浏览器没有关,这个Session就一直存在。
- 用户登录之后,整个网站的子页面都是可以访问的。Eg:保存用户信息;保存购物车信息……
对比Session和Cookie:
-
Cookie是把用户的数据写给用户的浏览器,浏览器保存。(可以保存多个)
-
Session是把用户的数据写到用户的Session中,由服务器创建,服务器端保存。(应仅保存重要的信息,避免造成服务器端的资源浪费)
使用场景:
-
保存一个用户登录的信息。(用户登录;购物车等)
-
在整个网站中经常会使用的数据,我们将其保存在Session中。
代码分析:
文件排序:
web.xml:
<servlet>
<servlet-name>Session1</servlet-name>
<servlet-class>com.edwin.servlet.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session1</servlet-name>
<url-pattern>/Session1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Session2</servlet-name>
<servlet-class>com.edwin.servlet.SessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session2</servlet-name>
<url-pattern>/Session2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Session3</servlet-name>
<servlet-class>com.edwin.servlet.SessionDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session3</servlet-name>
<url-pattern>/Session3</url-pattern>
</servlet-mapping>
Session01:
package com.edwin.servlet;
import com.edwin.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
/
* @author Edwin D
* @date 2020.6.6 上午 11:11
*/
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("Edwin 段",17));
// 获取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);
}
}
Session02:
package com.edwin.servlet;
import com.edwin.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/
* @author Edwin D
* @date 2020.6.6 下午 9:23
*/
public class SessionDemo02 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();
Person person = (Person) session.getAttribute("name");
System.out.println(person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Session03:
package com.edwin.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/
* @author Edwin D
* @date 2020.6.6 下午 9:50
*/
public class SessionDemo03 extends HttpServlet {
// 清空Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
// 注销原有的SessionID,但是会立刻生成了一个新的。
session.invalidate();
System.out.println("注销了原有Session");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
使用后再访问Session02:
后台:
自动注销的XML配置:
<!--设置Session默认失效的时间-->
<session-config>
<!--以分钟为单位,这里是设置为17分钟后自动更换Session-->
<session-timeout>17</session-timeout>
</session-config>
效果对比:
1Min后:
后期的学习:
路漫漫其修远兮,吾将上下而求索。
参考文献
《【狂神说Java】JavaWeb入门到实战》
2020.06.07