Overview of Session
- (1) What is Session?
Session technology is to access session data on the server side - (2) What are the characteristics?
Access to key-value
data has a survival time
, the jsessionId of each session is different
Principle of Session
Principle:
1" The server assigns an object called session to the browser
2" Each session has an id and can store key-value
3" In order for each browser to access its own data without being affected by others, the session must have a jessionId
Session access data
- (1) Get Session
HttpSession getSession() Get session object - (2) Save data
setAttribute(key, value) - (3) Read data
getAttribute(key)
src\com\wzx\pack01_set\Demo01SetServlet.java
save data
@WebServlet("/set")
public class Demo01SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问服务器,服务器为每个浏览器分配一个 session,有自己的id
//1 获取session对象
HttpSession session = request.getSession();
//2 获取session的编号jsessionId
String jsessionId = session.getId();
response.getWriter().println(jsessionId);
//3 保存key-value
session.setAttribute("name","jack");
session.setAttribute("age","20");
}
}
src\com\wzx\pack01_set\Demo02GetServlet.java
@WebServlet("/get")
public class Demo02GetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1 获取session对象
HttpSession session = request.getSession();
//2 获取key对应的value
String name = (String) session.getAttribute("name");
String age = (String) session.getAttribute("age");
System.out.println(name);
System.out.println(age);
}
}
Use Cookie to save sessionID
src\com\wzx\pack02_session_persister\Demo03SetServlet.java
-
Every time you close the browser and access the project, the server will assign a new sessionId.
If you close the browser and restart it,
there is a requirement to access the data of the previous session . Use cookies to save the sessionId to the browser. The sessionid will be automatically placed in the request header when accessing the project. -
getSession() first finds whether there is a session before according to jsessionId, if there is, reuse the session, otherwise create a new one.
-
Cookie cookie = new Cookie("JSESSIONID",jsessionId); 参数名必须是JSESSIONID
@WebServlet("/set_jsessionId")
public class Demo03SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//打开浏览器,向服务器发一个jsessionID,让服务器根据jsessionID打开之前的session
//取里面的数据,得使用技术保存jsessionID在浏览器 Cookie
//1 获取session对象
HttpSession session = request.getSession();
//2 获取session的编号jsessionId
String jsessionId = session.getId();
//3 保存key-value
session.setAttribute("name","jack");
session.setAttribute("age","20");
response.getWriter().println(jsessionId);
Cookie cookie = new Cookie("JSESSIONID",jsessionId);
//存活时间
cookie.setMaxAge(10*60);
//项目地址
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
}
Session invalidation
session.invalidate(); //Let the session fail, the original data is gone
@WebServlet("/clear")
public class Demo04SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//invalidate()清除失效
HttpSession session = request.getSession();
session.invalidate();
}
}
Passivation and activation of Session (understand)
- Closing the service in tomcat will automatically make the session persistence, also called passivation, and vice versa, activation
- This is a session.ser that stores session data in the work directory
- Friends who use idea to publish the project check the log console and find the work directory.
My files are as follows