본문 바로가기
Back-end/Spring Framework

java spring 세션 & 쿠키

by javapp 자바앱 2020. 8. 10.
728x90

세션

일반적인 로그인 상태 유지, 장바구니 등의 기능

      

1. HttpServletRequest 

 

       @RequestMapping(value = "/login", method = RequestMethod.POST)

       public String memLogin(Member member, HttpServletRequest request) {

            

             Member mem = service.memberSearch(member);

            

             HttpSession session = request.getSession();

             session.setAttribute("member", mem);

            

             return "/member/loginOk";

       }

 

       

 2. HttpSession 

 

       @RequestMapping(value = "/login", method = RequestMethod.POST)

       public String memLogin(Member member, HttpSession session) {

            

             Member mem = service.memberSearch(member);

            

             session.setAttribute("member", mem);

            

             return "/member/loginOk";

       }

 

 

수정

       @RequestMapping(value = "/modify", method = RequestMethod.POST)

       public ModelAndView modify(Member member, HttpServletRequest request) {

            

             HttpSession session = request.getSession();

            

             Member mem = service.memberModify(member);

             session.setAttribute("member", mem);

            

             ModelAndView mav = new ModelAndView();

             mav.addObject("memAft", mem);

             mav.setViewName("/member/modifyOk");

            

             return mav;

       }

      

    

 

세션 삭제

주로 로그아웃 또는 회원탈퇴

1.

       // Logout

      

       @RequestMapping("/logout")

       public String memLogout(Member member, HttpServletRequest request) {

            

             HttpSession session = request.getSession();

             session.invalidate();

            

             return "/member/logoutOk";

       }

       

2.

       @RequestMapping("/logout")

       public String memLogout(Member member, HttpSession session) {

            

             session.invalidate();

            

             return "/member/logoutOk";

       }

 

 

3. 제거

   // Remove

       @RequestMapping("/removeForm")

       public ModelAndView removeForm(HttpServletRequest request) {

            

             ModelAndView mav = new ModelAndView();

            

             HttpSession session =  request.getSession();

             Member member = (Member) session.getAttribute("member");

             mav.addObject("member", member);

             mav.setViewName("/member/removeForm");

            

             return mav;

       }

      

       @RequestMapping(value = "/remove", method = RequestMethod.POST)

       public String memRemove(Member member, HttpServletRequest request) {

            

             service.memberRemove(member);

            

             HttpSession session = request.getSession();

             session.invalidate();

            

             return "/member/removeOk";

       }

 

 

 

 

 


 

 

 

쿠키

 

       @RequestMapping("/index")

       public String mallIndex(Mall mall,

                    @CookieValue(value="gender", required=false) Cookie genderCookie, 

                    HttpServletRequest request) {  //쿠키가 없을때 false로 exception 안 뜨게

            

             if(genderCookie != null)

                    mall.setGender(genderCookie.getValue());

            

             return "/mall/index";

       }

      

       @RequestMapping("/main")

       public String mallMain(Mall mall, HttpServletResponse response){

            

             Cookie genderCookie = new Cookie("gender", mall.getGender());

            

             if(mall.isCookieDel()) {   //쿠키가 삭제 되었을 때

                    genderCookie.setMaxAge(0);

                    mall.setGender(null); //끊기

             } else {

                    genderCookie.setMaxAge(60*60*24*30);

             }

             response.addCookie(genderCookie);

            

             return "/mall/main";

       }

 

 

jsp 로 받기

    <c:choose>

        <c:when test="${gender == 'M'}">

            남성 상품 노출

        </c:when>

        <c:when test="${gender == 'W'}">

            여성 상품 노출

        </c:when>

        <c:otherwise>

            남성, 여성 상품 노출

        </c:otherwise>

    </c:choose>

 

 

쿠키이름으로 꺼내어 쓸 수 있다.

댓글