-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsessionid.java
More file actions
29 lines (24 loc) · 968 Bytes
/
Jsessionid.java
File metadata and controls
29 lines (24 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.web.session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
// Session的实现是依赖于Cookie的
@WebServlet("/jsessionid")
public class Jsessionid extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取session
HttpSession session = req.getSession();
System.out.println(session);
//期望客户端关闭后,session也能相同
// 创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存
Cookie c = new Cookie("JSESSIONID", session.getId());
c.setMaxAge(60 * 60);
resp.addCookie(c);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}