본문 바로가기

JAVA/JSP & Servlet

쿠키(cookie) & 세션(session)

 

쿠키 & 세션 (변수의 범위, 지속시간)

 

  - 쿠키 : 변수값 -> 클라이언트의 브라우져에 저장

  -
세션 : 변수값 -> 서버에 저장(세션). 쿠키도 사용(쿠키에 세션 고유값을 저장)
             ex)
로그인 - 로그아웃

 

 

 

쿠키가 저장되는 폴더

C:\사용자\AppData\Roaming\Microsoft\Windows\Cookies

 

 

 

 

 

쿠키 (cookie)


cookie_before.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>cookie</title>

</head>

<body>

 

<form action="cookie_ok.jsp" method="post">

아이디 : <input type="text" name="id" /><br />

이메일 : <input type="text" name="email" /><br />

<input type="submit" value="전송" /><br />

<br />

 

<a href="cookie_delete.jsp">쿠키삭제</a>

 

</form>

 

 

</body>

</html>

 

 

 

 

 

 

cookie_ok.jsp

<%@ 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");

       

        Cookie cookie1 = new Cookie("id", id);

        Cookie cookie2 = new Cookie("email", email);

       

//쿠키 지속시간(초단위). -1 설정해주면 바로 삭제!

        cookie1.setMaxAge(3600);

        cookie2.setMaxAge(3600);

       

        response.addCookie(cookie1);

        response.addCookie(cookie2);

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

쿠키가 저장되었습니다.<br />

<a href="cookie_after.jsp">쿠키확인</a>

 

</body>

</html> 

 

 

 

 

 

 

 

cookie_after.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

   

<%!

        //쿠키를 받아오는 메서드

        private String getCookieValue(Cookie[] cookies, String name){

               if(cookies == null) return null;

               for(Cookie cookie : cookies){

                       if(cookie.getName().equals(name)){

                              return cookie.getValue();

                       }

               }

               return null;

        }

%>

 

<%

        Cookie[] cookies = request.getCookies();

        String id = getCookieValue(cookies, "id");

        String email = getCookieValue(cookies, "email");

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

쿠키내용 : <%=id %><br />

쿠키내용 : <%=email %><br />

 

 

</body>

</html>

 

 

 

 

 

 

cookie_delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

   

<%

        Cookie cookie1 = new Cookie("id", "");

        Cookie cookie2 = new Cookie("email", "");

       

        cookie1.setMaxAge(0);

        cookie2.setMaxAge(0);

       

        response.addCookie(cookie1);

        response.addCookie(cookie2);

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

쿠키가 삭제되었습니다.<br />

<a href="cookie_after.jsp">쿠키확인</a>

 

</body>

</html>

 

 

 

 

 

 

 

 

세션(session)


 

session_before.jsp

<%@ 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>

 

<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>

 

</body>

</html>

 

 

 

 

 

 

 

 

session_ok.jsp

<%@ 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>JSP</title>

</head>

<body>

 

세션이 저장되었습니다.<br />

<a href="session_after.jsp">세션확인</a>

 

</body>

</html>

 

 

 

 

 

 

session_after.jsp

<%@ 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>JSP</title>

</head>

<body>

 

세션내용 : <%=id %><br />

세션내용 : <%=email %><br />

세션 유지시간 : <%=session.getMaxInactiveInterval() %><br />

세션 아이디 : <%=session.getId() %><br />

 

</body>

</html>

 

 

 

 

 

 

 

 

session_delete.jsp

<%@ 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>JSP</title>

</head>

<body>

 

세션이 삭제되었습니다.<br />

<a href="session_after.jsp">세션확인</a>

 

 

</body>

</html>