๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring/Boost Course Web

5.1 ์ƒํƒœ์œ ์ง€๊ธฐ์ˆ  - Cookie & Session

1) ์ƒํƒœ์ •๋ณด๋ž€?

์›น์—์„œ์˜ ์ƒํƒœ ์œ ์ง€ ๊ธฐ์ˆ 

  • HTTPํ”„๋กœํ† ์ฝœ์€ ์ƒํƒœ ์œ ์ง€๊ฐ€ ์•ˆ๋˜๋Š” ํ”„๋กœํ† ์ฝœ์ž…๋‹ˆ๋‹ค.
    - ์ด์ „์— ๋ฌด์—‡์„ ํ–ˆ๊ณ , ์ง€๊ธˆ ๋ฌด์—‡์„ ํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    - ์›น ๋ธŒ๋ผ์šฐ์ €(ํด๋ผ์ด์–ธํŠธ)์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ํ•˜๊ณ  ๋‚˜๋ฉด ํ•ด๋‹น ํด๋ผ์ด์–ธํŠธ์™€์˜ ์—ฐ๊ฒฐ์„ ์ง€์†ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ์ƒํƒœ ์œ ์ง€๋ฅผ ์œ„ํ•ด Cookie์™€ Session๊ธฐ์ˆ ์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.

์ฟ ํ‚ค(Cookie)์™€ ์„ธ์…˜(Session)

  • ์ฟ ํ‚ค
    - ์‚ฌ์šฉ์ž ์ปดํ“จํ„ฐ์— ์ €์žฅ
    - ์ €์žฅ๋œ ์ •๋ณด๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋˜๋Š” ์‹œ์Šคํ…œ์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ 
    - ์œ ํšจ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์‚ฌ๋ผ์ง

  • ์„ธ์…˜
    - ์„œ๋ฒ„์— ์ €์žฅ
    - ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์œ ํšจ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

์ฟ ํ‚ค(Cookie) ๋™์ž‘ ์ดํ•ด

์„ธ์…˜์˜ ๋™์ž‘ ์ดํ•ด

HttpSession์ด๋ผ๋Š” ๊ฐ์ฒด: ์„ธ์…˜์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ธฐ ์œ„ํ•ด ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด


2) ์ฟ ํ‚ค๋ž€?

์ฟ ํ‚ค ์ •์˜

  • ํด๋ผ์ด์–ธํŠธ ๋‹จ์— ์ €์žฅ๋˜๋Š” ์ž‘์€ ์ •๋ณด์˜ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. (์ฟ ํ‚คํ•˜๋‚˜: key-value ํ•˜๋‚˜์”ฉ)

  • ํด๋ผ์ด์–ธํŠธ์—์„œ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅ๋  ์ˆ˜ ์žˆ๊ณ , ์„œ๋ฒ„ ๋‹จ์—์„œ ์ „์†กํ•œ ์ฟ ํ‚ค๊ฐ€ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์šฉ ๋ฐฉ๋ฒ•

javax.servlet.http.Cookie
์„œ๋ฒ„์—์„œ ์ฟ ํ‚ค ์ƒ์„ฑ, Reponse์˜ addCookie๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†ก
Cookie cookie = new Cookie(์ด๋ฆ„, ๊ฐ’);
response.addCookie(cookie);
  • ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ๋ธŒ๋ผ์šฐ์ €๋กœ ์ „์†ก๋˜์–ด ์‚ฌ์šฉ์ž์˜ ์ปดํ“จํ„ฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ €์žฅ๋œ ์ฟ ํ‚ค๋Š” ๋‹ค์‹œ ํ•ด๋‹นํ•˜๋Š” ์›น ํŽ˜์ด์ง€์— ์ ‘์†ํ•  ๋•Œ, ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„๋กœ ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค๋Š” ์ด๋ฆ„(name)๊ณผ ๊ฐ’(value) ์Œ์œผ๋กœ ์ •๋ณด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    - ์ด๋ฆ„-๊ฐ’ ์Œ ์™ธ์—๋„ ๋„๋ฉ”์ธ(Domain), ๊ฒฝ๋กœ(Path), ์œ ํšจ๊ธฐ๊ฐ„(Max-Age, Expires), ๋ณด์•ˆ(Secure), HttpOnly ์†์„ฑ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค๋Š” ๊ทธ ์ˆ˜์™€ ํฌ๊ธฐ์— ์ œํ•œ

  • ๋ธŒ๋ผ์šฐ์ €๋ณ„๋กœ ์ œํ•œ ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ๊ฐ€์ ธ๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ฐธ๊ณ  : http://browsercookielimits.squawky.net/

  • ์ฟ ํ‚ค๋Š” (์ด๋ฆ„, ๊ฐ’)์˜ ์Œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค์˜ ์ด๋ฆ„์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž, ์–ธ๋”๋ฐ”๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ •์˜๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด RFC 6265(https://tools.ietf.org/html/rfc6265)) ๋ฌธ์„œ [4.1.1 Syntax] ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

 

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ์ฟ ํ‚ค ์ •๋ณด ์ฝ๊ธฐ

Cookie[] cookies = request.getCookies();

  • ์ฟ ํ‚ค ๊ฐ’์ด ์—†์œผ๋ฉด null์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

  • Cookie๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” getName()๊ณผ getValue()๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ์ฟ ํ‚ค์ •๋ณด๋ฅผ ์ฐพ์•„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค ์‚ญ์ œ ์š”์ฒญ

  • ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์€ ์—†๊ณ , maxAge๊ฐ€ 0์ธ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
Cookie cookie = new Cookie("์ด๋ฆ„", null);
cookie.setMaxAge(0); ๋œป: ์œ ์ง€์‹œ๊ฐ„ 0
response.addCookie(cookie);

์ฟ ํ‚ค์˜ ์œ ํšจ๊ธฐ๊ฐ„ ์„ค์ •

  • ๋ฉ”์†Œ๋“œ setMaxAge()
    - ์ธ์ž๋Š” ์œ ํšจ๊ธฐ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ดˆ ๋‹จ์œ„์˜ ์ •์ˆ˜ํ˜•
    - ๋งŒ์ผ ์œ ํšจ๊ธฐ๊ฐ„์„ 0์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ์ฟ ํ‚ค์˜ ์‚ญ์ œ
    - ์Œ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์ฟ ํ‚ค๊ฐ€ ์‚ญ์ œ

  • ์œ ํšจ๊ธฐ๊ฐ„์„ 10๋ถ„์œผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด
    - cookie.setMaxAge(10 * 60); //์ดˆ ๋‹จ์œ„ : 10๋ถ„
    - 1์ฃผ์ผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด (7*24*60*60)๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

javax.servlet.http.Cookie

 

Spring MVC์—์„œ์˜ Cookie ์‚ฌ์šฉ

  • @CookieValue ์• ๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ
    - ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ CookieValue์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์›ํ•˜๋Š” ์ฟ ํ‚ค์ •๋ณด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์Šต๋‹ˆ๋‹ค.

  • ์ปจํŠธ๋กค๋Ÿฌ๋ฉ”์†Œ๋“œ(@CookieValue(value="์ฟ ํ‚ค์ด๋ฆ„", required=false, defaultValue="๊ธฐ๋ณธ๊ฐ’") String ๋ณ€์ˆ˜๋ช…)


3) ์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•œ ์ƒํƒœ์ •๋ณด ์œ ์ง€ํ•˜๊ธฐ

package kr.or.connect.guestbook.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import kr.or.connect.guestbook.dto.Guestbook;
import kr.or.connect.guestbook.service.GuestbookService;

@Controller
public class GuestbookController {

@Autowired
GuestbookService guestbookService;
@GetMapping(path="/list")
public String list(@RequestParam(name="start", required=false, defaultValue="0") int start, ModelMap model, HttpServletRequest request, HttpServletResponse response) {
    String value = null;
boolean find = false; // ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ฟ ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด?

    Cookie\[\] cookies = request.getCookies();
if(cookies != null) {

for(Cookie cookie : cookies) {

if("count".equals(cookie.getName())) {

find = true;

                value = cookie.getValue();
break;

            }

        }

    }
if(!find) {

value = "1";

    }else { // ์ฟ ํ‚ค๋ฅผ ์ฐพ์•˜๋‹ค๋ฉด.
try {

int i = Integer.parseInt(value);

            value = Integer.toString(++i);

        }catch(Exception ex) {
value = "1";

        }

    }
// ์ฟ ํ‚ค ์ƒ์„ฑ

Cookie cookie = new Cookie("count", value);

    cookie.setMaxAge(60 \* 60 \* 24 \* 365); // 1๋…„ ๋™์•ˆ ์œ ์ง€.

    cookie.setPath("/"); // / ๊ฒฝ๋กœ ์ดํ•˜์— ๋ชจ๋‘ ์ฟ ํ‚ค ์ ์šฉ.
response.addCookie(cookie); // ๋ณ€๊ฒฝ๋œ ์ฟ ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ณด๋‚ด์ค˜์•ผ์ง€. ๊ทธ๋ž˜์•ผ ์ฟ ํ‚ค ๊ต์ฒด.

    List<Guestbook> list = guestbookService.getGuestbooks(start);
int count = guestbookService.getCount();

int pageCount = count / GuestbookService.LIMIT;

if(count % GuestbookService.LIMIT > 0)

        pageCount++;
List pageStartList = new ArrayList<>();

for(int i = 0; i < pageCount; i++) {

        pageStartList.add(i \* GuestbookService.LIMIT);

    }

    model.addAttribute("list", list);

    model.addAttribute("count", count);

    model.addAttribute("pageStartList", pageStartList);

    model.addAttribute("cookieCount", value); // jsp์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฟ ํ‚ค ๊ฐ’์„ model์— ๋‹ด์•„ ์ „์†กํ•œ๋‹ค.
return "list";

}
@PostMapping(path="/write")

public String write(@ModelAttribute Guestbook guestbook, HttpServletRequest request) {

    String clientIp = request.getRemoteAddr();

    System.out.println("clientIp : " + clientIp);

    guestbookService.addGuestbook(guestbook, clientIp);
return "redirect:list";

}
}

4) Session์ด๋ž€?

์„ธ์…˜ ์ •์˜

  • ํด๋ผ์ด์–ธํŠธ ๋ณ„๋กœ ์„œ๋ฒ„์— ์ €์žฅ๋˜๋Š” ์ •๋ณด์ž…๋‹ˆ๋‹ค.

์ด์šฉ ๋ฐฉ๋ฒ•

  • ์›น ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์ธก์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” session id๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„๋Š” session id๋ฅผ ์ด์šฉํ•ด์„œ key์™€ value๋ฅผ ์ด์šฉํ•œ ์ €์žฅ์†Œ์ธ HttpSession์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„๋Š” session id๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„์ธก์— ์š”์ฒญ์„ ๋ณด๋‚ผ๋•Œ session id๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„๋Š” ์ฟ ํ‚ค์— ์žˆ๋Š” session id๋ฅผ ์ด์šฉํ•ด์„œ ๊ทธ ์ „ ์š”์ฒญ์—์„œ ์ƒ์„ฑํ•œ HttpSession์„ ์ฐพ๊ณ  ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์„ธ์…˜ ์ƒ์„ฑ ๋ฐ ์–ป๊ธฐ

 

HttpSession session = request.getSession();
HttpSession session = request.getSession(true); 
// ๋ชจ๋‘ ๋™์ž‘ ๊ฐ™์Œ
  • request์˜ getSession()๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ฒ„์— ์ƒ์„ฑ๋œ ์„ธ์…˜์ด ์žˆ๋‹ค๋ฉด ์„ธ์…˜์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์—†๋‹ค๋ฉด ์ƒˆ๋กญ๊ฒŒ ์„ธ์…˜์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋œ ์„ธ์…˜์ธ์ง€๋Š” HttpSession์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” isNew()๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

HttpSession session = request.getSession(false);
  • request์˜ getSession()๋ฉ”์†Œ๋“œ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ false๋ฅผ ์ „๋‹ฌํ•˜๋ฉด, ์ด๋ฏธ ์ƒ์„ฑ๋œ ์„ธ์…˜์ด ์žˆ๋‹ค๋ฉด ๋ฐ˜ํ™˜ํ•˜๊ณ  ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์„ธ์…˜์— ๊ฐ’ ์ €์žฅ

setAttribute(String name, Object value)
session.setAttribute(์ด๋ฆ„, ๊ฐ’)
  • name๊ณผ value์˜ ์Œ์œผ๋กœ ๊ฐ์ฒด Object๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

  • ์„ธ์…˜์ด ์œ ์ง€๋˜๋Š” ๋™์•ˆ ์ €์žฅํ•  ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์„ธ์…˜์— ๊ฐ’ ์กฐํšŒ

getAttribute(String name) ๋ฉ”์†Œ๋“œ
String value = (String) session.getAttribute("id");
  • ์„ธ์…˜์— ์ €์žฅ๋œ ์ž๋ฃŒ๋Š” ๋‹ค์‹œ getAttribute(String name) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜ํ™˜ ๊ฐ’์€ Object ์œ ํ˜•์ด๋ฏ€๋กœ ์ €์žฅ๋œ ๊ฐ์ฒด๋กœ ์ž๋ฃŒ์œ ํ˜• ๋ณ€ํ™˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฉ”์†Œ๋“œ setAttribute()์— ์ด์šฉํ•œ name์ธ “id”๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๋ฐ”๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋กœ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

์„ธ์…˜์— ๊ฐ’ ์‚ญ์ œ

removeAttribute(String name) ๋ฉ”์†Œ๋“œ
  • name๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์„ธ์…˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

  • invalidate() ๋ฉ”์†Œ๋“œ: ๋ชจ๋“  ์„ธ์…˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

javax.servlet.http.HttpSession
javax.servlet.http.HttpSession

 

์„ธ์…˜์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๋Š” ์ˆœ๊ฐ„ ์ƒ์„ฑ

  • ํŠน๋ณ„ํžˆ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์„ธ์…˜์˜ ์œ ์ง€ ์‹œ๊ฐ„์€ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ 30๋ถ„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

  • ์„ธ์…˜์˜ ์œ ์ง€ ์‹œ๊ฐ„์ด๋ž€ ์„œ๋ฒ„์— ์ ‘์†ํ•œ ํ›„ ์„œ๋ฒ„์— ์š”์ฒญ์„ ํ•˜์ง€ ์•Š๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.

  • 30๋ถ„ ์ด์ƒ ์„œ๋ฒ„์— ์ „ํ˜€ ๋ฐ˜์‘์„ ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ์„ธ์…˜์ด ์ž๋™์œผ๋กœ ๋Š์–ด์ง‘๋‹ˆ๋‹ค.

  • ์ด ์„ธ์…˜ ์œ ์ง€ ์‹œ๊ฐ„์€ web.xmlํŒŒ์ผ์—์„œ ์„ค์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

<session-config>
	<session-timeout>30</session-timeout>
</session-config>

5) Session์„ ์ด์šฉํ•œ ์ƒํƒœ์ •๋ณด ์œ ์ง€ํ•˜๊ธฐ

์‹ค์Šต์ฝ”๋“œ

  • /guess๋กœ ์š”์ฒญ์„ ํ•˜๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ 1๋ถ€ํ„ฐ 100 ์‚ฌ์ด์˜ ์ž„์˜์˜ ๊ฐ’ ์ค‘์˜ ํ•˜๋‚˜๋ฅผ ๋งž์ถฐ๋ณด๋ผ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ํ•ด๋‹น ๊ฐ’์€ ์„ธ์…˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž๋Š” 1๋ถ€ํ„ฐ 100 ์‚ฌ์ด์˜ ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ž…๋ ฅํ•œ ๊ฐ’์ด ์„ธ์…˜ ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด, ์ž…๋ ฅํ•œ ๊ฐ’์ด ์ž‘๋‹ค๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ž…๋ ฅํ•œ ๊ฐ’์ด ์„ธ์…˜ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด, ์ž…๋ ฅํ•œ ๊ฐ’์ด ํฌ๋‹ค๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ž…๋ ฅํ•œ ๊ฐ’์ด ์„ธ์…˜ ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ๋ช‡ ๋ฒˆ์งธ์— ๋งž์ท„๋‹ค๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

GuessNumberController.java

package kr.or.connect.guestbook.controller;
 
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
@Controller
public class GuessNumberController {
 
  @GetMapping("/guess")
  public String guess(@RequestParam(name="number", required=false) Integer number,
      HttpSession session,
      ModelMap model) {
    
    String message = null;
 
    // get๋ฐฉ์‹์œผ๋กœ /guess ๋ฅผ ์š”์ฒญํ•˜๋Š”๋ฐ ํŒŒ๋ผ๋ฏธํ„ฐ number๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š” session์— count๋ฅผ 0์œผ๋กœ randomNumber์—” 1๋ถ€ํ„ฐ 100์‚ฌ์ด์˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    if(number == null) {
      session.setAttribute("count", 0);
      session.setAttribute("randomNumber", (int)(Math.random() * 100) + 1); // 1 ~ 100์‚ฌ์ด์˜ random๊ฐ’
      message = "๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ์ˆซ์ž๋ฅผ ๋งž์ถฐ๋ณด์„ธ์š”.";
    }else {
 
      // numberํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์„ธ์…˜์—์„œ ๊ฐ’์„ ์ฝ์–ด๋“ค์ธ ํ›„, number์™€ ์„ธ์…˜์— ์ €์žฅ๋œ ๊ฐ’์„ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.
      // ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ์ž‘๊ฑฐ๋‚˜ ํฌ๋‹ค๋ฉด ์นด์šดํŠธ๋ฅผ 1์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ 
      // ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด ์„ธ์…˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
      // ๊ฐ ์ƒํ™ฉ์— ๋งž๋Š” ๋ฉ”์‹œ์ง€๋ฅผ message๋ณ€์ˆ˜์— ์ €์žฅ์„ ํ•œ ํ›„ jsp์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ModelMap์˜ addAttribute๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
      int count = (Integer)session.getAttribute("count");
      int randomNumber = (Integer)session.getAttribute("randomNumber");
    
      
      if(number < randomNumber) {
        message = "์ž…๋ ฅํ•œ ๊ฐ’์€ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋Š” ์ˆซ์ž๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค.";
        session.setAttribute("count", ++count);
      }else if(number > randomNumber) {
        message = "์ž…๋ ฅํ•œ ๊ฐ’์€ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋Š” ์ˆซ์ž๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.";
        session.setAttribute("count", ++count);
      }else {
        message = "OK " + ++count + " ๋ฒˆ์งธ ๋งž์ท„์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ์ˆซ์ž๋Š” " + number + " ์ž…๋‹ˆ๋‹ค.";
        session.removeAttribute("count");
        session.removeAttribute("randomNumber");
      }
    }
    
    model.addAttribute("message", message);
    
    return "guess";
  }
}

guess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„</title>
</head>
<body>
  <h1> ์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„.</h1>
  <hr>
  <h3>${message }</h3>

  <c:if test="${sessionScope.count != null}">
    <form method="get" action="guess">
      1๋ถ€ํ„ฐ 100์‚ฌ์ด์˜ ์ˆซ์ž๋กœ ๋งž์ถฐ์ฃผ์„ธ์š”.<br>
      <input type="text" name="number"><br>
      <input type="submit" value="ํ™•์ธ">
    </form>
  </c:if>

  <a href="guess">๊ฒŒ์ž„ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๊ธฐ.</a>
</body>
</html>

 

'Spring > Boost Course Web' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

4.5 Swagger  (0) 2020.11.19
4.4 Controller  (0) 2020.11.19
4.3 WEB API  (0) 2020.11.19
4.2 Layered Architecture  (0) 2020.11.19
4.1 Spring MVC  (0) 2020.11.19
3.3 Spring JDBC  (0) 2020.11.19