Web/Spring
[Spring] Session 사용하기
개쿠
2019. 7. 21. 16:19
728x90
반응형
스프링에서 세션 사용하기
1. 세션에 Data 저장하기
session.setAttribute("저장 하고자 하는 변수이름", 저장변수값);
Java Controller
1
2
3
4
5
6
7
8
9
10
11
12
|
@RequestMapping(value = "/test.do")
public ModelAndView test(HttpServletRequest request) throws Exception {
HttpSession session = request.getSession();
String name = "세션저장하기";
session.setAttribute("ssVar", name);
ModelAndView mv = new ModelAndView();
mv.setViewName("/test/test");
return mv;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
JSP
1
2
3
4
|
<body>
<input type="text" value="${ssVar }"/>
</body>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
2. 세션에 저장된 Data 가져오기
session.getAttribute("저장한 변수 이름");
Java Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@RequestMapping(value = "/test2.do")
public ModelAndView test2(HttpServletRequest request) throws Exception {
ModelAndView mv = new ModelAndView();
HttpSession session = request.getSession();
String name = (String) session.getAttribute("ssVar");
System.out.println("==============================");
System.out.println("세션에 저장 되 있는 변수 : "+name);
System.out.println("==============================");
name = "세션값 변경";
session.setAttribute("ssVar", name);
mv.setViewName("/test/test");
return mv;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
Java Console 확인
JSP
1
2
3
4
|
<body>
<input type="text" value="${ssVar }"/>
</body>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
3. 세션 초기화 하기
session.invalidate();
728x90
반응형