<5. 쿠키를 이용한 상태정보 유지하기>
참고: [boostcourse full-stack] http://www.edwith.org/boostcourse-web
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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; //37~45: 기존의 클라이언트가 들고 온 쿠키 //쿠키를 검사한 후 내가 원하는 쿠키가 있는지 검사한 후 쿠키의 값이 무엇인지 얻어낸다 Cookie[] cookies = request.getCookies(); if(cookies != null) { for(Cookie cookie : cookies) { if("count".equals(cookie.getName())) { find = true; value = cookie.getValue(); } } } 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<Integer> 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"; } } |
list.jsp 에서 방명록 전체 수 옆에 방문한 수를 출력하는 el 코드를 추가한다.
- 방명록 전체 수 : ${count }
- 방문한 수 : ${cookieCount }<br><br>
- CookieValue애노테이션 이용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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, @CookieValue(value="count", defaultValue="0", required=true) String value, HttpServletResponse response) { //request 대신에 @CookieValue를 사용한다 // 쿠키 값을 1증가 시킨다. 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<Integer> 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); // 쿠키를 추가한다. 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"; } } | cs |
'BoostCource > Back-end' 카테고리의 다른 글
#05. BE - Session을 이용한 상태정보 유지하기 (0) | 2018.11.19 |
---|---|
#05. BE - Session (0) | 2018.11.19 |
#05. BE - 쿠키 (0) | 2018.11.16 |
#05. BE - 상태정보 (0) | 2018.11.16 |
#03. BE - RestController (0) | 2018.08.03 |