(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-8242763509535969", enable_page_level_ads: true }); [JSP] 장바구니 1분 지나면 자동적으로 세션 만료하기 :: 깜냥깜냥


지난 포스팅에선 기본적인 장바구니 만들기에 대해 알아보았습니다! 이번 포스팅은 세션을 자동으로 끊어버리는 방식을 추가한 예제입니다!


Login.jsp

 

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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Shopping</title>
</head>
<body>
    <center>
        <h2>Shopping</h2>
        <hr>
        <form method="post" action=setProduct.jsp>
            ID : <input type="text" name=Id> <input type="submit"
                value="확인">
        </form>
    </center>
    <%
        session.setAttribute("Id", request.getParameter("Id"));
    %>
 
</body>
</html>

 

setProduct.jsp

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@page import="com.sun.java.swing.plaf.windows.resources.windows"%>
<%@page import="com.sun.xml.internal.bind.v2.runtime.Location"%>
<%@page import="javafx.scene.control.Alert"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>상품선택/Shopping</title>
</head>
<%
    if (session.getAttribute("Id") != null) {
        session.setAttribute("Id", session.getAttribute("Id"));
    } else {
        session.setAttribute("Id", request.getParameter("Id"));
    }
%>
<%
    if (session.getAttribute("Id") == null || session.getAttribute("Id") == "")
        response.sendRedirect("Login.jsp");
%>
<%
    session.setMaxInactiveInterval(60);
%>
<body>
    <center>
        <h2>Shopping</h2>
        <hr>
 
        ${Id}님 환영합니다~!!!
        <hr>
        <form name="f1" action="add.jsp" method="post">
            <select name=product><option value="딸기">딸기</option>
                <option value="귤"></option>
                <option value="포도">포도</option>
                <option value="파인애플">파인애플</option>
                <option value="사과">사과</option>
                <option value="수박">수박</option>
            </select><input type="submit" value="추가"><br> <a
                href="checkOut.jsp">계산</a>
        </form>
    </center>
</body>
</html>

add.jsp

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
31
32
33
34
<%@page import="java.util.ArrayList"%>
<%@page import="com.sun.corba.se.spi.orbutil.fsm.Action"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>상품추가/Shopping</title>
</head>
<%
    session.setMaxInactiveInterval(60);
%>
<body>
    <%
        String productname = request.getParameter("product");
        ArrayList addlist = (ArrayList) session.getAttribute("productlist");
        if (addlist == null) {
            addlist = new ArrayList();
        }
 
        session.setAttribute("productlist", addlist);
        addlist.add(productname);
    %>
 
 
    <%
        out.println("<script> alert('상품이 추가되었습니다.'); history.go(-1);</script>");
    %>
</body>
</html>


checkOut.jsp

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("euc-kr");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>장바구니/Shopping</title>
</head>
<%
    if (session.getAttribute("Id") == null || session.getAttribute("Id") == "")
        response.sendRedirect("Login.jsp");
%>
<% session.setMaxInactiveInterval(60); %>
<body>
    <center >
        <h2>Shopping</h2>
        <hr>
        장바구니에 담겨진 물건들
        <hr>
        <%
            ArrayList addlist = (ArrayList) session.getAttribute("productlist");
            if (addlist == null) {
                out.println("Empty"+ "<br>");
            } else {
                for (int i = 0; i < addlist.size(); i++) {
                    out.println(addlist.get(i) + "<br>");
                }
            }
        %>
 
        
        <%
        session.setAttribute("Id", session.getAttribute("Id"));
            session.setAttribute("addlist", addlist);
        %>
        <input type="button" name=b1 value="뒤로가기"
            onclick="location.href='setProduct.jsp'">
            
            <input type="button" name=b2 value="로그아웃"
            onclick="location.href='Login.jsp';session.invalidate();">
        </form>
    </center>
</body>
</html>



세션을 자동 중지 하려면 Login.jsp를 제외한 다른 jsp에 <% session.setMaxInactiveInterval(60); %> 추가해주시길 바랍니다.

 

+ Recent posts