Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

Everything has an expiration date

[JSP & JDBC & Oracle] 20231215 - [프로그램 소스] - TestApplication, TestSetCookie, TestGetCookie, TestRemoveCookie 본문

[JSP & JDBC & Oracle]/Program source (JSP & JDBC & Oracle)

[JSP & JDBC & Oracle] 20231215 - [프로그램 소스] - TestApplication, TestSetCookie, TestGetCookie, TestRemoveCookie

Jelly-fish 2023. 12. 18. 01:58

01234
TestSetCookie, TestGetCookie, TestRemoveCookie, TestAppapplication

 

 

 

WebApp15


 


 

TestSetCookie.jsp
▷ 쿠키 객체 생성 (*서버에 설정) : `Cookie c = new Cookie("쿠키이름", "쿠키 값");`
▷ 쿠키 시간 설정(* 서버에 설정, 초 단위) : `Cookie.setMaxAge(쿠키 생존 초)`
▷ 쿠키 추가 : `response.addCookie(Cookie 객체)`

 

<%@ page contentType="text/html; charset=UTF-8"%>
<% 
	request.setCharacterEncoding("UTF-8");

	// 쿠키 생성 (서버에 생성된 쿠키)
	// 서블릿 컨테이너에 저장. 『서버』에 저장.
	Cookie c = new Cookie("cookie_test", "studyCookie");
	//                       쿠키이름     쿠키 값
	
	
	// 쿠키 설정 (서버에 생성된 쿠키에 대한 설정)
	c.setMaxAge(3600);		// 쿠키 1시간 유지 (서버에서 설정한 것이다.)
	
	
	// 쿠키 추가 (서버에서 생성되고 설정된 쿠키를 클라이언트에 전달(심기))
	response.addCookie(c);
	// 클라이언트는 자신의 쿠키만 갖고 있는 것이 아니다.
	// 다른 쿠키도 다 갖고있다. 서버에 쿠키가 있기 때문에...
	
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestSetCookie.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>

<div>
	<h1>쿠키 설정 및 추가</h1>
	<hr>
</div>

<div>
	<a href="TestGetCookie.jsp"><button type="button" class="btn">쿠키 정보 확인</button></a>
	<a href="TestRemoveCookie.jsp"><button type="button" class="btn">쿠키 정보 삭제</button></a>
</div>




</body>
</html>

 


 

TestGetCookie.jsp
▷  요청 객체(request)의 모든 쿠키를 배열로 반환 받기 : `request.getCookies()`
<%@ page contentType="text/html; charset=UTF-8"%>
<% 
	request.setCharacterEncoding("UTF-8");

	Cookie[] ck = request.getCookies();
	// 쿠키 배열 반환
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestGetCookie.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>


<div>
	<h1>쿠키 정보 얻어내기</h1>
	<hr>
</div>

<div>
	<table class="table">
		<tr>
			<th style="width: 120px;">쿠키 이름</th>
			<th>쿠키 값</th>
		</tr>
		<%
		for (Cookie c : ck)
		{
		%>
		<tr>
			<td><%=c.getName() %></td>
			<td><%=c.getValue() %></td>
		</tr>
		<%
		}
		%>
	</table>
</div>


<div>
	<a href="TestSetCookie.jsp"><button type="button" class="btn">쿠키 정보 설정</button></a>
	<a href="TestRemoveCookie.jsp"><button type="button" class="btn">쿠키 정보 삭제</button></a>
</div>




</body>
</html>

 

TestRemoveCookie.jsp
▷ 쿠키 삭제 (비어있는 쿠키로 덮어쓰기) 
     ① 새로운 쿠키 객체 생성 : `Cookie killCookie = new Cookie("쿠키 이름", "");`
          → ★ 이 때, 쿠키의 이름은 덮어씌우려는 쿠키의 이름과 같아야 한다.
     ②  새로운 쿠키 객체의 유효 시간(expiry)을 0 으로 설정. → 생성과 동시에 제거
     ③  새롭게 생성한 비어있는 쿠키 객체를 클라이언트에게 전송할 응답 객체(response)에 추가
           `response.addCookie(killCookie)`

 

★ 『`setMaxAge(int expiry)`』 메소드

   ⓐ `expiry`가 『음수』일 때 브라우저가 종료되면 쿠키가 제거된다.
   ⓑ `expiry`가 『0』일 때 생성과 동시에 쿠키가 제거된다.
   ⓒ `expiry`가 『양수』일 때 초 단위로 해당 시간만큼 쿠키가 유지된다.

 

<%@ page contentType="text/html; charset=UTF-8"%>
<% 
	request.setCharacterEncoding("UTF-8");

	// 쿠키는 제거하는 기능을 가진 메소드가 없다.
	// → 비어있는 쿠키로 덮어쓰기 하는 형태로 처리한다.
	
	// 비어있는 내용으로 기존의 쿠키를 덮어쓰기 하는 개념
	Cookie killCookie = new Cookie("cookie_test", "");
	
	killCookie.setMaxAge(0);
	response.addCookie(killCookie);
	//-- 처음 쿠키를 구성할 때에도 『addCookie()』로 처리하였기 때문에
	//   삭제할 때에도 같은 이름의 쿠키를 『addCookie()』로 처리해야 안정적으로 삭제.
	//   즉, 내용이 포함된 쿠키를 심었다가...
	//   이번에는 내용이 비어있는 쿠키를 심는다는 개념.
	
	// ※ 여기서 쿠키 삭제는 클라이언트 입장에서의 삭제이다.
	//    한 명의 클라이언트가 쿠키를 삭제했다고 해서
	//    다른 클라이언트들의 쿠키도 삭제되면 안되기 때문에
	//    이 쿠키의 정보는 로컬 PC 에서 컨트롤하게 된다. (브라우저가...)
	


%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestRemoveCookie.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>


<div>
	<h1>쿠키 정보 삭제하기</h1>
	<hr>
</div>

<div>
	<h2>성공적으로 쿠키를 제거했습니다~!!!</h2>
</div>


<div>
	<a href="TestSetCookie.jsp"><button type="button" class="btn">쿠키 정보 설정</button></a>
	<a href="TestGetCookie.jsp"><button type="button" class="btn">쿠키 정보 확인</button></a>
</div>




</body>
</html>

 

 



0
TestApplication


 

★ TestApplication.jsp (머지 이해가 잘 안 가...)

 

▷ `application.setAttribute("변수명", "저장값")`
      - 어플리케이션값을 저장한다.

▷ `application.getAttribute("변수명")`
     - setAttribute()로 저장할때 사용했던 변수값으로 어플리케이션저장된 데이터를 불러온다.

▷ `application.removeAttribute("변수명")`
     - 해당 변수명으로 저장된 어플리케이션 값을 삭제한다.

▷ `application.getRealPath("/")`
      - 웹 서버 실제 경로를 문자열로 반환한다.
▷ `application.log("문자열")`
      - log 기록에 "문자열"을 작성한다.
▷ `request.getRemoteAddr()`
      - 클라이언트의 IP 주소 값을 얻을 수 있다.
<%@ page contentType="text/html; charset=UTF-8"%>
<% 
	// 접속자 수
	int n = 0;

	String count = (String)application.getAttribute("count");
	
	
	if (count != null)
		n = Integer.parseInt(count);
	
	n++;
	
	application.setAttribute("count", Integer.toString(n));
	
	
	// 웹 서버 실제 경로를 찍은 것.
	String realPath = application.getRealPath("/");
	
	// 접속자가 누구인지 알 수 있는것
	application.log("접속자 : " + request.getRemoteAddr());
	// 클라이언트가 무언가를 요청할 때는 Http 헤더에 실제 어느 주소에 어떤 인코딩 방식으로 했는지 등
	// 나머지 속성값을 들고온다.
	
	// 원격지에 있는 주소~~
	
	//-----------------------------------------------
	// 접속자 : 0:0:0:0:0:0:0:1 → localhost의 주소!
	//-----------------------------------------------



%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TestApplication.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>

<div>
	<h1>Application 을 활용한 접속자 수 체크</h1>
	<hr>
</div>

<div>
	<h2>총 접속자 : <%=n %></h2>
	<h2>웹 서버 실제 경로 : <%=realPath %></h2>
</div>


</body>
</html>

`