IT 공부/JSP

Cookies

toraa 2022. 8. 1. 17:00

쿠키
 ; 웹 브라우저에 저장되는 작은 크기의 텍스트 데이터이다.

1. 생성 : Cookie 클래스   -   request.getCookie( )   >>   Cookie[ ] cookie


2. 메소드
    . getName() : 쿠키 이름
    . getValue(): 쿠키값 읽기
    . setValue(String value)
    . setMaxAge(int time) : 유효시간을 초단위로 설정

                                         음수로 처리시, 브라우저가 닫히면 쿠키삭제 (기본값-1)


3. 클라이언트 전송
    . response.addCookie();


4. 삭제되었거나 유효시간이 종료되지 않았다면, 같은 브라우저 서로 다른 페이지에서도 공유할 수 있다.


5. JSESSIONID 

    - 톰캣 컨테이너에서 세션을 유지하기 위해 발급되는 키
    - 세션의 저장소를 생성할 때 사용하는 키
    - 이후 해당 저장소에 접근할때도 사용된다.


1. 쿠키 객체 생성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>쿠키</title>
</head>
<body>

	<% 
		//쿠키객체생성          cookies --- > [new Cookie()][new Cookie()]...
		
		Cookie cookie_name = new Cookie("name","deok");
		Cookie cookie_age = new Cookie("age","23");
		
		response.addCookie(cookie_name);
		response.addCookie(cookie_age);		
	%>
	<a href="jsp2.jsp">쿠키확인하기</a>
	
</body>
</html>

2. 쿠키

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookies</title>
</head>
<body>
	<%
		Cookie[] cookies = request.getCookies();
		
		if(cookies != null && cookies.length >0){
			for(int i=0; i<cookies.length; i++){
				out.print(cookies[i].getName());
				out.print(":");
				out.print(cookies[i].getValue());
				out.print("<br>");
			}
		}
	%>
</body>
</html>

3. 쿠키 변경 확인

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>쿠키 변경 확인하기</title>
</head>
<body>
	<%
		Cookie cookie = new Cookie("name","web");
		response.addCookie(cookie);
	%>
	<a href="jsp2.jsp">쿠키 변경 확인하기</a>
</body>
</html>

4. 쿠키 삭제

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>쿠키삭제하기</title>
</head>
<body>
	<%
		Cookie[] cookies = request.getCookies();
	
		if(cookies != null && cookies.length >0){
			for(int i=0; i<cookies.length; i++){
				
				if(cookies[i].getName().equals("name")){
					Cookie cookie = new Cookie("name","");
					cookie.setMaxAge(0);
					response.addCookie(cookie);
					
					out.print("쿠키가 삭제되었습니다.");
				}
			}
		}
	%>
	<br>
	<a href=jsp2.jsp>쿠키확인하기</a>
</body>
</html>

5. 쿠키 확인

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>쿠키확인하기</title>
</head>
<body>
	<%
		Cookie cookie = new Cookie("name","jsp");
		cookie.setMaxAge(60*60*24);
		response.addCookie(cookie);
	%>
	<a href="jsp2.jsp">쿠키확인하기</a>
</body>
</html>

쿠키를 활용한 로그인/로그아웃

 

1. 로그인창

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 창</title>
</head>
<body>
	<form action="jsp7.jsp" method="post">
		아 이 디 : <input type="text" name="id"><br>
		비밀번호 : <input type="password" name="pw"><br><br>
		
		
		<input type="submit" value="확인">
	</form>
</body>
</html>

2. 로그인 실패

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 실패</title>
</head>
<body>
	<%
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		if(id.equals("admin") && pw.equals("1234")){
			Cookie cookie = new Cookie("ID","ADMIN");
			response.addCookie(cookie);
			
			out.print("<h2>환영합니당</h2>");
		}
		else{
			out.print("<h3>로그인에 실패하였습니당 ㅠㅅㅠ</h3>");
		}
	%>
	<a href="jsp8.jsp">메인창으로 이동합니당</a>
	
</body>
</html>

3. 로그아웃 상태입니다

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그아웃 상태입니당</title>
</head>
<body>
	<%
		Cookie[] cookies = request.getCookies();
		boolean isLogin = false;
		
		if(cookies != null && cookies.length>0){
			for(int i=0; i<cookies.length; i++){
				String cookies_name = cookies[i].getName();
				String cookies_value = cookies[i].getValue();
				
				if(cookies_name.equals("ID") && cookies_value.equals("AdMIN")){
					isLogin = true;
					break;					
				}
			}
		}
		if(isLogin){
			out.print("<h2>로그인 상태입니당</h2>");
		}
		else{
			out.print("<h2>로그아웃 상태입니당</h2>");
		}
	%>
	<a href="jsp9.jsp">로그아웃</a>
	
</body>
</html>

4. 로그아웃 되었습니당

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그아웃 되었습니당</title>
</head>
<body>
	<%
		Cookie cookie = new Cookie("ID","ADMIN");
		cookie.setMaxAge(0);	//로그아웃
		response.addCookie(cookie);
	%>
	<h3>로그아웃 되었습니당ㅇㅅㅇ 다시 방문해주세요!</h3>
</body>
</html>