Spring 조회수 중복 증가 방지하기 cookie 사용

북마크 추가

페이지를 호출 할 때

@RequestMapping(value = "/{classify}/detail/{seq}", method = RequestMethod.GET)
public String selectBoard(@PathVariable String seq,@PathVariable String classify,
Model model,HttpServletRequest req,HttpServletResponse res) {
    // 저장된 쿠키 불러오기
   Cookie cookies[] = req.getCookies();
   Map map = new HashMap();
   if(req.getCookies() != null){
    for (int i = 0; i < cookies.length; i++) {
    Cookie obj = cookies[i];
    map.put(obj.getName(),obj.getValue());
    }
   }

   // 저장된 쿠키중에 read_count 만 불러오기
   String readCount = (String) map.get("read_count");
    // 저장될 새로운 쿠키값 생성
   String newReadCount = "|" + seq;

   // 저장된 쿠키에 새로운 쿠키값이 존재하는 지 검사
   if ( StringUtils.indexOfIgnoreCase(readCount, newReadCount) == -1 ) {
         // 없을 경우 쿠키 생성
         Cookie cookie = new Cookie("read_count", readCount + newReadCount);
        
         res.addCookie(cookie);
          boardDAOService.updateHit(Integer.parseInt(seq)); // 업데이트 실행

   }

  .

  .

  .

 

 

출처 - http://syaku.tistory.com/275  

 

 

AD
관리자
2014-08-11 10:52
SHARE