[Spring] SessionAttributes, cookie 얻기

*include

 - 여러 JSP를 한번에 보여줄 때.

 - 중복디자인이나 중복코드를 줄일 목적으로 사용.

<%@ include %> - 중복코드를 처리할 때.( 속도 빠름, 변수, method 공유, 지시자 충돌 발생)

 <jsp:include>

 <c:import>

 -legacy : JSP도 연결 가능, Context로도 연결가능

      <jsp:include page=“/경로/JSP”/>

      <jsp:include page=“/Mapping”/>

 -boot : “/경로/JSP으로는 연결할 수 없다.( Mapping명으로만 연결 가능 )

    <c:import page=“/Mapping”/>

    <jsp:include page=“”/> => Mapping 명은 사용할 수 없고, 상대경로로 직접 연결가능

   

*관계유지

 - 무상태인 웹에서 접속자의 정보를  저장하기 위해 Session, cookie

 

*session

 -접속자의 정보( 접속자 웹 브라우저에 id를 부여)서버측 메모리에 저장하는 기술.

 -브라우저를 구분하지 않고, 정보를 저장할 수 있다.

 -사용할 수 있는 객체 : HttpSession, Model, ModelAndView

*HttpSession사용

 1. Controllermethod에서 HttpSession을 매개변수로 선언

   @GetMapping(“.. “)

   public String method( HttpSession session){

    1.session의 생존시간 설정.

      session.setMaxInactiveInterval( );

    2.세션 값 할당

      session.setAttribute(“이름”, );

    3.세션 값 얻기

      (Casting)session.getAttribute(“이름”)

    4.세션 값 삭제

      session.remoteAttribute(“이름”)

    5.세션 무효화

      session.invalidate();

  }

 

*@SessionAttributes

 - Model, ModelAndView에 값을 설정하면 request scope객체에 값이 설정된다.

 - class @SessionAttributes(  ) 를 사용하면 Model, ModelAndView에 설정된 값이

    sessionrequest에 동시에 할당된다.

사용법)

 1.SessionAttributes선언

  @Controller

  @SessionAttributes(“이름”)  

  public class TestController{

  

  2. method의 매개변수로 Model 선언

   @GetMpping( .. )

  public String method( Model model ){

  

  3.SessionAttributes에 설정된 이름과 동일한 이름으로 값 설정

  

   model.addAttribute(“이름”, ); //request scopesession scope가 동시에 할당된다.

 

*값 얻기

*Controller  값 얻기

 -HttpSession사용.

  public String method(HttpSession session){

    session.getAttribute(“이름”);

 -Model 사용( Spring 5.2버전에서 부터 지원 )

 public String method(Model model){

    model.getAttribute(“이름”);

 

*JSP에서 값 얻기

-scriptlet

 <% session.getAttribute(“이름”); %>

-EL

  ${ sessionScope.이름 }

 

-세션 삭제.

 - HttpSession 인터페이스 제공하는 method로는 삭제 할 수 없다.

 - org.springframework.web.bind.support.SessionStatus인터페이스를 사용하여 삭제.

 사용법)

 1.SessionStatus를 매개변수로 하는 methd 생성

    public String method( SessionStatus ss ){

      ss.setComplete();

 

*Cookie

 - 접속자의 정보를 접속자 HDDFile로 저장하는 기술.( 문자열로만 저장가능 )

 - HttpServletRequest, HttpServletResponse, @CookieValue annotation사용.

*사용법)

 쿠키 심기)

1.Controllermethod에서 HttpServletResponse를 선언

  @GetMapping( .. )

  public String method( HttpServletReponse response){

 2.쿠키생성

  Cookie cookie=new Cookie(“이름”, ””);

 3. 생존시간 설정

   cookie.setMaxAge( );

 4. 쿠키 심기

   reponse.addCookie(  cookie );

 

*쿠키읽기

 -HttpServletRequest

 1. Controller method에서 HttpServletRequest 정의

  @GetMapping( .. )

  public String method( HttpServletRequest request){

 2.쿠키읽기

   Cookie[] cookies=request.getCookies();

 3. 반복

  for( Cookie tempCookie : cookies ){

   4. 쿠키 정보얻기

   -쿠키명얻기

    cookie.getName()

   -쿠키의 값 얻기

    cookie.getValue()

  }

 

*annotation사용)

  method의 매개변수 앞에 선언)

 사용법)

 1.Controller method에서 매개변수앞에

   @CookieValue() 선언

@GetMapping( .. )

public String method(

   @CookieValue(value=“이름”,defaultValue=“기본값”) String 변수명){

   변수명

 

*EL에서 쿠키의 값을 얻기

${ cookie.쿠키의이름 }   이름에 해당하는 쿠키가 존재하는지 비교할 때

${ cookie.쿠키의이름.value }   이름에 해당하는 쿠키의 값을 얻을 때.