세션
일반적인 로그인 상태 유지, 장바구니 등의 기능
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>
쿠키이름으로 꺼내어 쓸 수 있다.
'Back-end > Spring Framework' 카테고리의 다른 글
java spring 커넥션 풀 , jdbcTemplate (0) | 2020.08.13 |
---|---|
java spring 리다이렉트, 인터셉트 (0) | 2020.08.12 |
java spring , Controller 설정팁 (0) | 2020.08.09 |
java spring , controller & dao & service 객체 (0) | 2020.08.08 |
java spring 스프링 mvc 웹서비스 STS(spring tools suite) (0) | 2020.08.06 |
댓글