λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JSP

[JSP] 슀크립트 μš”μ†Œ

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 9.
λ°˜μ‘ν˜•

β–Ά μ„ μ–Έλ¬Έ(Declaration)

선언문은 JSP νŽ˜μ΄μ§€μ—μ„œ μžλ°” μ½”λ“œμ—μ„œ λ§ν•˜λŠ” 멀버 λ³€μˆ˜μ™€ λ©”μ„œλ“œλ₯Ό μ„ μ–Έν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€. 선언문을 μ‚¬μš©ν•΄ μ„ μ–Έλœ λ³€μˆ˜λŠ” JSP 파일이 μ›Ή μ»¨ν…Œμ΄λ„ˆμ— μ˜ν•΄ 컴파일될 λ•Œ 멀버 λ³€μˆ˜λ‘œ μΈμ‹λ˜κΈ° λ•Œλ¬Έμ— JSP νŽ˜μ΄μ§€μ˜ μ–΄λŠ μœ„μΉ˜μ—μ„œλ„ ν•΄λ‹Ή λ³€μˆ˜λ₯Ό μ°Έμ‘°ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€. λ’€μ—μ„œ μ„€λͺ…ν•  μŠ€ν¬λ¦½ν‹€λ¦Ώ μš”μ†Œμ—μ„œ λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λ©΄ 멀버 λ³€μˆ˜κ°€ μ•„λ‹ˆλΌ 지역 λ³€μˆ˜λ‘œ μΈμ‹λ˜κΈ° λ•Œλ¬Έμ— λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜κΈ° μ΄μ „μ˜ μœ„μΉ˜μ—μ„œλŠ” μ°Έμ‘°κ°€ λΆˆκ°€λŠ₯ν•œ 점이 κ°€μž₯ 큰 차이점이라 ν•  수 μžˆλ‹€.

선언문은 <%!...%> νƒœκ·Έλ₯Ό μ‚¬μš©ν•œλ‹€.

​

​

declarationTest.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>
	<h1><%=getStr()%></h1>
	<%!
		private String getStr() {
			str += "ν…ŒμŠ€νŠΈμž…λ‹ˆλ‹€.";
			return str;
		}

		private String str = "μ„ μ–Έλ¬Έ ";%>
</body>
</html>

μœ„μ—μ„œ μ„ μ–Έλœ String νƒ€μž…μ˜ λ³€μˆ˜ str은 ν•΄λ‹Ή JSP νŽ˜μ΄μ§€μ˜ μ–΄λŠ μœ„μΉ˜μ—μ„œλ„ μ°Έμ‘° κ°€λŠ₯ν•œ 멀버 λ³€μˆ˜κ°€ λœλ‹€.

μ„ μ–Έλ¬Έ νƒœκ·ΈμΈ <%!...%>λŠ” 쀑볡 μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.

​

​

<μ‹€ν–‰ κ²°κ³Ό>

​​

​

​

β–Ά μŠ€ν¬λ¦½ν‹€λ¦Ώ(Scriptlet)

μ•žμ—μ„œ μ„€λͺ…ν–ˆλ“―이 JSPλŠ” μ„œλΈ”λ¦Ώ μ½”λ”©μ˜ 어렀움을 κ·Ήλ³΅ν•˜κΈ° μœ„ν•΄μ„œ 개발된 방식이닀. μ„œλΈ”λ¦ΏμœΌλ‘œ HTML 응닡을 λ§Œλ“€μ–΄ λ‚΄κΈ° μœ„ν•΄μ„œλŠ” 각각의 HTML νƒœκ·Έλ₯Ό PrintWriter λ“±μ˜ 클래슀λ₯Ό μ‚¬μš©ν•΄ 좜λ ₯ 객체λ₯Ό μƒμ„±ν•œ ν›„ 일일이 좜λ ₯ν•΄ μ£Όμ–΄μ•Ό ν•œλ‹€. λ”°λΌμ„œ HTML νƒœκ·Έλ‘œ 이루어진 μ½”λ“œμ˜ 길이가 κΈΈμ–΄μ§ˆμˆ˜λ‘ μ½”λ“œμ˜ μœ μ§€/λ³΄μˆ˜κ°€ μ–΄λ €μ›Œμ§€λ©° 그만큼 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 개발 어렀움이 μ¦κ°€ν•˜κ²Œ λœλ‹€. JSPμ—μ„œλŠ” μ΄λŸ¬ν•œ 어렀움을 μŠ€ν¬λ¦½ν‹€λ¦Ώ(Scriptlet)을 μ‚¬μš©ν•˜μ—¬ κ·Ήλ³΅ν•œλ‹€. HTML μ½”λ“œλ‘œ 된 뢀뢄은 일반 HTML 파일처럼 κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜κ³  μžλ°” μ½”λ“œλ‘œ 이루어진 λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 뢀뢄은 <%...%>둜 ν‘œν˜„λ˜λŠ” μŠ€ν¬λ¦½ν‹€λ¦Ώ νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬λΆ„ν•¨μœΌλ‘œμ¨ out 객체λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³ λ„ μ‰½κ²Œ HTML 응닡을 λ§Œλ“€μ–΄ λ‚Ό 수 μžˆλ‹€.

​

예제 1)

​

 

scriptletTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Calendar" %>
<%
   	Calendar c = Calendar.getInstance();
   	 int hour=c.get(Calendar.HOUR_OF_DAY);
    int minute=c.get(Calendar.MINUTE);
    int second=c.get(Calendar.SECOND);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Scriptlet Test</title>
</head>
<body>
<h1>ν˜„μž¬μ‹œκ°„μ€ <%=hour %>μ‹œ <%=minute %>λΆ„ <%=second %>초 μž…λ‹ˆλ‹€.</h1>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

​​

​

예제 2)

 

​

scriptletTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Calendar" %>
<%
    Calendar c = Calendar.getInstance();
    int hour=c.get(Calendar.HOUR_OF_DAY);
    int minute=c.get(Calendar.MINUTE);
    int second=c.get(Calendar.SECOND);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Scriptlet Test2</title>
</head>
<body>
<h1>ν˜„μž¬ μ‹œκ°„ <%=hour %>μ‹œ <%=minute %>λΆ„ <%=second %>초</h1>
<% if(hour >= 12) { %>
    <h2>μ˜€ν›„μž…λ‹ˆλ‹€.</h2>
<% } else { %>
    <h2>μ˜€μ „μž…λ‹ˆλ‹€.</h2>
<% } %>
<% if(hour >= 12) {
    out.print("<h2>μ˜€ν›„μž…λ‹ˆλ‹€.</h2>");
} else {
	out.print("<h2>μ˜€μ „μž…λ‹ˆλ‹€.</h2>");
}
%>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

​

​​

​

β–Ά ν‘œν˜„μ‹(Expression)

ν‘œν˜„μ‹μ€ μ„ μ–Έλ¬Έ λ˜λŠ” μŠ€ν¬λ¦½ν‹€λ¦Ώ νƒœκ·Έμ—μ„œ μ„ μ–Έλœ λ³€μˆ˜ κ°’μ΄λ‚˜ λ©”μ„œλ“œμ˜ 리턴값을 μŠ€ν¬λ¦½ν‹€λ¦Ώ νƒœκ·Έ μ™ΈλΆ€μ—μ„œ 좜λ ₯ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” 방법이닀.

​

​

expressionTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%!
	public int sum() {
		int total = 0;
		
		for(int i = 1; i <= 100; i++) {
		total += i;
		}
		
		return total;
	}
%>
<% String str = "1λΆ€ν„° 100κΉŒμ§€μ˜ ν•©"; %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Expression Test</title>
</head>
<body>
<h2><%=str %>은 <b><%=sum() %></b>μž…λ‹ˆλ‹€.</h2>
<br>
<h2><%=str %>에 3을 κ³±ν•˜λ©΄ <b><%=sum() * 3 %></b>이 λ©λ‹ˆλ‹€.</h2>
<br>
<h2><%=str %>을 1000으둜 λ‚˜λˆ„λ©΄ <b><%=sum() / 1000. %></b>κ°€ λ©λ‹ˆλ‹€.</h2>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

 

λ°˜μ‘ν˜•

λŒ“κΈ€