JSP + Servlet | ๐Ÿช ์ฟ ํ‚ค ์ƒ์„ฑ, ์‚ฌ์šฉ, ์‚ญ์ œ

ํŒŒ๊ณผยท2022๋…„ 7์›” 13์ผ
0

JSP + Servlet

๋ชฉ๋ก ๋ณด๊ธฐ
10/33
post-custom-banner

๐Ÿช ์ฟ ํ‚ค

์ธํ„ฐ๋„ท์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์›น ์„œ๋น„์Šค๋กœ๋Š” FTP, HTTP ๋“ฑ์ด ์žˆ๋‹ค. ์ด๋“ค ์„œ๋น„์Šค๊ฐ€ ์ง€์ผœ์•ผ ํ•˜๋Š” ๊ทœ์น™์„ ํ”„๋กœํ† ์ฝœ(ํ‘œ์ค€ ์ธํ„ฐ๋„ท ๊ทœ์•ฝ)์ด๋ผ๊ณ  ํ•œ๋‹ค.

  • FTP (File Transfer Protocol) ์›๊ฒฉ ์„œ๋ฒ„ ์‚ฌ์ด์— ํŒŒ์ผ์„ ๊ตํ™˜ํ•˜๊ธฐ ์œ„ํ•œ TCP/IP ํ”„๋กœํ† ์ฝœ.
  • HTTP (Hyper Text Transfer Protocol) HTML ๋ฌธ์„œ์˜ ์†ก์ˆ˜์‹ ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœํ† ์ฝœ.

HTTP๋Š” ๋น„์—ฐ๊ฒฐ(connectionless) ์ง€ํ–ฅํ˜• ํ†ต์‹  ํ”„๋กœํ† ์ฝœ๋กœ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๋ฉด ์›น์„œ๋ฒ„๊ฐ€ ํ•ด๋‹น ํŽ˜์ด์ง€๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†ก(์‘๋‹ต)ํ•œ ํ›„ ์—ฐ๊ฒฐ์„ ๋Š๋Š”๋‹ค. ๋˜ํ•œ ์„œ๋ฒ„๋Š” ์ด์ „ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜์ง€ ์•Š๊ณ  ๋งค ์š”์ฒญ๋งˆ๋‹ค ์ƒˆ ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•˜๊ณ  ์‘๋‹ต ํ›„ ์—ฐ๊ฒฐ์„ ๋Š๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ์ด๋ฅผ ๋ฌด ์ƒํƒœ(stateless) ์„œ๋ฒ„๋ผ๊ณ  ํ•œ๋‹ค.
๋”ฐ๋ผ์„œ ์„œ๋กœ ์—ฐ๊ด€ ์žˆ๋Š” ์ •๋ณด์˜ ํŽ˜์ด์ง€๋“ค์„ ์ ‘์†ํ•  ๋•Œ ์ด์ „ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ธฐ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค.

์ฟ ํ‚ค๋Š” HTTP์˜ ๋น„์—ฐ๊ฒฐ์ง€ํ–ฅํ˜• ์„œ๋น„์Šค์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
์ฟ ํ‚ค๋Š” ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ์˜ ํ•˜๋“œ๋””์Šคํฌ์— ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด ์„œ๋ฒ„์˜ ๋™์ž‘์— ๋ฌด๋ฆฌ๋ฅผ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

์ฟ ํ‚ค์˜ ํฌ๊ธฐ๋Š” 4KB์ดํ•˜๋กœ ์ œํ•œ๋˜์–ด ์žˆ๊ณ , 300๊ฐœ๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ ์ •๋ณด ๋ฐฐ์—ด์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ตœ๋Œ€ ์šฉ๋Ÿ‰์€ 1.2MB. ์˜ค๋ž˜๋œ ์ •๋ณด๋Š” ์ž๋™์œผ๋กœ ์‚ญ์ œ๋œ๋‹ค.

๐Ÿช ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅํ•˜๊ธฐ(์ฟ ํ‚ค ์ƒ์„ฑํ•˜๊ธฐ)

  1. ์ฟ ํ‚ค ๊ฐ์ฒด ์ƒ์„ฑ
Cookie(java.lang.String name, java.lang.String value)
  1. ์ฟ ํ‚ค์— ์†์„ฑ ๊ฐ’ ์„ค์ •
Cookie info = new Cookie("test", "pink");
  1. response ๊ฐ์ฒด์˜ addCookie() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฟ ํ‚ค๋ฅผ ์ถ”๊ฐ€
    ์ฟ ํ‚ค๊ฐ์ฒด์ด๋ฆ„.์ฟ ํ‚ค๊ด€๋ จ๋ฉ”์†Œ๋“œ() ๋กœ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

์˜ˆ์ œ

response๊ฐ์ฒด์˜ addCookie๋ฉ”์†Œ๋“œ๋กœ info(Cookie ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋œ ๊ฐ์ฒด)๋ฅผ ์„ค์ •ํ•˜๋ฉด, ํด๋ผ์ด์–ธํŠธ์˜ ์ผ์ • ํด๋”์— info ์ฟ ํ‚ค์˜ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๊ฒŒ ๋œ๋‹ค.

setCookies.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie &amp; Session</title>
</head>
<body>
<%
	//1. Cookie ๊ฐ์ฒด ์ƒ์„ฑ
	Cookie c = new Cookie("id", "pinksung");
	//2. ์œ ํšจ๊ธฐ๊ฐ„ ์„ค์ •(1๋…„)
	c.setMaxAge(365*24*60*60);
	//3. ํด๋ผ์ด์–ธํŠธ์— ์ฟ ํ‚ค ์ „์†ก
	response.addCookie(c);
	//4. ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „์†ก
	response.addCookie(new Cookie("pw", "test1234"));
	response.addCookie(new Cookie("age", "20"));
%>
<h3>์ฟ ํ‚ค ์„ค์ •</h3>
</body>
</html>

f12 - application - cookie์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿช ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ฟ ํ‚ค ์ •๋ณด ์–ป์–ด์˜ค๊ธฐ

  1. ์ฟ ํ‚ค ๊ฐ์ฒด๋ฅผ ์–ป์–ด์˜จ๋‹ค.
Cookie[] cookies = request.getCookies();
  1. ์ฟ ํ‚ค ๊ฐ์ฒด์— ์„ค์ •๋œ ๊ฐ’์„ ์•Œ์•„๋‚ธ๋‹ค.
for (Cookie c : cookies){
	out.println(c.getName()+" : " + c.getValue() + "<br>");
};

์˜ˆ์ œ

์œ ํšจ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋‹ซ์•˜์„ ๋•Œ ์ •๋ณด๊ฐ€ ๋‚ ์•„๊ฐ.
๋ธŒ๋ผ์šฐ์ € ๋‹ค ๊ป๋‹ค๊ฐ€ ๋‹ค์‹œ ์ผœ๋ณด๋ฉด id๊ฐ’๋งŒ ๋ณด์ธ๋‹ค.

getCookies.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie &amp; Session</title>
</head>
<body>
<h3>ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์–ป์–ด์˜จ Cookie</h3>
<%
	Cookie[] cookies = request.getCookies();
	for (Cookie c : cookies){
		out.println(c.getName()+" : "+c.getValue() + "<br>");
	}
%>
</body>
</html>

๐Ÿช ์ฟ ํ‚ค ์‚ญ์ œํ•˜๊ธฐ

์ฟ ํ‚ค๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งŒ๋ฃŒ์‹œํ‚จ๋‹ค.
setMaxAge()์— ์œ ํšจ๊ธฐ๊ฐ„์„ 0์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋งŒ๋ฃŒ๋œ๋‹ค.

์˜ˆ์ œ

์ž‘์„ฑ ํ›„ set - get - remove ์ˆœ์œผ๋กœ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด, id๊ฐ’๋งŒ ์ œ์™ธํ•˜๊ณ  ์ฟ ํ‚ค ๊ฐ’๋“ค์ด ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

removeCookies.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie &amp; Session</title>
</head>
<body>
	<%
		Cookie cookie = new Cookie("id", "");
		cookie.setMaxAge(0); //์ฟ ํ‚ค์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งŒ๋ฃŒ์‹œํ‚ด
		response.addCookie(cookie);
	%>
	
	<h3> id์ฟ ํ‚ค๊ฐ€ ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. </h3>
	<a href="240_getCookies.jsp">
		์ฟ ํ‚ค ์‚ญ์ œ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ํด๋ฆญํ•˜์„ธ์š”.
</a>
</body>
</html>

๐Ÿช ์ „์ฒด ์ฟ ํ‚ค ์‚ญ์ œํ•˜๊ธฐ

for๋ฌธ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์™€ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งŒ๋ฃŒ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ

<%@ 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++){
			//์ฟ ํ‚ค๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์™€ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋งŒ๋ฃŒ์‹œํ‚ด
			cookies[i].setMaxAge(0) ;
			//ํด๋ผ์ด์–ธํŠธ์— ํ•ด๋‹น ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์ „์†ก (response ๊ฐ์ฒด ํ—ค๋”์— ์ถ”๊ฐ€)
			response.addCookie(cookies[i]);
		}
	}
%>
<script>
	alert("๋กœ๊ทธ์•„์›ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
	location.href="loginForm.jsp";
</script>
</body>
</html>
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€