Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags more
Archives
Today
Total
관리 메뉴

David의 블로그

[JSP/Servlet]상태를 유지할 수 있는 Session, 세션(세번째) 본문

프로그래밍/Jsp_Servlet

[JSP/Servlet]상태를 유지할 수 있는 Session, 세션(세번째)

David 리 2024. 1. 20. 12:28

이번에는

web.xml에 세션타임아웃을 등록하여 하나의 페이지에서 세션종료 알림 팝업 만들기를 해 볼 것이다.

 

서버에서 세션 시간을 정하는 방법은 두 가지가 있다.

1. session.setMaxInactiveInterval()

2. web.xml에 <session-timeout>를 통해 설정한다.

공통점은 세션 시간을 정하는 것이고, 차이점이라 하면

session.setMaxInactiveInterval()은 '초'로 설정해주고, <session-timeout>은 '분'으로 설정이 된다.

 

 

[web.xml에 세션타임아웃을 등록하여 하나의 페이지에서 세션종료 알림 팝업 만들기]

[web.xml]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>seesionPractice</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  
  <session-config>
      <session-timeout>1</session-timeout> <!-- 분으로 설정이 됨. -->
  </session-config>
</web-app>
cs

<sessio-config> 자식 태그로 <session-timeout>태그를 지정한다.

위에서 말한대로 해당 숫자는 '분'을 의미한다.

 

 

 

[aa.jsp]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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>
<script type="text/javascript">
    var secondsBeforeExpire = ${pageContext.session.maxInactiveInterval};
    var timeToDecide = 45// Give client 15 seconds to choose.
    alert(secondsBeforeExpire)
    setTimeout(function() {
        alert('세션 타임아웃까지 ' + timeToDecide + ' 초 남았습니다~!~!~')
    }, (secondsBeforeExpire - timeToDecide) * 1000);
    
    
</script>
</body>
</html>
cs

11Line : web.xml에 등록한 세션 타임아웃 시간을 가져온다.

12Line : 세션 종료 알림 시간.

14 ~ 16Line : 서버에 등록된 세션 타임시간과 세션 종료 알림시간을 뺀 ?초 이후에 실행될 함수.

 

 

[결과]