쿠키 & 세션 (변수의 범위, 지속시간)
- 세션 : 변수값 -> 서버에 저장(세션). 쿠키도 사용(쿠키에 세션 고유값을 저장)
ex) 로그인 - 로그아웃
쿠키가 저장되는 폴더
쿠키 (cookie)
cookie_before.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookie</title>
</head>
<body>
<form action="cookie_ok.jsp" method="post">
아이디 : <input type="text" name="id" /><br />
이메일 : <input type="text" name="email" /><br />
<input type="submit" value="전송" /><br />
<br />
<a href="cookie_delete.jsp">쿠키삭제</a>
</form>
</body>
</html>
cookie_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String email = request.getParameter("email");
Cookie cookie1 = new Cookie("id", id);
Cookie cookie2 = new Cookie("email", email);
//쿠키 지속시간(초단위). -1로 설정해주면 바로 삭제!
cookie1.setMaxAge(3600);
cookie2.setMaxAge(3600);
response.addCookie(cookie1);
response.addCookie(cookie2);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
쿠키가 저장되었습니다.<br />
<a href="cookie_after.jsp">쿠키확인</a>
</body>
</html>
cookie_after.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
//쿠키를 받아오는 메서드
private String getCookieValue(Cookie[] cookies, String name){
if(cookies == null) return null;
for(Cookie cookie : cookies){
if(cookie.getName().equals(name)){
return cookie.getValue();
}
}
return null;
}
%>
<%
Cookie[] cookies = request.getCookies();
String id = getCookieValue(cookies, "id");
String email = getCookieValue(cookies, "email");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
쿠키내용 값 : <%=id %><br />
쿠키내용 값 : <%=email %><br />
</body>
</html>
cookie_delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Cookie cookie1 = new Cookie("id", "");
Cookie cookie2 = new Cookie("email", "");
cookie1.setMaxAge(0);
cookie2.setMaxAge(0);
response.addCookie(cookie1);
response.addCookie(cookie2);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
쿠키가 삭제되었습니다.<br />
<a href="cookie_after.jsp">쿠키확인</a>
</body>
</html>
세션(session)
session_before.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session</title>
</head>
<body>
<form action="session_ok.jsp" method="post">
아이디 : <input type="text" name="id" /><br />
이메일 : <input type="text" name="email" /><br />
<input type="submit" value="전송" /><br />
<br />
<a href="session_delete.jsp">세션삭제</a>
</body>
</html>
session_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String email = request.getParameter("email");
session.setAttribute("id", id);
session.setAttribute("email", email);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
세션이 저장되었습니다.<br />
<a href="session_after.jsp">세션확인</a>
</body>
</html>
session_after.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = (String)session.getAttribute("id");
String email = (String)session.getAttribute("email");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
세션내용 값 : <%=id %><br />
세션내용 값 : <%=email %><br />
세션 유지시간 : <%=session.getMaxInactiveInterval() %><br />
세션 아이디 : <%=session.getId() %><br />
</body>
</html>
session_delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//기존의 세션객체를 파기하고 새로 생성!
//session id 고유값이 재생성됨!
session.invalidate();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
세션이 삭제되었습니다.<br />
<a href="session_after.jsp">세션확인</a>
</body>
</html>
'JAVA > JSP & Servlet' 카테고리의 다른 글
JSP 예외처리(Exception) 방법 3가지 (0) | 2013.03.20 |
---|---|
세션(session) URL 재작성 (0) | 2013.03.20 |
Servlet(서블릿) 생성 기본! [annotation] (0) | 2013.03.15 |
[펌]JSP 모델1과 모델 2의 비교 (0) | 2013.03.14 |
톰캣에서 JSP 데이터베이스 커넥션 풀 사용하기 (context, 풀링) (0) | 2013.03.14 |