Facebook

통계 위젯 (화이트)

155185
388
100194


cookie & session by ToTo

쿠키
서버에서 만들어진 작은 정보의 단위. 서버에서 클라이언트의 브라우저로 전송되어 사용자 컴퓨터에 저장
보안에 취약. 객체저장 안됨. 도저히 내 컴퓨터에서 Cookies 폴더를 찾을 수 없었음.

쿠키 생성
Cookie c = new Cookie("user","lee");

쿠키 유효시간 지정 및 조회
c.setMaxAge(30);
int n = c.setMaxAge();

쿠키 값 수정
c.setValue("kang");

쿠키 저장
response.addCookie(c);

모든 쿠키 조회
Cookie [] cs = request.getCookies();

개별 쿠키 이름 조회
cs[i].getValue();

쿠키 삭제
c.setMaxAge(0);

그래서 쿠키보다 세션을 쓴다.
쿠키 = 클라이언트에 저장. 세션 = 서버에 저장 => 보안

세션은 내부적으로 쿠키매커니즘으로 저장.
브라우저를 클라이언트 하나로 인식.
서버에 부여된 세션마다 고유한 아이디.

세션의 생성
request.getSession()

세션이 존재하면 존재하는 세션을 리턴, 없으면 무조건 생성해서 리턴
= getSession(true)

세션이 존재하면 존재하는 세션을 리턴, 없으면 null을 리턴
= getSession(false)

세션 속성 설정
session.setAttribute("time",new Date());

세션 속성 조회
Date d = (Date) session.getAttribute("time");

세션 유효기간 지정 및 조회
session.setMaxInactiveInterval(30);
session.setMaxInactiveInterval(0); //= KILL
session.setMaxInactiveInterval(-1); //=무한대

int n = session.getMaxInactiveInterval();

세션 모든 속성의 이름 조회
Enumeration e = session.getAttibuteNames();

Enumeration 처리
while(e.hasMoreElements()){
String name = (String) e.nextElements();
if(name.equals("time")){
Data d = session.getAttribute(name);
}
}

세션 ID 조회
String id = session.getId();

세션 생성 시간 조회
long ctime = session.getCreationTime();

세션 종료
session.invalidate();

HttpSession session= req.getSession();
session.setAttribute("user", result);
RequestDispatcher rd = req.getRequestDispatcher(머시기url);
rd.forward(req,res);

....
<% memberVO vo =(memberVO)session.getAttribute("user");
if(vo==null){
....
....
memberVO user =(memberVO)session.getAttribute("user");
ArrayList res =(ArrayList)request.getAttribute("all");%>

<%=user.getName() %>님의 홈페이지


....

출처: http://cbak.blog.me/30085669335

1 2 3 4 5 6 7 8 9 10 다음