๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JSP - EL ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•๊ณผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

by Leica 2020. 2. 13.
๋ฐ˜์‘ํ˜•

JSP - EL ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•๊ณผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

 

EL(Expression Language)์€ ์ž๋ฐ” ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ, ๊ฐ’์„ JSP์˜ ํ‘œํ˜„์‹ <%= %>์ด๋‚˜ ์•ก์…˜ ํƒœ๊ทธ <jsp:useBean>๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ ๋ณด๋‹ค ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊บผ๋‚ผ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค.

๋˜ํ•œ static ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ JSP์—์„œ๋Š” ์ฃผ๋กœ ์„œ๋ธ”๋ฆฟ ๋ณด๊ด€์†Œ(JspContext, ServletRequest, HttpSession, ServletContext)์—์„œ ๊ฐ’์„ ๊บผ๋‚ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

EL ํ‘œ๊ธฐ๋ฒ•

${ }

- JSP๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์ฆ‰์‹œ ๋ฐ˜์˜๋œ๋‹ค. (Immediate evaluation)
- ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๊บผ๋‚ผ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ
#{ }

- ์‹œ์Šคํ…œ์—์„œ ํ•„์š”ํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋  ๋•Œ ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค. (Deferred evaluation)
- ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ๋‹ด๋Š” ์šฉ๋„๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ

${member.no} ๋˜๋Š” ${member["no"])

 

์œ„ EL ํ‘œํ˜„์‹์˜ ์ž๋ฐ” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1
2
Member obj = (Member) pageContext.findAttribute("member");
int value = obj.getNo();
cs

 

PageContext์˜ findAttribute()๋Š” ์„œ๋ธ”๋ฆฟ ๋ณด๊ด€์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋‘ ๋’ค์ ธ์„œ ๊ฐ์ฒด๋ฅผ ์ฐพ๋Š”๋‹ค.

๋งˆ์ง€๋ง‰ ๋ณด๊ด€์†Œ์—๋„ ์—†๋‹ค๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

JspContext → ServletRequest → HttpSession → ServletContext

 

EL์—์„œ ๋ณด๊ด€์†Œ ๊ฒ€์ƒ‰ ๋ฒ”์œ„ ์ง€์ •

${member.no} ์ฒ˜๋Ÿผ ๋ณด๊ด€์†Œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ๋ณด๊ด€์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์ง€๋งŒ ํŠน์ • ๋ณด๊ด€์†Œ์—์„œ๋งŒ ์ฐพ๋„๋ก ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ์€ EL์—์„œ ๋ณด๊ด€์†Œ๋ฅผ ์ฐธ์กฐํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฆ„์ด๋‹ค.

 

์ด๋ฆ„ ๋ณด๊ด€์†Œ
pageScope JspContext
requestScope ServletRequest
sessionScope HttpSession
applicationScope ServletContext

 

๋”ฐ๋ผ์„œ ${requestScope.member.no}์™€ ๊ฐ™์ด ์“ฐ๋ฉด ServletRequest์—์„œ๋งŒ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐพ๋Š”๋‹ค.

์ฐธ๊ณ ๋กœ ์ด๋Š” ๋‹ค์Œ ์ž๋ฐ” ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

 

1
2
Member obj = (Member) request.getAttribute("member");
int value = obj.getNo();
cs

 

EL ๊ธฐ๋ณธ ๊ฐ์ฒด

๋‹ค์Œ์€ JSP์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” EL ๊ฐ์ฒด์ด๋‹ค.

 

๊ฐ์ฒด ์„ค๋ช… ์ฝ”๋“œ
pageContext JSP์˜ PageContext ๊ฐ์ฒด  
servletContext ServletContext ๊ฐ์ฒด ${pageContext.servletContext.๊ฐ์ฒด๋ช…}
session HttpSession ๊ฐ์ฒด ${pageContext.session.๊ฐ์ฒด๋ช…}
request ServletRequest ๊ฐ์ฒด ${pageContext.request.๊ฐ์ฒด๋ช…}
response ServletResponse ๊ฐ์ฒด  
param ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’ ์กฐํšŒ ${param.๋งค๊ฐœ๋ณ€์ˆ˜๋ช…}
paramValues ์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’ ๋ฐฐ์—ด ์กฐํšŒ ${paramValues.๋งค๊ฐœ๋ณ€์ˆ˜๋ช…}
header HTTP ํ—ค๋”์˜ ๊ฐ’ ์กฐํšŒ ${header.ํ—ค๋”๋ช…}
headerValues HTTP ํ—ค๋”์˜ ๊ฐ’ ๋ฐฐ์—ด ์กฐํšŒ ${headerValues.ํ—ค๋”๋ช…}
cookie ์ฟ ํ‚ค ๊ฐ’ ์กฐํšŒ ${cookie.์ฟ ํ‚ค๋ช…}
initParam ์ปจํ…์ŠคํŠธ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’ ์กฐํšŒ ${initParam.๋งค๊ฐœ๋ณ€์ˆ˜๋ช…}
pageScope page ๋ณด๊ด€์†Œ์˜ ๊ฐ’ ์กฐํšŒ ${pageScope.๊ฐ์ฒด๋ช…}
requestScope request ๋ณด๊ด€์†Œ์˜ ๊ฐ’ ์กฐํšŒ ${requestScope.๊ฐ์ฒด๋ช…}
sessionScope session ๋ณด๊ด€์†Œ์˜ ๊ฐ’ ์กฐํšŒ ${sessionScope.๊ฐ์ฒด๋ช…}
applicationScope application ๋ณด๊ด€์†Œ์˜ ๊ฐ’ ์กฐํšŒ ${applicationScope.๊ฐ์ฒด๋ช…}

 

 

EL ๋ฆฌํ„ฐ๋Ÿด ํ‘œํ˜„์‹

๋ฌธ์ž์—ด, ์ •์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , Boolean, Null์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1
2
3
4
5
6
๋ฌธ์ž์—ด : ${"test"}
๋ฌธ์ž์—ด : ${'test'}
์ •์ˆ˜ : ${20}
๋ถ€๋™์†Œ์ˆ˜์  : ${3.14}
Boolean : ${true}
null : ${null}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ

${null}์€ ๋นˆ ๋ฌธ์ž์—ด(" ")๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

EL ํ‘œํ˜„์‹์œผ๋กœ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

๋ฐฐ์—ด์—์„œ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

1
2
3
4
<%
    pageContext.setAttribute("scores"new int[]{908070100});
%>
${scores[2]}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : 70

 

List ๊ฐ์ฒด์—์„œ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

1
2
3
4
5
6
7
8
9
<%
    List<String> list = new LinkedList<>();
    list.add("๊ฐ€๋‚˜");
    list.add("๋‹ค๋ผ");
    list.add("๋งˆ๋ฐ”");
 
    pageContext.setAttribute("list", list);
%>
${list[1]}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : ๋‹ค๋ผ

 

Map ๊ฐ์ฒด์—์„œ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

1
2
3
4
5
6
7
8
<%
    Map<StringString> map = new HashMap<>();
    map.put("1""a");
    map.put("2""b");
    map.put("3""c");
    pageContext.setAttribute("map", map);
%>
${map["1"]}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : a

 

์ž๋ฐ” ๊ฐ์ฒด์—์„œ ํ”„๋กœํผํ‹ฐ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

1
2
3
4
5
6
7
8
<%
    pageContext.setAttribute("student",
            new Student()
                    .setStudentNo("202012121")
                    .setStudentName("ํ™๊ธธ๋™")
    );
%>
${student.studentNo} - ${student.studentName}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : 202012121 - ํ™๊ธธ๋™

 

ResourceBundle ๊ฐ์ฒด์—์„œ ๊ฐ’ ๊บผ๋‚ด๊ธฐ

src/MyResourceBundle.java

1
2
3
4
5
6
7
8
9
10
11
public class MyResourceBundle extends ListResourceBundle {
    @Override
    protected Object[][] getContents() {
        return new Object[][] {
                {"OK""ํ™•์ธ"},
                {"Cancle""์ทจ์†Œ"},
                {"Reset""์žฌ์„ค์ •"},
                {"Submit""์ œ์ถœ"}
        };
    }
}
cs

 

JSP

1
2
3
4
<%
    pageContext.setAttribute("myRB", ResourceBundle.getBundle("MyResourceBundle"));
%>
${myRB.OK}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : ํ™•์ธ

 

EL ํ‘œํ˜„์‹์˜ ์—ฐ์‚ฐ์ž

EL์—์„œ ์ง€์›ํ•˜๋Š” ์ฃผ์š” ์—ฐ์‚ฐ์ž๋Š” ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž, ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž, empty ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค.

 

EL ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

ํ‘œํ˜„์‹ ์‹คํ–‰ ๊ฒฐ๊ณผ
${10+20} 30
${10-20} -10
${10*20} 200
${10/20} 0.5
${10 div 20} 0.5
${10 % 20} 10
${10 mod 20} 10

 

EL ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

ํ‘œํ˜„์‹ ์‹คํ–‰ ๊ฒฐ๊ณผ
${true && false} false
${true and false] false
${false || true} true
${false or true] true
${not true} false
${!true} false

 

EL ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž

ํ‘œํ˜„์‹ ์—ฐ์‚ฐ์ž
${10 == 11} false
${10 eq 11} false
${10 != 11} true
${10 ne 11} true
${10 < 11} true
${10 lt 11} true
${10 > 11} false
${10 gt 11} false
${10 <= 11} true
${10 le 11} true
${10 >= 11} false
${10 ge 11} false

 

 

empty ์—ฐ์‚ฐ์ž

- ๊ฐ’์ด null์ด๋ฉด true
- ๋ฌธ์ž์—ด, ๋ฐฐ์—ด, Map, Collection ๊ฐ์ฒด์˜ ํฌ๊ธฐ๊ฐ€ 0์ด๋ฉด true
- ๊ทธ ์™ธ์—๋Š” false

 

1
2
3
4
<%
    pageContext.setAttribute("title""EL ์—ฐ์‚ฐ์ž");
%>
${empty title}, ${empty title2}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : false, true

 

์กฐ๊ฑด ์—ฐ์‚ฐ์ž

EL ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋Š” ์ž๋ฐ” 3ํ•ญ ์—ฐ์‚ฐ์ž์™€ ๋ฌธ๋ฒ•์ด ๋™์ผํ•˜๋‹ค.

1
${10 > 20 ? "ํฌ๋‹ค" : "์ž‘๋‹ค"}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ : ์ž‘๋‹ค

 

EL ์˜ˆ์•ฝ ํ‚ค์›Œ๋“œ

and, or, not, eq, ne, lt, gt, le, ge, true, false, null, instanceof, empty, div, mod

 

์„œ๋ธ”๋ฆฟ ๋ณด๊ด€์†Œ์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•  ๋•Œ Key๊ฐ€ EL ์˜ˆ์•ฝ ํ‚ค์›Œ๋“œ์ด๋ฉด EL ํ‘œํ˜„์‹์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

1
2
3
4
<%
    pageContext.setAttribute("ne""EL ์˜ˆ์•ฝ ํ‚ค์›Œ๋“œ๋ฅผ Key๋กœ ์‚ฌ์šฉ");
%>
${ne}
cs

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

EL ํ™œ์šฉ

๋‹ค์Œ์€ ํšŒ์› ๊ฐ€์ž… ์˜ˆ์ œ์˜ ํ•™์ƒ ์ •๋ณด ๋ณ€๊ฒฝ ํŽ˜์ด์ง€์ด๋‹ค.

 

StudentUpdateForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page import="com.atoz_develop.spms.vo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Student loginMember = (Student) session.getAttribute("student");
    Student student = (Student) request.getAttribute("student");
%>
<html>
<head>
    <title>ํ•™์ƒ ์ •๋ณด</title>
</head>
<body>
<h1>ํ•™์ƒ์ •๋ณด</h1>
<form action='update' method='post'>
    ํ•™๋ฒˆ: <input type='text' name='student_no' value='<%=student.getStudentNo()%>' readonly><br>
    ํ•™๊ณผ: <input type='text' name='department' value='<%=student.getDepartment()%>'><br>
    ์ด๋ฆ„: <input type='text' name='student_name' value='<%=student.getStudentName()%>'><br>
    ํ•™๋…„: <input type='text' name='grade' value='<%=student.getGrade()%>' readonly><br>
    ์„ฑ๋ณ„: <input type='text' name='gender' value='<%=student.getGender()%>' readonly><br>
    ๋‚˜์ด: <input type='text' name='age' value='<%=student.getAge()%>' readonly><br>
    ์ „ํ™”๋ฒˆํ˜ธ: <input type='text' name='phone_number' value='<%=student.getPhoneNumber()%>'><br>
    ์ฃผ์†Œ: <input type='text' name='address' value='<%=student.getAddress()%>'><br>
    <input type='submit' value='์ˆ˜์ •'>
    <%
        if(loginMember != null && "admin".equals(loginMember.getStudentNo())) {
    %>
    <input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=<%=student.getStudentNo()%>'" />
    <%
        }
    %>
    <input type='button' value='์ทจ์†Œ' onClick='location.href="list"'>
</form>
</body>
</html>
cs

 

<% %>์— session์—์„œ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  <%= %>๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , EL ํ‘œํ˜„์‹์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

StudentUpdateForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ํ•™์ƒ ์ •๋ณด</title>
</head>
<body>
<h1>ํ•™์ƒ์ •๋ณด</h1>
<form action='update' method='post'>
    ํ•™๋ฒˆ: <input type='text' name='student_no' value='${requestScope.student.studentNo}' readonly><br>
    ํ•™๊ณผ: <input type='text' name='department' value='${requestScope.student.department}'><br>
    ์ด๋ฆ„: <input type='text' name='student_name' value='${requestScope.student.studentName}'><br>
    ํ•™๋…„: <input type='text' name='grade' value='${requestScope.student.grade}' readonly><br>
    ์„ฑ๋ณ„: <input type='text' name='gender' value='${requestScope.student.gender}' readonly><br>
    ๋‚˜์ด: <input type='text' name='age' value='${requestScope.student.age}' readonly><br>
    ์ „ํ™”๋ฒˆํ˜ธ: <input type='text' name='phone_number' value='${requestScope.student.phoneNumber}'><br>
    ์ฃผ์†Œ: <input type='text' name='address' value='${requestScope.student.address}'><br>
    <input type='submit' value='์ˆ˜์ •'>
    <input type="button" value="์‚ญ์ œ" onClick="location.href='delete?student_no=${student.studentNo}'"
           style="display: ${'admin' eq sessionScope.student.studentNo ? inline-block : none}">
    <input type='button' value='์ทจ์†Œ' onClick='location.href="list"'>
</form>
</body>
</html>
cs

 

์ด์™€ ๊ฐ™์ด EL์„ ์‚ฌ์šฉํ•˜๋ฉด <% %>์— ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ <jsp:useBean>์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  <%= %>๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ EL๋งŒ ์‚ฌ์šฉํ•ด๋„ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง€์ง€๋งŒ ๋‚˜์•„๊ฐ€ JSP์— ์ž๋ฐ”๋กœ ์ž‘์„ฑ๋œ ๋ฐ˜๋ณต๋ฌธ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์—†์• ๋ ค๋ฉด JSTL์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ธํŒ…

- [JSP] JSTL ์‚ฌ์šฉ ๋ฐฉ๋ฒ• - ์ฃผ์š” ํƒœ๊ทธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

- [JSP] JSTL ํ™œ์šฉ ๋ฐฉ๋ฒ• - JSP์—์„œ ์ž๋ฐ” ์ฝ”๋“œ ์ œ๊ฑฐ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€