1. Cookie
웹에서 만들어지는 특정한 데이터를 저장하고 싶을 경우가 있다.
이 경우 cookie 와 session 이라는 저장 객체를 사용한다.
저장 위치가 서로 다르므로 사용법과 보안 수준이 다르다.

Client PC 에 저장이 되었을 때 가질 수 있는 특징은?
→ 브라우저를 닫거나 서비스를 종료해도 정보가 남는다.
그로 인한 보안상 취약점은?
→ 공공장소에서 사용하는 PC 의 경우 Cookie 에 저장된 내용을 누구든지 사용할 수 있게 된다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// cookie 는 PC 에 저장되므로 resp 로 저장하고 req 로 가져온다.
// 1. 쿠키 객체화(값에 , 나 - 가 있으면 에러가 난다.)
Cookie cookie = new Cookie("name","hong_in_pyo"); // key-value
// 2. 쿠키 수명 지정
cookie.setMaxAge(10*60); // 초 단위
// 3. 쿠키를 클라이언트(사용자PC) 에 저장
response.addCookie(cookie);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="button" value="쿠키 불러오기" onclick="location.href='cookieResult.jsp'"/>
<input type="button" value="쿠키 예제" onclick="location.href='example/infoInput.jsp'"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 쿠키는 클라이언트로 부터 받아와야 하므로 request 로 불러야 한다.
// 쿠키는 중복된 이름 사용이 불가능하여, 여러 이름으로 저장된다.
Cookie[] cookies = request.getCookies();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
for(Cookie cookie:cookies) {
%>
<p><%=cookie.getName() %> : <%= cookie.getValue()%></p>
<%
}
%>
</body>
</html>
2. Session
Session 은 서버에 저장이 된다.
서비스에서 나가거나 브라우저가 변경될 경우 끊어지게 된다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 세션은 섭에 접속하면 그냥 주워지는 객체이다.
System.out.println("Session ID : " + session.getId());
// 서버를 떠나거나 인위적으로 지울 때까지 유지된다.
// 또는 기본 30분이나 지정된 시간동안 아무런 동작을 하지 않으면 초기화 된다.
// session.setMaxInactiveInterval(10); // 10초간 동작이 없으면 세션 초기화
System.out.println(session.getMaxInactiveInterval() + "초");
// 은행(네이버) 로그인을 생각하자.
// 세션에 저장된 값 확인
String name = (String) session.getAttribute("name");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>세션 테스트</h2>
<a href="sessionSet.jsp">세션에 값 저장</a><br/>
<p>세션에 저장된 name 값 : <%=name %></p>
<a href="sessionDel.jsp">세션에 값 삭제</a><br/>
<a href="sessionInit.jsp">세션 초기화</a><br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// session 에 name 이라는 이름으로 "kim,ji-hoon" 이라는 값을 저장한다.
session.setAttribute("name", "hong,in-pyo");
response.sendRedirect("index.jsp");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.removeAttribute("name");
response.sendRedirect("index.jsp");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 세션 초기화의 경우는 완전히 다른 세션을 받기 때문에 세션 아이디가 변경되어 있다.
session.invalidate();
response.sendRedirect("index.jsp");
%>
3. Scope
JSP 에서는 데이터 저장 시 공유하는 영역들이 있다.
이 영역들은 데이터를 "언제까지 가지고 있는지" 를 중심으로 구분할 수 있다.

<%@ 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>
<form action="application.jsp" method="post">
<table border="1">
<caption>Application 에 저장될 내용</caption>
<tbody>
<tr>
<th>아이디</th>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<th>이름</th>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<th colspan="2"><input type="submit" value="전송"/></th>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
System.out.println(email + " / " + phone);
if(email != null && phone != null) {
session.setAttribute("email", email);
session.setAttribute("phone", phone);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="result.jsp">저장 내용 확인하기</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
System.out.println(id + " / " + name);
if(id != null && name != null) {
application.setAttribute("id", id);
application.setAttribute("name", name);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="session.jsp" method="post">
<table border="1">
<caption>Session 에 저장될 내용</caption>
<tbody>
<tr>
<th>이메일</th>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<th>연락처</th>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<th colspan="2"><input type="submit" value="전송"/></th>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<%@ 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>
<!-- 어플리케이션에 저장된 정보는 서버가 꺼질 때까지 남아 있는다. -->
<h2>application 영역에 저장된 정보</h2>
<p>id : <%=application.getAttribute("id") %></p>
<p>name : <%=application.getAttribute("name") %></p>
<!-- 세션에 저장된 정보는 브라우저가 꺼질 때까지 남아있는다. -->
<h2>session 영역에 저장된 정보</h2>
<p>email : <%=session.getAttribute("email") %></p>
<p>phone : <%=session.getAttribute("phone") %></p>
</body>
</html>'JSP' 카테고리의 다른 글
| 03. Action TAG, JAVA Bean (0) | 2024.03.24 |
|---|---|
| 01. Scriptlet, Request, Reponse (0) | 2024.03.24 |