EL(Expression Language) - 표현식 언어
☞ JSP 2.0에서 새로 추가된 스크립트 언어
☞ 기존의 expression tag의 업그레이드 버전
☞ 주요기능
◎ 리터럴 데이터 출력
◎ 각종 연산자를 제공하여 연산결과 출력을 지원
◎ 4개의 속성 scope(page, request, session, application)에 binding된 Attribute(객체)나 그 Attribute의 property 값 출력
◎ JSTL과 연동
☞ 구문
◎ ${출력내용} - ${10}, ${"안녕"}, ${10+20}
◎ ${value1.value2[.value3 ..]}
- 특정 객체의 property 접근 할 때 사용
◎ ${value1[value2]}
- 컬렉션(list 계열)의 값을 출력할 때 주로 사용
- value1에는 EL 내장객체나 attribute 명이 들어와야 한다.
- 구문을 실행 도중 null이 나오면 더이상 진행하지 않고 출력도 하지 않는다.
- 예)
${requestScope.mto.id}
${cto.address.zipcode}
${sessionScope.list[0]}
- JSP의 script(스크립트 릿, 표현식, 선언식, 지시자) 태그에는 사용 못한다.
- action 태그내에는 사용 가능
<jsp:include pages="${dir}/a.jsp"/>
☞ EL의 내장 객체(기본 객체)
◎ EL은 11개의 내장객체를 제공하여 별다른 추가 코드 없이 특정 객체에 접근하여 사용할 수 있도록 한다.
- JSP의 내장객체와는 다르다.
- EL 내장객체 중 pageContext를 제외하고는 모두 name-value 형태의 값을 관리한다.
- ${} 내에서만 사용가능 하며 첫번째 값으로만 올 수 있다.
◎ Scope와 관련된 내장객체
- pageScope : pageScope에 접근
- requestScope : request Scope에 접근
- sessionScope : session Scope에 접근
- applicationScope : application Scope에 접근
◎ 요청파라미터 관련 내장객체
- param : 요청파마리터 조회시 사용(하나의 이름 하나의 값이 넘어온 경우)
- paramValues : 요청 파라미터 조회시 사용(하나의 이름으로 여러값이 넘어온 경우)
◎ 헤더 값 관련
- header
- headerValues
◎ 쿠키 값 조회
- cookie : ${cookie.이름.value}
◎ 초기 파라미터 조회(context-param으로 설정된 것)
- initParam
◎ PageContext 객체 관련 내장객체
- pageContext
el_test1.jsp
☞ folder : WebContent/el
소스보기
<%@ 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>Insert title here</title>
</head>
<body>
<!--
EL의 리터럴(literal) 값
- 문자열 : "," 로 감싼다.
- 숫자
- true / false
- null
-->
${"안녕하세요"}
${10 }<br>
연산식<br>
사칙연산 : ${10+20+30 }, ${"30"+ 20 }, ${null+300 }, ${mto.age +30 }<br>
비교연산 : ${10 == 20 } -> ${10 eq 20 }, ${10 !=20 } -> ${10 ne 20 },
${10 > 20 } -> ${10 gt 20 }, ${10 < 20 } -> ${10 lt 20 },
${10 >= 20 } -> ${ 10 ge 20 }, ${10 <= 20 } -> ${10 le 20 }<br>
논리연산 : ${true && true } -> ${true and true },
${true || true } -> ${true or true },
${!true } -> ${ not true }<br>
empty연산자 : ${empty null }, ${empty "" }, ${empty " " }, ${empty 20 }
<!-- empty 값 - 값이 null, length가 0인 문자열인 경우 true -->
</body>
</html>
el_test2.jsp
☞ folder : WebContent/el
☞ expression language를 통해 CustomerDTO 객체의 getter 메소드로부터 값을 가져오는 것이 아니라 bind된 CustomerDTO객체의 값을 간편하게 조회할 수 있다.
소스보기
<%@page import="myjsp.dto.CustomerDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setAttribute("name", "홍길동");
request.setAttribute("age", 20);
CustomerDTO cto = new CustomerDTO("aaaaa", "1111", "유재석", "abc@acc.com", 30);
session.setAttribute("sess_cto", cto);
%>
<!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>Insert title here</title>
</head>
<body>
이름 : ${name }<br>
나이 : ${age }<br>
이름이 홍길동인가? ${ name=="홍길동" }, ${name eq "홍길동" }<br>
미성년자인가? ${age < 19 }, ${age lt 19 }
<hr>
고객정보<br>
id : ${sess_cto.id }<br>
password : ${sess_cto.password }<br>
name : ${sess_cto.name }<br>
email : ${sess_cto.email }<br>
age : ${sess_cto.age }
</body>
</html>
el_test3.jsp
☞ folder : WebContent/el
소스보기
<%@page import="java.util.HashMap"%>
<%@page import="myjsp.dto.CustomerDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String [] names = {"유재석", "박명수", "정형돈", "하하", "노홍철"};
int [] ages = {10, 20, 12, 25, 17};
request.setAttribute("names", names);
request.setAttribute("ages", ages);
ArrayList list = new ArrayList();
list.add("aaa");
list.add("bbbb");
list.add(2000);
list.add(new CustomerDTO("id", "pwd", "이름", "email@a.com", 20));
request.setAttribute("list", list);
HashMap map = new HashMap();
map.put("name", "tv");
map.put("quntity", 20);
map.put("maker", "LG");
map.put("price", 30000);
request.setAttribute("productMap", map);
%>
<!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>Insert title here</title>
</head>
<body>
배열의 값 접근 : ${ names[0]}, ${names[1] }, ${names[2] }, ${names[3] }, ${names[4] }<br>
숫자 : ${ages[0]}, ${ages[1] }, ${ages[2] }, ${ages[3] }, ${ages[4] }<br>
<hr>
ArrayList 값 접근<br>
0 : ${list[0] }, 1: ${list[1] }, 2: ${list[2] } <br>
3 : ${ list[3] } <br>
${list[3].id }, ${list[3].password }, ${list[3].name }, ${list[3].email }, ${list[3].age }<br>
<hr>
Map 값 접근<br>
상품명 : ${productMap.name }<br>
수량 : ${productMap.quantity }<br>
제조사 : ${productMap["maker"]}<br>
가격 : ${productMap.price }<br>
할인가: ${productMap.price *0.9 } - ${productMap.price*0.1} 할인
</body>
</html>
req.jsp
☞ folder : WebContent/el
소스보기
<%@ 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>Insert title here</title>
</head>
<body>
<form action="${initParam.context_root }el/el_test4.jsp" method="post">
이름 : <input type="text" name="name"><br>
나이 : <input type="text" name="age"><br>
취미 : 영화 <input type="checkbox" name="hobby" value="영화감상">
음악 <input type="checkbox" name="hobby" value="음악감상">
독서 <input type="checkbox" name="hobby" value="독서">
게임 <input type="checkbox" name="hobby" value="게임">
<br>
<input type="submit" value="전송">
</form>
</body>
</html>
el_test4.jsp
☞ folder : WebContent/el
소스보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("utf-8"); %>
<%-- <%@ page isELIgnored="true" %> --%>
<!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>Insert title here</title>
</head>
<body>
이름 : ${param.name }<br>
나이 : ${param.age }<br>
취미 : ${paramValues.hobby[0] }, ${paramValues.hobby[1]}, ${paramValues.hobby[2]}, ${paramValues.hobby[3]}
<hr>
context-param 초기 파라미터 조회 : ${initParam.name }
</body>
</html>
조회결과