Spring 세션 값 가져오기 - spring sesyeon gabs gajyeoogi

1. 세션에 Data 저장

session.setAttribute("저장하고자 하는 변수명", 저장변수값);

<Controller>

@RequestMapping(value = "/test.do")
public String test(HttpServletRequest request) throws Exception {
        
    HttpSession session = request.getSession();
    String name = "홍길동";
    session.setAttribute("sessionId", name);
    
    return "test/test";
}

<View   ex.Thymeleaf>

<body>
    <h2 th:text="${sessionId}"></h2>   <!-- 출력값: 홍길동 -->
</body>

2. 세션에 저장된 Data 가져오기

session.getAttribute("저장한 변수명");

<Controller>

@RequestMapping(value = "/test2.do")
public String test2(HttpServletRequest request) throws Exception {
        
    HttpSession session = request.getSession();
    String name = (String) session.getAttribute("sessionId");
        
    System.out.println("==============================");
    System.out.println("세션에 저장 되 있는 변수 : "+name);  // 홍길동 출력
    System.out.println("==============================");
        
    name = "유재석";
    session.setAttribute("sessionId", name);

    return "test/test";
}

<View  ex.Thymeleaf>

<body>
    <h2 th:text="${sessionId}"></h2>   <!-- 출력값: 유재석 -->
</body>

3. 세션 초기화 하기

session.invalidate();

Spring 세션 값 가져오기 - spring sesyeon gabs gajyeoogi
@luke-peters unsplash
[spring] 스프링 세션(session) 생성 방법

스프링에서 세션을 생성하는 방법으로는 아래처럼 생성해주시는 방법이 있고

HttpSession session = request.getSession();

또는 매개변수로 HttpSession session으로 설정해주시면 됩니다.

@RequestMapping(value = "/admin/doLogin", method = {RequestMethod.POST})
public String adminLogin(HttpServletRequest request, Model model, HttpSession session) throws Exception {
}

세션에서 값을 넣고 가져오는 방법
session.setAttribute("name", value); // 세션에 값을 세팅하는 방법
session.getAttribute("name") // 세션에서 값을 가져오는 방법

실제 예시를 보자면 이렇게 해주시면 됩니다.

@RequestMapping(value = "/admin/doLogin", method = {RequestMethod.POST})
public String adminLogin(HttpServletRequest request, Model model, HttpSession session) throws Exception {
    String id = request.getParameter("id");
	session.setAttribute("id", id);                 // 세션에 값을 셋팅하는 방법
    String sessionId = session.getAttribute("id");  // 세션에서 값을 가져오는 방법		
}

스프링에서 세션 사용하기

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;

}

JSP

<body>

<input type="text" value="${ssVar }"/>

</body>

Spring 세션 값 가져오기 - spring sesyeon gabs gajyeoogi

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;

}

Java Console 확인

Spring 세션 값 가져오기 - spring sesyeon gabs gajyeoogi

JSP

<body>

<input type="text" value="${ssVar }"/>

</body>

Spring 세션 값 가져오기 - spring sesyeon gabs gajyeoogi

 3. 세션 초기화 하기

session.invalidate();