【发布时间】:2019-05-19 14:22:24
【问题描述】:
我的目标是使用 JSESSIONID 在 java 中创建一个会话,我正在传递请求 cookie,以便当我再次发送请求时,我可以每次都使用相同的会话,并且不会使用新的会话 ID 创建新会话。
public ObjectRestResponse<String> storeToken(StoreMobileTokenRequest tokenRequestBody, HttpServletRequest request) {
ObjectRestResponse<String> response = new ObjectRestResponse<String>();
System.out.println("SessionIdFromRequest"+request.getCookies());
HttpSession session = request.getSession();
System.out.println("SessionId"+session.getId());
HashMap<String, String> hmpAttribute =null;
if(session.getAttribute("NotificationToken") == null) {
hmpAttribute = new HashMap<String, String>();
hmpAttribute.put(tokenRequestBody.getEmpNo(), tokenRequestBody.getTokenId());
session.setAttribute("NotificationToken", hmpAttribute);
}
else {
hmpAttribute = (HashMap<String, String>) session.getAttribute("NotificationToken");
hmpAttribute.put(tokenRequestBody.getEmpNo(), tokenRequestBody.getTokenId());
session.setAttribute("NotificationToken", hmpAttribute);
}
response.setData("Success");
return response;
}
但是对于来自 Rest 客户端的每个新请求,都会创建一个新会话,而不是使用旧会话。
请告诉我我做错了什么或我需要做什么。
【问题讨论】:
标签: java rest httprequest session-cookies jsessionid