工作中想用http session,但是遇见了很多问题
cookie和Session其实两者分不开的,cookie存储SessionId,然后Session的内容存在服务器中,如下图
SessionStorage其实这是浏览器(也即客户端)的session存储,和后端(服务端)的session是两回事。SessionStorage和LocalStorage都存储在本地,两者差别见 Window.sessionStorage - Web API 接口参考 | MDN
SessionScope这个就是Spring的注解,把他放在Controller或者Service上,那么在整个会话中,只有这一个Controller
package com.markerhub.controller0;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.SessionScope;
import javax.servlet.http.HttpSession;
@SessionScope
//@Scope(value = "prototype")
@RequestMapping("/")
@RestController
public class UserController0 {
private String test = "123";
@Autowired
private UserService userService;
@RequestMapping("/test")
public Object test(HttpSession httpSession){
userService.userlogin(new User(), httpSession);
test = "456";
return "123 ";
}
}
不过工作中的疑问是,每次我用jsp访问,都会创建一个新的session,导致每次都新建一个controller,所以我觉得很奇怪,jsp默认重新创建新的session?



