* EL(Expression Language)
- EL의 일종의 스크립트 언어로 자료 타입, 수치 연산자, 논리연산자, 비교 연산자 등을 제공하며 표현식을 대체할 수 있다.
-EL사용법
ex) 표현식 <%=value %> //EL $(value)
-EL 내부에 사용하는 연산자
1. 산술 : +, -, *, /, %
2. 관계 : ==, !=, <, <=, >, >=
3. 조건 : a ? b : c (a조건식이 참이면 b를 실행, 거짓이면 c를 실행)
4. 논리 : &&, ||
* 액션태그와 EL
-액션태그 <jsp:getProperty name="member" property="name"/>
-EL -> ${member.name}
-EL 내장객체
1.pageScope: JSP의 page 객체를 참조하는 객체
2. requestScope : JSP의 request 객체를 참조하는 객체
3. sessionScope : JSP의 session 객체를 참조하는 객체
4. applicationScope : JSP의 applecation 객체를 참조하는 객체
5. param : 요청 파라미터를 참조하는 객체
6. paramValues: : 요청 파라미터(배열)를 참조하는 객체
7. initParam : 서블릿 컨텍스트 초기화 파라미터를 참조하는 객체
8. cookie : 쿠키 객체를 참조하는 객체.
실습.
1.EL 내부에 사용하는 연산자
el_basic.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>
${10} <br/>
${5.55} <br/>
${"abc"} <br/>
${true}
<hr/>
${ 1+2 } <br/>
${ 1-2 } <br/>
${ 1*2 } <br/>
${ 1/2 } <br/>
${ 1>2 } <br/>
${ 1<2 } <br/>
${ 1==2 } <br/>
${ (1<2) || (1>2) } <br/>
${(1==2) ? "1은 2와 같음" : "1과 2는 다름" }
${ "\"안녕하세요\"" }
<hr/>
</body>
</html>
2. 액션태그 EL을 사용해보자.
el_obj.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>
<form action="el_obj_ok.jsp">
이름: <input type="text" name="name" size="10"/><br/>
별명: <input type="text" name="nick" size="10"/><br/>
<input type="submit" value="확인" />
</form>
</body>
</html>
el_obj_ok.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>
<%-- 이 코드를 el로 작성해보자.
<%
String name = request.getParameter("name");
String nick = request.getParameter("nick");
%>
이름: <%=name %> <br/>
별명: <%=nick %>
--%>
이름: ${param.name} <br/>
별명: ${param.nick} <!-- request.getParameter()할필요없이 el로 쉽게 값을 가져올수있다. -->
</body>
</html>
![]() |
![]() |
3. 섭씨와 화씨를 출력하는 로직을 작성해보자.
Thermometer.java
package kr.co.koo.el;
import java.util.*;
public class Thermometer {
private Map<String, Double> degrees = new HashMap<>(); //map컬렉션 사용
public void setDegrees(String city, double celsius) {
degrees.put(city, celsius);
}
public double getDegrees(String city) {
return degrees.get(city);
}
//섭씨를 화씨로 변환해주는 메서드.
public double change(String city) {
double celsius = getDegrees(city);
return celsius * 1.8 + 32;
}
}
el_thermometer.jsp
<%@page import="kr.co.koo.el.Thermometer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
/*
- EL태그로 자바의 객체를 활용하려면 request, session, application
객체에 자바의 객체를 저장시켜야 합니다.
*/
request.setAttribute("t", new Thermometer());
//객체를 t로 저장을해놓으면 브라우저가 유지되는 동안에 객체를 사용페이지에 계속 만들어줄필요없이
//t라는 이름을 가지고 편하게 사용할 수 있다.
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- el에서 객체의 메서드 호출 시 매개값으로 문자열을 전달할 때는 홑따옴표 사용 --%>
${t.setDegrees('서울', 22.5)}
서울 온도: 섭씨 ${t.getDegrees('서울')}도 // 화씨 ${t.change('서울')}도
<br/>
${t.setDegrees('알래스카', 19.8)}
알래스카 온도: 섭씨 ${t.getDegrees('알래스카')}도 // 화씨 ${t.change('알래스카')}도
</body>
</html>
'IT' 카테고리의 다른 글
17.2 - JSP MVC Model 2 Architecture - FrontController란? (0) | 2022.11.02 |
---|---|
17.JSP - JSTL(JSP Standard Tag Library) (0) | 2022.11.01 |
9. JSP 자바빈 (JAVA Bean) (0) | 2022.10.22 |
12-1. jsp - JDBC프로그래밍 PreparedStatement (0) | 2022.10.12 |
11-1. JSP - JDBC 프로그래밍 select, delete (1) | 2022.10.11 |