Servlet 저장소
appliction : 전역범위에서 사용. WAS가 시작해서 종료할 때 까지 존재
session : 세션범위에서 사용. 접속자(세션 아이디) 마다 공간을 가짐. 세션이 시작해서 종료할 때 까지 존재
cookie : 웹브라우저별로 지정한 path범주에 해당하는 url에서만 사용할 수도 있음. 만료시간까지 존재
appliction
값을 저장
ServletContext appliction = request.getServletContext
application.setAttribute("value",v);
값을 꺼내
int x = application.getAttribute("value")
session
사용 예시
[1] 로그인 시
HttpSession session=request.getSession();
//사용자가 적은 아이디로
String id=request.getParameter("id");
String pwd=request.getParameter("pwd");
MemberDao memberDAO=MemberDao.getInstance();
int result = memberDAO.userCheck(id,pwd);
if (result == 0) {
request.setAttribute("message", "비밀번호가 일치하지 않습니다");
} else if (result == -1) {
request.setAttribute("message", "존재하지 않는 회원입니다.");
}
//그 아이디의 정보를 데려옴
MemberVO memberVO=memberDAO.getMember(id);
if(result == 1){
//아이디 비번이 일치하면
if(memberVO.getPwd().equals(pwd)){
//세션에 memberVO(로그인한 회원 정보)를 loginUser로 저장
session.setAttribute("loginUser", memberVO);
url="LoginOK.jsp";
}
}
[2] 다른 페이지에서 사용 시
// 세션객체 얻어와~
HttpSession session = request.getSession();
// 로그인한 회원 정보 얻어와~
MemberVO loginUser = (MemberVO) session.getAttribute("loginUser");
if (loginUser == null) { // 로그인 안돼있으면 로그인창
url = "login.jsp";
} else {
url = "review/review_write.jsp";
}
[3] jsp에서 사용시
${sessionScope.loginUser}
Cookie
: 사용자(클라이언트) 측에 정보를 저장
-쿠키 저장하기(브라우저에 전달)
//key("c")로 value를 심어서 저장 (문자형만 가능)
Cookie cookie = new Cookie("c",String.valueOf(result));
//addCookie를 이용해 클라이언트로 cookie를 보냄
response.addCookie(cookie);
-쿠키 읽기(쿠키를 요청하면 읽어옴)
//쿠키는 여러개가 있을 수 있기 때문에 배열로 온다.(Map처럼)
Cookie[] cookies = request.getCookies();
String _c = "";
if(cookies != null)
//배열로 왔기 때문에 for문을 돌려서 내가 찾는 값을 찾아줘야함
//key값을 찾아서 그 key값에 해당하는 value를 얻는 것!
for(Cookie cookie : cookies)
if("c".equals(cookie.getName()))
_c = cookie.getValue(); //찾았으면 값을 저장
break;
-쿠키 수정
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length>0){
for(int i=0; i<cookies.length; i++){
if(cookies[i].getName().equals("name")){//cookies중에 name값이 있으면
Cookie cookie = new Cookie("name","Lee"); //이걸루 다시만들어
response.addCookie(cookie);
}
}
}
-쿠키 삭제(키값까지 싹 지우기)
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length>0){
for(int i=0; i<cookies.length; i++){
if(cookies[i].getName().equals("name")){
Cookie cookie = new Cookie("name","Lee");
cookie.setMaxAge(0); //유효시간을 0으로 만들어서 만료시켜버림
response.addCookie(cookie);
}
}
}
메서드
setMaxAge() : 쿠키 유효기간을 설정 합니다.(브라우저를 닫아도 유지됨)
cookie.setMaxAge(60*60) //1시간이 지나면 만료됨
setpath() : 쿠키사용의 유효 디렉토리를 설정(어느 경우에 사용자에게 전달 되어야 하는지 지정)
//모든 경우에 cookie를 가져옴
cookie.setPath("/");
//notice가 포함된 하위 url을 요청할 경우에만 cookie를 가져올 수 있음
cookie.setPath("/notice/");
Cookie cookie1 = new Cookie("name","HGD");
Cookie cookie2 = new Cookie("age","20");
//setPath = 경로를 다르게 해서 쿠키를 저장(경로마다 따로따로 저장하겟다.)
cookie2.setPath(request.getContextPath()+"/path1");//cookie2에만 경로 지정
response.addCookie(cookie1);
response.addCookie(cookie2);
//Set-Cookie: name=HGD
//Set-Cookie: age=20; Path=/web-study-05/path1
setValue() : 쿠키의 값을 설정 합니다.
setVersion() : 쿠키 버전을 설정 합니다.
getMaxAge() : 쿠키 유효기간 정보를 얻습니다.
getName() : 쿠키 이름을 얻습니다.
getValue() : 쿠키의 값을 얻습니다.
//쿠키 만들어주고
Cookie cookie1 = new Cookie("name","HGD");
// response에 추가(클라이언트에 전달)하면
response.addCookie(cookie1);
//cookie.getName()은 name
//cookie.getValue()은 HGD
getPath() : 쿠키사용의 유효 디렉토리 정보를 얻습니다.
getVersion() : 쿠키 버전을 얻습니다.
-Headers
General
Response Headers : 서버가 클라이언트에게 보내는 정보(심어진 쿠키를 볼 수 있음)
Request Headers : 쿠키정보를 요청하고 가져감(전달된 쿠키를 볼 수 있음)