๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JSP/Servlet] JSP ํ•„์š”์„ฑ๊ณผ ํŠน์ง•, ๊ตฌ์„ฑ์š”์†Œ (2023.05.15) ๋ณธ๋ฌธ

full stack/Back-End

[JSP/Servlet] JSP ํ•„์š”์„ฑ๊ณผ ํŠน์ง•, ๊ตฌ์„ฑ์š”์†Œ (2023.05.15)

Shinjio 2023. 5. 15. 17:15

๐ŸŽˆ JSP(JavaServer Pages) ๊ฐœ์š”

๐Ÿ“– JSP๋ž€?

 

HTML ์ฝ”๋“œ์— JAVA ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด ๋™์  ์›นํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋„๊ตฌ

Java ์–ธ์–ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” Server Side ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด

 

์–ธ์–ด์˜ ์ข…๋ฅ˜

์ปดํŒŒ์ผ ์–ธ์–ด
- ex. Java
- ๋ฒˆ์—ญ์˜ ๋Š๋‚Œ
- ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ์— ํ•ด์„ํ•ด์„œ ๋‚˜์ค‘์—๋Š” ์‹คํ–‰๋งŒ
- ํ•ด์„์€ ์˜ค๋ž˜๊ฑธ๋ฆฌ์ง€๋งŒ ์‹คํ–‰์€ ๋น ๋ฅด๋‹ค

์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด(Python)ํ†ต์—ญ - ํ•œ ์ค„์”ฉ ํ•ด์„ํ•˜๊ณ  ์‹คํ–‰ํ•˜๋‚˜์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด(JavaScript, JSP)์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด์— ์†ํ•จํ•œ ์ค„์”ฉ ํ•ด์„ํ•˜๊ณ  ์‹คํ–‰๋งŒ๋“ค์–ด์ง„ ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์–ดํ•  ๋•Œ ์‚ฌ์šฉ

 

๐Ÿ“– JSP๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 

โš™ Servlet์˜ ํŠน์ง•

ํ•œ ํด๋ž˜์Šค ๋‚ด์— Business Logic๊ณผ Presentation Logic์ด ๊ฐ™์ด ์ž‘์„ฑ๋จ

(Business Logic : ๋ฐ์ดํ„ฐ ๋ฐ›์•„์„œ ๊ฐ€๊ณต ๋ฐ ์ฒ˜๋ฆฌ ์ˆ˜ํ–‰)

 

 

 

๐Ÿ“– JSP vs Servlet

 

 

 

์›นํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋•Œ์—๋Š” Servlet๊ณผ JSP๋ฅผ ์„ž์–ด์„œ ์‚ฌ์šฉ 

๋กœ์ง ๊ตฌํ˜„ > Servlet

ํ™”๋ฉด ๊ตฌ์„ฑ > JSP 

๋‚˜๋ˆ ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ๋งŽ๋‹ค. 

 

๐Ÿ“– JSP์˜ ํŠน์ง•

- .jsp์˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง

- ๋™์ ์œผ๋กœ ์ž‘๋™ํ•˜์—ฌ ์‘๋‹ต์€ HTML์„ ์ด์šฉํ•จ

- jsp >> ์„œ๋ธ”๋ฆฟ(.java) >> ํด๋ž˜์Šค(.class) >> html๋กœ ๋ณ€ํ™˜๋˜์–ด ์‹คํ–‰๋จ

 

HTML์— ์ž๋ฐ” ์ฝ”๋“œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์ปดํŒŒ์ผ ๋˜์–ด์•ผ ํ•จ
jsp ํŒŒ์ผ ์ž์ฒด๊ฐ€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต๋˜๋Š”๊ฒŒ ์•„๋‹˜. ์ปดํŒŒ์ผ ๋œ ํ›„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต 
jsp ์ปดํŒŒ์ผ๋  ์ˆ˜ ์žˆ๋„๋ก ์ž๋ฐ”๋กœ ๋ณ€ํ™˜์ด ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ์ด ์—ญํ• ์„ WAS ๋‚ด Web Container (ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ) ๊ฐ€ ์•Œ์•„์„œ ์ง„ํ–‰ํ•ด์คŒ
์šฐ๋ฆฌ๋Š” jsp๋ฅผ ์“ฐ์ง€๋งŒ web container ์•ˆ์—์„œ ์ž๋™์œผ๋กœ ์ž๋ฐ” ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ (.java) > ์ปดํŒŒ์ผํ•ด์„œ .class ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ > ์‹คํ–‰ํ•˜๋ฉด ์ •์  ๋ฆฌ์†Œ์Šค์ธ html๋กœ ๋ณ€ํ™˜ > ์ด ์ •์  html์„ ์‘๋‹ตํ•ด์ฃผ๋Š” ๊ตฌ์กฐ

 

 

 

๐ŸŽˆ JSP ์ž‘์„ฑํ•˜๊ธฐ

 

jsp ํŒŒ์ผ์€ webapp ํด๋” ๋‚ด์— ์ž‘์„ฑ

 

 

๐Ÿ“– JSP ๊ตฌ์„ฑ์š”์†Œ

 

 

<%@ : ์ง€์‹œ์ž

JSP์— ๋Œ€ํ•œ ์„ค์ • ์ •๋ณด ๋˜๋Š” JSP ํŽ˜์ด์ง€์— ๋‹ค๋ฅธ ๋ฌธ์„œ๋ฅผ ํฌํ•จ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉ

footer ๋“ฑ ๋งŒ๋“ค์–ด์ง„ ํ•˜๋‚˜์˜ ๋ฌธ์„œ๋ฅผ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์“ฐ๊ณ  ์‹ถ์„ ๋•Œ (๋ฉ”์„œ๋“œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ)

 

<%! : ์„ ์–ธ๋ฌธ

JSP ํŽ˜์ด์ง€ ๋‚ด๋ถ€์— ์ž๋ฐ” ๋ฉ”์„œ๋“œ, ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

๋ณ€์ˆ˜ ์„ ์–ธ, ๋ฉ”์„œ๋“œ ์„ ์–ธ

 

<% : ์Šคํฌ๋ฆฝํŠธ๋ฆฟ

JSP ํŽ˜์ด์ง€ ๋‚ด๋ถ€์— ์ž๋ฐ”(Java) ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

java Class ๋‚ด์— ์ž‘์„ฑ๋˜๋Š” ์ฝ”๋“œ ๊ทธ๋Œ€๋กœ ์ž‘์„ฑ

(๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ)

๋ณ€์ˆ˜ ์‚ฌ์šฉ. ๊ฐ’ ์š”์ฒญ. ์‘๋‹ต ๋“ฑ

์“ฐ๋Š” ์œ„์น˜๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Œ

 

<%= : ํ‘œํ˜„์‹

์ถœ๋ ฅ๊ฒฐ๊ณผ(HTML ์š”์†Œ ๋‚ด)์— ๊ฐ’์„ ๋„ฃ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

HTML ์š”์†Œ ๋‚ด์— ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

ํ‘œํ˜„์‹ ์•ˆ์— ์žˆ๋Š” ๊ฐ’๋“ค์€ ์‚ฌ์šฉ์ž ๋ˆˆ์— ๋ณด์ž„. HTML ๋ฌธ์„œ ๋‚ด์—์„œ ์ถœ๋ ฅ์ด ๋œ๋‹ค๋Š” ๋ง

 

 

โš™ JSP ํƒœ๊ทธ ์‚ฌ์šฉ ์‹ค์Šต

<!-- ์ง€์‹œ์ž(Directive) : JSP ์„ค์ • ์ •๋ณด ๋ฐ ๋‹ค๋ฅธ ๋ฌธ์„œ์—(๋ฅผ) ํฌํ•จ ๋“ฑ -->
<!-- ์ง€์‹œ์ž์˜ ์ข…๋ฅ˜ : page, include, taglib(tag library >> ex. JSTL) -->
<!-- contentType : ์‘๋‹ต๋ฐ์ดํ„ฐ์˜ ํ˜•์‹๊ณผ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ๋ธŒ๋ผ์šฐ์ €(ํด๋ผ์ด์–ธํŠธ)์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•จ -->
<!-- pageEncoding : JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ธ์ฝ”๋”ฉ ์ง€์ • -->
<%@page import="java.util.Random"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- charset : html ๋ฌธ์„œ์— ๋Œ€ํ•œ ์ธ์ฝ”๋”ฉ ์ง€์ • -->
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- ์Šคํฌ๋ฆฝํ‹€๋ฆฟ(Scriptlet : JSP ํŽ˜์ด์ง€ ๋‚ด๋ถ€์— ์ž๋ฐ” ์†Œ์Šค ์ฝ”๋“œ ์‚ฝ์ž…) -->
<!-- ์Šคํฌ๋ฆฝํ‹€๋ฆฟ ๋‚ด์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ service() ๋ฉ”์„œ๋“œ ์•ˆ์— ์ž‘์„ฑ๋จ -->
<% 
	// JSP ๊ตฌ์„ฑ ์š”์†Œ ๋‚ด์—์„œ๋Š” ์ž๋ฐ” ์ฃผ์„ ์ž‘์„ฑ
	// ์Šคํฌ๋ฆฝํ‹€๋ฆฟ ์•ˆ์—์„œ๋Š” ๋ฌด์กฐ๊ฑด ; ์ž‘์„ฑํ•ด์•ผ ํ•จ
	System.out.print("Hello"); //์ดํด๋ฆฝ์Šค ์ฝ˜์†”์ฐฝ์— ์ถœ๋ ฅ
	int num = 10;
	int num2 = 20;
	
	Random rd = new Random();
	int ran = rd.nextInt(10);
	// out ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•œ ์  ์—†์ง€๋งŒ jsp์ฝ”๋“œ๊ฐ€ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋˜๋ฉด์„œ WebContainer๊ฐ€ ์ž๋™์œผ๋กœ out ๊ฐ์ฒด(๋‚ด์žฅ๊ฐ์ฒด)๋ฅผ ์ƒ์„ฑํ•จ
	out.print(ran + "<br>");
	// out.println - ์ฝ”๋“œ๋งŒ ๊ฐœํ–‰(ํ™”๋ฉด์ƒ ๊ฐœํ–‰X) >> ํ™”๋ฉด์ƒ ๊ฐœํ–‰ํ•˜๋ ค๋ฉด <br>
%>
<!--  ํ‘œํ˜„์‹(Expression : ํ™”๋ฉด(์›น๋ธŒ๋ผ์šฐ์ €์ƒ)์— ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ, ; ์ž‘์„ฑ X -->
<%=ran %><br>
<%="String" %><br>
<%="ํ’€"+"์Šคํƒ" %><br>
<!-- ์„ ์–ธ๋ฌธ(Declaration) : ์ž๋ฐ” ๋ฉ”์„œ๋“œ, ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ -->
<!-- ์„ ์–ธ๋ฌธ ์•ˆ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ์˜ ์œ„์น˜๋Š” ํด๋ž˜์Šค ์ด๋ฆ„ ๋ฐ”๋กœ ๋ฐ‘, ์ „์—ญ -->
<%! int num3 = 100; %>
<%! 
	public void print(){
		System.out.println("print!");
	}	
%>
</body>
</html>

 

์‹ค์Šต 2. 

 

<%@ 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>
	<table border=1>
		<tr>
			<%
			for(int i = 1; i <= 10; i++){
				out.print("<td>" + i + "</td>");
			}
			%>
		</tr>
	</table>
	
	<table border=1>
		<tr>
			<%for(int i = 1; i <= 10; i++){%>
				<td><%=i %></td>
			<%}%>
		</tr>
	</table>
	
</body>
</html>

 

์‹ค์Šต 3.

<HTML ์ฝ”๋“œ>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="Ex02Output.jsp" method="post">
		<input type="text" name="data">
		<input type="submit">
	</form>
</body>
</html>

 

<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>
	<%
	request.setCharacterEncoding("UTF-8");
	String data = request.getParameter("data");
	out.print(data);
	%>
	<%=data %>
</body>
</html>

 

์‹ค์Šต 4.

 

<html ์ฝ”๋“œ>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="Ex03Output.jsp" method="post">
		<input type="number" name="num1">
		<br>
		<input type="number" name="num2">
		<br>
		<select name="ope">
			<option>+</option>
			<option>-</option>
			<option>*</option>
			<option>/</option>
		</select>
		<input type="submit" value="๊ณ„์‚ฐ">
	</form>
</body>
</html>

 

<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>
	<%
		request.setCharacterEncoding("UTF-8");
		int num1 = Integer.parseInt(request.getParameter("num1"));
		int num2 = Integer.parseInt(request.getParameter("num2"));
		String ope = request.getParameter("ope");
		
		int result;
		if(ope.equals("+")){
			result = num1 + num2;
		}else if(ope.equals("-")){
			result = num1 - num2;
		}else if(ope.equals("*")){
			result = num1 * num2;
		}else{
			result = num1 / num2;
		}
		//์ถœ๋ ฅ๋ฐฉ๋ฒ• 1)
		out.print(num1 + ope + num2 + "=" + result);
	%>
	<!-- ์ถœ๋ ฅ๋ฐฉ๋ฒ• 2) -->
	<%= num1 %>
	<%= ope %>
	<%= num2 %>
	=
	<%= result %>
	
	<!-- ์ถœ๋ ฅ๋ฐฉ๋ฒ• 3) -->
	<%= num1+ope+num2+"="+result %>
	
</body>
</html>

 

JSP๋„ url mapping ๊ฐ€๋Šฅ