- Session 객체
- 브라우저별로 session이 할당됨.
- 클라이언트(브라우저)의 상태 정보를 저장하는 기능.
- 한 사용자와 관련된 정보를 JSP들이 공유하기 위하여 사용됨.
- session이 새로 할당 된 경우 true, session객체가 달라짐을 의미함.
📌
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- /jsp1/src/main/webapp/chap4/session1.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session 내장 객체</title>
</head>
<body>
<%
//세션 유지시간 : 유지시간이 지나면 새로운 세션 할당.
session.setMaxInactiveInterval(10); //초단위. 10초 설정.
%>
<h3>session 객체 : 브라우저 별로 session이 할당 됨.<br>
클라이언트(브라우저)의 상태 정보를 저장하는 기능 </h3>
<%-- session이 새로 할당 된 경우 true, session 객체가 달라짐을 의미. --%>
isNew() : <%=session.isNew() %><br>
<%-- session 생성시간 : 1970년 이후부터 현재까지의 시간을 밀리초로 리턴. --%>
생성 시간 : <%=session.getCreationTime() %><br>
<%-- session 브라우저에서 페이지가 최종 요청된 시간 : 1970년 이후부터 현재까지를 밀리초로 리턴.
세션 유지시간은 최종접속시간부터 계산.
--%>
최종 접속 시간 : <%=session.getLastAccessedTime() %><br>
<%-- 세션의 고유한 id 값. id 값이 다른경우 다른 세션을 의미한다. --%>
session id : <%=session.getId() %><br>
</body>
</html>
- session 예제 - 장바구니
📌
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- /jsp1/src/main/webapp/chap4/sessionForm.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session을 이용한 장바구니 예제</title>
</head>
<body>
<h3>상품 선택</h3>
<form action="sessionAdd.jsp" method="post">
<select name="product">
<option>사과</option><option>배</option><option>감</option>
<option>자몽</option><option>귤</option><option>딸기</option>
</select>
<input type="submit" value="장바구니 추가">
</form> <a href="sessionView.jsp">장바구니 보기</a>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- /jsp1/src/main/webapp/chap4/sessionAdd.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>장바구니 추가</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String product = request.getParameter("product");
//Object session.getAttribute("cart")
// : session 객체에 속성 중 키의 이름이 cart인 객체를 리턴.
List<String> cart = (List<String>)session.getAttribute("cart");
if(cart == null) { //session 속성에 cart 속성이 등록되지 않음.
cart = new ArrayList<String>();
session.setAttribute("cart",cart);
}
cart.add(product);
%>
<script type="text/javascript">
alert("<%=product%> 이(가) 장바구니에 추가되었습니다.")
history.go(-1)
</script>
</body>
</html>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- /jsp1/src/main/webapp/chap4/sessionView.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>장바구니 보기</title>
</head>
<body>
<%
List<String> cart = (List<String>)session.getAttribute("cart");
if(cart == null || cart.size() == 0) { %>
<h3>장바구니에 상품이 없습니다.</h3>
<% } else {%>
<h3>장바구니 상품</h3>
<% for(String p : cart) { %>
<h4><%=p %></h4>
<% }
} %>
<h4>장바구니 내용을 제거</h4>
<% session.removeAttribute("cart"); %>
<h4>application, session 객체의 test 속성 출력하기</h4>
application 객체의 test 속성 : <%= application.getAttribute("test") %><br>
session 객체의 test 속성 : <%= session.getAttribute("test") %><br>
</body>
</html>
'수업(국비지원) > JSP' 카테고리의 다른 글
| [JSP] 내장 객체 - Exam 여행지 정보 예제(redirect, forward) (0) | 2023.04.18 |
|---|---|
| [JSP] 내장 객체 - application 기본 객체: 초기화 파라미터, error 에러처리 (0) | 2023.04.18 |
| [JSP] 내장 객체 - 영역 담당 객체: PageContext 객체 (0) | 2023.04.18 |
| [JSP] 내장 객체 - out 내장 객체 (0) | 2023.04.18 |
| [JSP] 내장 객체 - 페이지 이동 방법 : response 응답 객체, forward (0) | 2023.04.18 |