세션 기술은 웹 브라우저를 거치지 않고 웹 서버에 있는 데이터 영역을 통해 데이터를 전달한다. 즉, 첫 번째 웹 컴포넌트가 웹 서버 쪽에 데이터를 저장해 놓고 그 데이터를 읽기 위해 필요한 세션 아이디만 웹브라우저로 보낸다. 그러면 웹 브라우저는 그 아이디를 저장해 두었다가 두 번째 웹 컴포넌트를 호출할 때 웹 서보로 보내 해당 데이터를 찾아 사용한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--session_before.jsp -->
<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>
</form>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
세션이 저장되었습니다.<br />
<a href="session_after.jsp">세션확인</a>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
세션 내용 값 : <%=id %><br />
세션 내용 값 : <%=email %><br />
세션 유지시간 : <%=session.getMaxInactiveInterval() %><br />
세션 아이디 : <%=session.getId()%><br />
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
세션이 삭제되었습니다.<br />
<a href="session_after.jsp">세션확인</a>
</body>
</html>
'JSP' 카테고리의 다른 글
익스프레션 언어 requestScope (0) | 2013.03.21 |
---|---|
서블릿의 라이프 사이클 (0) | 2013.03.21 |
익셉션 처리 실습 (1) | 2013.03.20 |
URL 재작성 메커니즘 (0) | 2013.03.20 |
쿠키 (0) | 2013.03.20 |