본문 바로가기

JSP/기본다지기

JSP 8일차 필기 (EL 태그)

EL태그란?


EL(Expression Language)


스크립트릿<%= %>

또는 out.print()와 같은 자바코드를

사용하지 않고, 간편하게 파라미터를

출력하기 위한 태그이다.


배열 또는 컬렉션, JavaBean의 Property

등에서 사용된다.


index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EL 태그를 알아보자</title>
</head>
<body>
    <h1>EL 태그</h1>
    <h1><a href="./L01ELDataType.jsp">el 태그의 데이터 타입</a></h1>
    <h1><a href="./L02ELLogin.jsp">el의 parameter</a></h1>
    <h1><a href="./L03ELHeader.jsp">el의 header</a></h1>
    <h1><a href="./L04ELCookie.jsp">el의 cookie</a></h1>
    <h1><a href="./L05ELScope.jsp">el의 scope</a></h1>
</body>
</html>
cs


먼저 EL태그의 데이터 타입에 대해서..


L01ELDataType.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el tag</title>
</head>
<body>
    <h1>EL 태그의 데이터 타입</h1>
    <h3>
    &lt;%="Hello"%> : <%="Hello"%><!-- out.print() -->
    &nbsp;////&nbsp;
    \${"Hello"} : ${"Hello"}
    <!-- \$ el tag로 인식하지 않는다. -->
    <!-- &lt; %script로 인식하지 않는다. -->
    </h3>
    <!-- el tag는 출력 or 연산에 사용 -->
    <h3>정수 : ${8}</h3>
    <h3>실수 : ${5.5}</h3>
    <h3>문자열 : ${"jspLesson"}</h3>
    <h3>논리 : ${true}</h3>
    <h3>정수 : ${8}</h3>
    <h3>null : ${null}</h3>
    
    <h3>\${5+10} : ${5+10}</h3>
    <h3>\${'5'+10} : ${'5'+10}</h3>
    <h3>\${"5"+10} : ${"5"+10}</h3>
    <h3>el은 script요소가 강하다.</h3>
    <%-- 스크립트 주석 ${int i=0--%>
    
    <!-- script는 자바를 모르는 사람을 위한 언어 -> 컴파일이 되지 않는다.-->
    <!-- 스크립트 언어 번역기가 컴파일 대신 사용된다. 문자열 -> 코드인식 -->
    <!-- 컴파일이 되지 않기 때문에 데이터 타입(기본데이터 타입)이 중요하지 않음 -->
    <!-- 데이터타입을 몰라도 코드를 작성할 수 있다. -->
    <!-- 자바 개발자에게는 불확실성이 더욱 어렵게 느껴진다. -->
    <!-- el tag는 html 주석을 사용할 수 있지만 스크립트 주석을 사용하는 것을 권장 -->
    <%-- !스크립트 주석은 html 주석과 같이 사용 가능 --%> -->
    
    <h3>\${10%3} : ${10%3}</h3>
    <h3>\${10 mod 3} : ${10 mod 3}</h3><!-- 나머지 -->
    <h3>\${5>2} : ${5>2}</h3>
    <h3>\${5 gt 2} : ${5 gt 2}</h3>
    <h3>\${5<2} : ${5<2}</h3>
    <h3>\${5 lt 2} : ${5 lt 2}</h3>
    <h3>\${(5>2)?"참":"거짓"} : ${(5>2)?"참":"거짓"}</h3>
    <!-- % < > 태그에서 사용하는 특수문자이기 때문에 mod, gt, lt 를 대신 사용 -->
    
    <!-- jstl core 태그를 이용하면 el의 변수를 선언할 수 있다. -->
    <%String input = "사용할 수 없음.";%>
    ${empty input}
    </body>
</html>
cs


EL태그는 스크립트 언어이기 때문에 컴파일이 되지 않는다.


이러한 이유로 데이터타입이 중요하지 않으며, 데이터 타입을 모르더라도 코드를 작성할 수가 있다.



EL태그로 파라미터를 받아, 출력해보자.


L02ELLogin.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EL로 로그인하기</title>
</head>
<body>
    <h1>로그인 form</h1>
    <form action="./L02ELParam.jsp" method="post">
        <p>
            <label>아이디 : </label>
            <input type="text" name="id" value="jsp">
        </p>
        <p>
            <label>비밀번호 : </label>
            <input type="password" name="pwd" value="admin1234">
        </p>
        <p>
            <label>취미 : </label>
            <input type="checkbox" name="hobby" value="그림" checked>그림
            <input type="checkbox" name="hobby" value="요리" checked>요리
            <input type="checkbox" name="hobby" value="게임">게임
            <input type="checkbox" name="hobby" value="만화">만화
            <input type="checkbox" name="hobby" value="바둑">바둑
        </p>
        <button type="submit">제출</button>
    </form>
</body>
</html>
cs


L02ELParam.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 language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el param으로 파라미터 받기</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");%>
    <h1>당신이 입력한 정보입니다.</h1>
    <h3>id : <%=request.getParameter("id")%></h3>
    <h3>pwd :<%=request.getParameter("pwd")%></h3>
    <h3>취미 : 
    <%
    String[] hobby =request.getParameterValues("hobby");
    for(String hob : hobby){
    %>
        <%=hob%>
    <%
    }
    %></h3>
    <hr>
    <h1>당신이 입력한 정보 (el param 이용)</h1>
    <h3>param : ${param}</h3>
    <h3>id : ${param.id}</h3>
    <h3>pwd : ${param.pwd}</h3>
    <h3>취미(param) : ${param.hobby}</h3>
    <h3>취미(paramValues) : ${paramValues.hobby[0]}, ${paramValues.hobby[1]}, ${paramValues.hobby[2]}</h3>
    <!-- jstl을 사용해야지 for로 출력 가능 -->
    <!-- spring, struts는 el태그를 많이 사용한다. -->
</body>
</html>
cs


11번~21번행 까지는 request 객체를 이용하여 파라미터를 출력하는 부분.


24번행 부터는 EL태그를 이용하여 파라미터를 출력하는 부분이다.


취미는 체크박스로 복수의 파라미터를 넘겨주기 때문에(배열) 출력을 하기 위해서는 paramValues로 출력해야 한다.

반복문을 사용하기 위해서는 jstl을 사용해야 하는데, 다음에 jstl 부분에서 다룬다.



EL태그로 host 헤더의 정보를 받아오기.

L03ELHeader.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el header</title>
</head>
<body>
    <h1>el태그로 host 헤더 정보 읽기</h1>
    <%=request.getHeader("host")%>
    <h3>접속 ip: ${header.host}</h3>
    <h3>접속 경로: ${header["referer"]}</h3>
    <!-- index.jsp에서 해당 경로로 들어가기 -->
    <h3>유저 시스템 정보: ${header["user-agent"]}</h3>
</body>
</html>
cs



EL태그로 쿠키를 출력하기.

L04ELCookie.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
34
35
36
37
38
39
40
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el로 쿠키 출력하기</title>
</head>
<body>
<%//id=jspLesson, pwd=admin1234 // 수명 1*24*60*60 쿠키 만들고 출력
    Cookie c1 = new Cookie("id","jspLesson");
    Cookie c2 = new Cookie("pwd","admin1234");
    c1.setMaxAge(1*24*60*60);
    c2.setMaxAge(1*24*60*60);
    response.addCookie(c1);
    response.addCookie(c2);
%>
<h3>쿠키불러오기 (el cookie 이용)</h3>
<h3>
<table>
    <tr>
        <td>cookie</td>
        <td>${cookie}</td>
    </tr>
    <tr>
        <td>${cookie.id.name}</td>
        <td>${cookie.id.value}</td>
    </tr>
    <tr>
        <td>${cookie.pwd.name}</td>
        <td>${cookie.pwd.value}</td>
    </tr>
    <tr>
        <td>${cookie.JSESSIONID.name}</td>
        <td>${cookie.JSESSIONID.value}</td>
    </tr>
</table>
</h3>
</body>
</html>
cs


EL태그를 이용하면 이처럼 쿠키를 쉽게 불러올 수 있다.

L05ELScope.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>attribute의 값을 el 태그로 출력해 보자</title>
</head>
<!-- attribute는 페이지의 내장 객체이며 Object 타입으로 저장됨.(보통 header)쿠키는 제외
     parameter는 외부로 전달하는 파라미터이미 String 타입으로 전달.(url로 전달)-->
<%
pageContext.setAttribute("name""page Attr");
request.setAttribute("name""request Attr");
session.setAttribute("name""session Attr");
application.setAttribute("name""app Attr");
%>
<body>
<h1>jsp 내장 객체 불러오기</h1>
<h3>
<table style = text-align:center;>
    <tr>
        <td>속성</td>
        <td></td>
    </tr>
    <tr>
        <td>\${name}</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>page 속성\${pageScope.name}</td>
        <td>${pageScope.name}</td>
    </tr>
    <tr>
        <td>request 속성\${requestScope.name}</td>
        <td>${requestScope.name}</td>
    </tr>
    <tr>
        <td>session 속성\${sessionScope.name}</td>
        <td>${sessionScope.name}</td>
    </tr>
    <tr>
        <td>application 속성\${applicationScope.name}</td>
        <td>${applicationScope.name}</td>
    </tr>
</table>
</h3>
    <hr>
    <h1>Scope 내장 객체란</h1>
    <h3>request: 클라이언트의 요청에 대해 같은 요청을 공유하는 
        페이지에 HttpServletRequest 객체에 저장
        request.getParameter() -> URL 파라미터 접근(get)
        request.getAttribute() -> 내장객체에 접근(post)
    </h3>
    <h3>Session: 하나의 웹 브라우저 당 1개의 session 객체가 생성된다.
        같은 웹 브라우저 내에서는 요청되는 페이지들은 같은 객체를 공유한다.
        생성된 객체는 HttpSession에 저장된다.
    </h3>
    <h3>page: 한 번의 웹브라우저(클라이언트)의 요청에 대해 하나의 JSP 페이지가 호출되며
        웹브라우저의 요청이 들어오면 이 때 단 한개의 페이지만 대응된다.
        생성된 객체는 PageContext에 저장된다.
    </h3>
    <h3>application: 하나의 웹 어플리케이션 당 1개의 application 객체가 생성된다.
        같은 웹 어플리케이션에 요청되는 페이지들은 같은 객체를 공유한다.
        생성된 객체는 실질적으로는 ServletContext에 저장된다.
    </h3>
</body>
</html>
cs