session 내장 객체
- HttpSession session
- 요청한 사용자에게 개별적으로 접근(나만의 캐비닛)
- 선언한 세션 변수는 전역적 상태로 유지된다
- 요청한 정보의 상태를 유지하기 위해서
- 일정 시간 동안 이벤트가 발생되지 않으면 자동 삭제
<%@ 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>
<h3>session 내장객체</h3>
<%
//세션 내장객체에서 발급해주는 아이디
out.print("세션 임시 아이디 : ");
out.print(session.getId());
out.print("<hr>");
%>
</body>
</html>
//세션변수
//->별도의 자료형이 없다
//->myweb 프로젝트의 모든 페이지에서 공유되는 전역변수 (메일/카페/블로그 등 상태유지에 사용)
session.setAttribute("s_id", "itwill");
session.setAttribute("s_pw", "12341234");
//세션변수값 가져오기
Object obj = session.getAttribute("s_id");
String s_id = (String)obj; //다형성
String s_pw = (String)session.getAttribute("s_pw");
out.print("세션변수 s_id :" + s_id + "<hr>");
out.print("세션변수 s_pw :" + s_pw + "<hr>");
//세션변수 강제 제거(로그아웃할때) -> null값
session.removeAttribute("s_id");
session.removeAttribute("s_pw");
out.print("세션변수 삭제후<hr>");
out.print("세션변수 s_id :" + session.getAttribute("s_id"));
out.print("<hr>");
out.print("세션변수 s_pw :" + session.getAttribute("s_pw"));
out.print("<hr>");
//세션영역에 있는 모든 값 전부 강제 삭제
session.invalidate();
로그인한 후 오랫동안 아무것도 하지 않고 있다가 다시 이용하려는 경우 로그아웃되어서 다시 로그인을 하라고 하는 경우가 있다. 이는 세션이 유지되는 시간이 있기 때문이다.
//세션 시간
out.print("현재 세션 유지 시간 : ");
out.print(session.getMaxInactiveInterval());
out.print("초(30분)");
out.print("<hr>");
유지시간이 30분으로 설정되어 있는데 이 값은 변경할 수 있다.
session.setMaxInactiveInterval(60*10); //10분
out.print("변경된 세션 유지 시간 : ");
out.print(session.getMaxInactiveInterval());
out.print("초(10분)");
out.print("<hr>");
'웹개발 교육 > jsp' 카테고리의 다른 글
[55일] jsp (26) - application, response 내장 객체 (0) | 2022.10.17 |
---|---|
[55일] jsp (25) - web.xml (0) | 2022.10.17 |
[55일] jsp (23) - SCOPE (0) | 2022.10.17 |
[55일] jsp (22) - jsp 내부객체 (0) | 2022.10.17 |
[54일] jsp (21) - myweb 프로젝트(페이징) (0) | 2022.10.14 |