개발새발 블로그

[JavaSpring] 세션과 쿠키

컴퓨터/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>
<% 
	// 쿠키의 값에 띄어쓰기, 특수기호 안됨
	Cookie cookie = new Cookie("myCookie","Apple");
	cookie.setMaxAge(60);
	cookie.setValue("Banana");
	response.addCookie(cookie);
%>
	<h1>쿠키를 사용하여 연결 유지</h1>
	쿠키를 만듭니다<p/>
	쿠키 내용은 <a href="01.2.tasteCookie.jsp">클릭하세요.</a>
</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>
<%
	Cookie[] cookie = request.getCookies();
	if(cookie != null) {
		for(int i=0; i<cookie.length; i++) {
			/* 
			if(cookie[i].getName().equals("myCookie")) {
				out.print("Cookie Name : " + cookie[i].getName() + "<br>");
				out.print("Cookie value : " + cookie[i].getValue() + "<p/>");
			}
			 */

			out.print("Cookie Name : " + cookie[i].getName() + "<br>");
			out.print("Cookie value : " + cookie[i].getValue() + "<p/>");

		}
	}
%>
</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>
<%
	/*
	Cookie cookie = new Cookie("myCookie","Apple");
	response.addCookie(cookie);
	*/
	
	// 한줄로 줄이면
	response.addCookie(new Cookie("NAME","John"));
	response.addCookie(new Cookie("GENDER","Male"));
	response.addCookie(new Cookie("AGE","25"));
%>
	쿠키생성<p/>
	쿠키의 내용은 <a href="02.2.testeCookie.jsp">클릭하세요</a>
</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>
<%-- 
<%
	Cookie[] cookies = request.getCookies();
	if(cookies != null) {
		for(int i=0; i<cookies.length; i++) {
			out.print("Cookie name : " + cookies[i].getName()+ "<p/>");
			out.print("Cookie value : " + cookies[i].getValue()+ "<hr>");
		}
	}
%>
--%>
<% Cookie[] cookies = request.getCookies(); %>
<%!
	public String getCookieValue(Cookie[] cookies, String name) {
		if(cookies != null) {
			for(int i=0; i<cookies.length; i++) {
				if(cookies[i].getName().equals(name))
					return cookies[i].getValue();
			}
		}
		return null;
	}
%>
이름 : <%=getCookieValue(cookies, "NAME") %><p/>
성별 : <%=getCookieValue(cookies, "GENDER") %><p/>
나이 : <%=getCookieValue(cookies, "AGE") %><p/>
</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>
<%
	session.setAttribute("idKey", "kim");
	session.setAttribute("pwKey", "1234");
%>
세션이 생성됨<p/>
세션 정보는 <a href="03.2.testeSession.jsp">클릭하세요</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	Enumeration name = session.getAttributeNames();
	while(name.hasMoreElements()) {
		String sName = (String)name.nextElement();
		String value = (String)session.getAttribute(sName);
		out.print("session name : " + sName + "<p/>");
		out.print("session value : " + value + "<hr>");
	}
%>
</body>
</html>