JiYoung Dev πŸ–₯

[JSP/Servlet] μ„ μ–Έλ¬Έ, μ§€μ‹œμž, λ‚΄μž₯객체 (2023.05.16) λ³Έλ¬Έ

full stack/Back-End

[JSP/Servlet] μ„ μ–Έλ¬Έ, μ§€μ‹œμž, λ‚΄μž₯객체 (2023.05.16)

Shinjio 2023. 5. 16. 20:01

🎈 μ„ μ–Έλ¬Έ(Declaration)

 

 

μ„ μ–Έλ¬Έ λ‚΄ μž‘μ„±ν•œ λ³€μˆ˜

- μœ„μΉ˜λŠ” 클래슀 λ°”λ‘œ 밑에 (μ „μ—­λ³€μˆ˜)

 

슀크립트릿 λ‚΄ μž‘μ„±ν•œ λ³€μˆ˜

- service λ©”μ„œλ“œ μ•ˆμœΌλ‘œ 듀어감 (μ§€μ—­λ³€μˆ˜)

 

🎈 μ§€μ‹œμž(Directive)

 

 

πŸ“– Page μ§€μ‹œμž

 

 

 

λΆˆκ°€λŠ₯ν•œκ±Έ μ»΄ν“¨ν„°μ—κ²Œ μ‹œν‚€λ©΄? ERROR λ°œμƒ

 

 

였λ₯˜ λ°œμƒμ‹œ νŠΉμ • νŽ˜μ΄μ§€ μ—°κ²°λ˜λ„λ‘!

 

ν˜„μž¬νŽ˜μ΄μ§€

errorPage="였λ₯˜λ°œμƒμ‹œ 보여쀄 νŽ˜μ΄μ§€"

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="Ex04ErrorPage.jsp"%>
    <!-- errorPage : ν˜„μž¬ νŽ˜μ΄μ§€μ—μ„œ 였λ₯˜κ°€ λ°œμƒν–ˆμ„ λ•Œ λŒ€μ‹  보여쀄 νŽ˜μ΄μ§€ 지정 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=2/0 %>
</body>
</html>

 

였λ₯˜ λ°œμƒμ‹œ 보여쀄 νŽ˜μ΄μ§€

isErrorPage="true"

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!-- 이 νŽ˜μ΄μ§€κ°€ 였λ₯˜κ°€ λ°œμƒν–ˆμ„ λ•Œ 보여쀄 νŽ˜μ΄μ§€μž„μ„ 지정! -->
<!-- >> isErrorPage : false(κΈ°λ³Έκ°’) -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align="center">
	<img src="./img/error.png" width="300px" height="300px"><br><br>
	μ„œλ²„ 였λ₯˜λ₯Ό μ κ²€μ€‘μž…λ‹ˆλ‹€
</body>
</html>

 

πŸ“– Include μ§€μ‹œμž

 

<%@ include file="μ—°κ²°ν•  νŽ˜μ΄μ§€ 파일" %>

 

 

<%@ 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>
	<%@ include file="Ex05header.jsp" %> <br>
	λ©”μΈνŽ˜μ΄μ§€<br>
	<a href="Ex05sub.jsp">μ„œλΈŒ νŽ˜μ΄μ§€λ‘œ 이동</a><br>
	<%@ include file="Ex05footer.jsp" %>
</body>
</html>

 

πŸ“– Taglib μ§€μ‹œμž

 

 

라이브러리 λ‹€μš΄λ‘œλ“œ

 

 

λ‹€μ΄λ‚˜λ―Ή μ›Ήν”„λ‘œμ νŠΈμ—μ„œ 라이브러리 μ‚¬μš©ν•  λ•Œ

WEB-INF > lib 폴더 내에 μ‚½μž…

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%for(int i=1; i<=10; i++){ %>
		<%=i %>
	<%} %>
	<br>
	<C:forEach var="i" begin="1" end="10" step="1">
		<!-- el 문법 : νŠΉμ • μ˜μ—­μ— μ €μž₯된 값을 μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•΄μ£ΌλŠ” 문법 -->
		<C:out value="${i}"/>
	</C:forEach>
</body>
</html>

 

πŸ“– 주석

 

 

html 주석은 μ‚¬μš©μžμ—κ²Œλ„ λ³΄μ—¬μ§ˆ 수 μžˆλ‹€ >> λ³΄μ•ˆμƒ μ€‘μš”ν•œ 것은 μ“°λ©΄ μ•ˆλ¨!

 

μžλ°” 파일둜 λ³€ν™˜λμ„ λ•ŒκΉŒμ§€ 확인 κ°€λŠ₯

 

jsp 주석은 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>
	<!-- HTML 주석 : μ΅œμ’… 응닡 λ¬Έμ„œμ—μ„œ 확인 κ°€λŠ₯ -->
	<% //μžλ°” 주석 : μžλ°” 파일둜 λ³€ν™˜λμ„ λ•ŒκΉŒμ§€ 확인 κ°€λŠ₯ %>
	<%-- JSP 주석 : JSP νŒŒμΌμ—μ„œλ§Œ 확인 κ°€λŠ₯ --%>
</body>
</html>

 

🎈 λ‚΄μž₯객체(Implicit Object)

JSP νŽ˜μ΄μ§€ λ‚΄λΆ€μ—μ„œ out, request λ“±μ˜ κ°μ²΄λŠ” λ”°λ‘œ μƒμ„±ν•˜μ§€ μ•Šλ”λΌλ„ λ°”λ‘œ μ‚¬μš© κ°€λŠ₯ν•œ μ΄μœ λŠ”?

λ‚΄μž₯객체 λ•Œλ¬Έ! -->  JSP νŽ˜μ΄μ§€ μ•ˆμ— μ„ μ–Έν•˜μ§€ μ•Šκ³ λ„ μ‚¬μš©ν•  수 μžˆλŠ” 객체

 

 

 

out.print둜 μ‚¬μš©

 

 

 

μ‹€μŠ΅1.

HTML μ½”λ“œ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fieldset>
		<legend>ν•™μ ν™•μΈν”„λ‘œκ·Έλž¨</legend>
		<form action="Ex01Request.jsp">
			<table>
				<tr>
					<td>이름</td>
					<td><input type="text" name="name"></td>
				</tr>
				<tr>
					<td>Java점수</td>
					<td><input type="text" name="java"></td>
				</tr>
				<tr>
					<td>Web점수</td>
					<td><input type="text" name="web"></td>
				</tr>
				<tr>
					<td>IoT점수</td>
					<td><input type="text" name="iot"></td>
				</tr>
				<tr>
					<td>Android점수</td>
					<td><input type="text" name="and"></td>
				</tr>
				<tr>
					<td><input type="submit" value="학점확인"></td>
				</tr>
			</table>
		</form>
	</fieldset>
</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>
	<fieldset>
		<legend>ν•™μ ν™•μΈν”„λ‘œκ·Έλž¨</legend>
		<form action="Ex01Request.jsp">
		<%
		
		int java = Integer.parseInt(request.getParameter("java"));
		int web = Integer.parseInt(request.getParameter("web"));
		int iot = Integer.parseInt(request.getParameter("iot"));
		int and = Integer.parseInt(request.getParameter("and"));
		
		%>
			<table align="center">
				<tr>
					<td>이름</td>
					<td><% out.print(request.getParameter("name")); %></td>
				</tr>
				<tr>
					<td>Java점수</td>
					<td><%= java %></td>
				</tr>
				<tr>
					<td>Web점수</td>
					<td><%= web %></td>
				</tr>
				<tr>
					<td>IoT점수</td>
					<td><%= iot %></td>
				</tr>
				<tr>
					<td>Android점수</td>
					<td><%= and %></td>
				</tr>
				<tr>
					<td>평균</td>
					<% double avg = (java + web + and + iot)/4.0;
						String score = "";
						
						if(avg >= 95){
							score = "A+";
						}else if(avg >= 90){
							score = "A";
						}else if(avg >= 85){
							score = "B+";
						}else if(avg >= 80){
							score = "B";
						}else {
							score = "F";
						}
						
						%>
					<td><%= avg %></td>
				</tr>
				<tr>
					<td>학점</td>
					<td><strong><%= score %></strong></td>
				</tr>
			</table>
		</form>
	</fieldset>
</body>
</html>

 

μ‹€μŠ΅ 2. 

html μ½”λ“œ

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align="center">
	<h1>랜덀 당첨 κ²Œμž„</h1>
	<fieldset>
		<legend>λžœλ€λ½‘κΈ°</legend>
		<form action="Ex02createInput.jsp">
			<input type="number" name="num">
			<br><br>
			<input type="submit" value="μƒμ„±ν•˜κΈ°">
		</form>
	</fieldset>
</body>
</html>

 

jsp 1 μ½”λ“œ (μž…λ ₯)

<%@ 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 align="center">
	<h1>랜덀 당첨 κ²Œμž„</h1>
		<fieldset>
			<legend>λžœλ€λ½‘κΈ°</legend>
			<form action="Ex02randomOutput.jsp">
				주제 : 
				<input type="text" name="subject">
				<br>
				
				<%
				
				for(int i = 1 ; i <= Integer.parseInt(request.getParameter("num")); i++){
					out.print("μ•„μ΄ν…œ : "); 
					out.print("<input type='text' name='item'>");
					out.print("<br>");
				}
				%>

				<input type="submit" value="λžœλ€λ½‘κΈ°">
				
			</form>
		</fieldset>

</body>
</html>

 

jspμ½”λ“œ(좜λ ₯)

 

<%@page import="java.util.Random"%>
<%@ 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 align="center">
	<h1>랜덀 당첨 κ²Œμž„</h1>
		<fieldset>
			<legend>λžœλ€λ½‘κΈ°κ²°κ³Ό</legend>
			<form action="Ex02createInput.jsp">
				<% 
				String[] items = request.getParameterValues("item");
				
				Random rd = new Random();
				int ran = rd.nextInt(items.length);
				
				String print = items[ran];
				
				%>
				<%= request.getParameter("subject") %>
				<br>
				<%=  print%>
			</form>
		</fieldset>
</body>
</html>