DEV&OPS/Java

EL (Expression Language)

ALEPH.GEM 2022. 7. 1. 15:35

 

EL 구문

${   }  내에 표현식으로 작성합니다.

JSP 2.0 부터 사용가능합니다.

boolean변수, 숫자, 문자열, null 데이터 타입을 사용할 수 있습니다.

JSP태그로 작업하는 기능은 EL구문으로 표현할 수 있습니다.

 

ex18.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
	문자: ${"Literals"}
	<br>
	bool: ${5>3}
	<br>
	객체: ${header["host"]}
</body>
</html>

 

EL 연산자

  • 산술 연산자: + , - , * , / , %, mod
  • 논리 연산자: &&, ||, !, and, or, not
  • 비교 연산자: ==, !=, <, >, <=, >=, eq, ne, lt, get, le, ge
  • empty 연산자: 값이 null 이나 공백 문자인지 편단하는 연산자   ${empty "" } 혹은 ${empty null}

 

EL 내장객체

jsp 내장 객체 처럼 선언하지 않고 기본적으로 사용할 수 있습니다.

  • pageContext : jsp내장객체와 같습니다.
  • pageScope : pageContext 데이터를 저장하고 있는 map 객체
  • requestScope : HttpServletRequest 데이터를 저장하고 있는 map 객체
  • sessionScope : HttpSession 데이터를 저장하고 있는 map 객체
  • applicationScope : ServletContext 데이터를 저장하고 있는 map객체
  • param : Query String으로 전달 된 데이터를 저장하고 있는 map객체
  • paramValues : 같은 변수 이름으로 전달된 질의 문자열의 데이터를 저장하고 있는 map객체
  • header : 요청 헤더 정보에 있는 데이터들을 저장하고 있는 map객체
  • headerValues : 요청 헤더 정보에 있는 데이터들을 저장하고 있는 map객체
  • cookie : 쿠키 데이터가 저장된 map객체
  • initParam : 웹 어플리케이션 초기 파라미터 데이터를 저장하고 있는 map 객체 

내장 객체 중에서 param과 header를 살펴보겠습니다.

ex19.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL</title>
</head>
<body>
	${param.id } / ${param.pwd } 
	<br>
	${param["id"]} / ${param["pwd"] }
</body>
</html>

 

ex20.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL</title>
</head>
<body>
	<jsp:forward page="${param.p}" />
</body>
</html>

 

 

ex21.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL</title>
</head>
<body>
<%
	Enumeration<String> list = request.getHeaderNames();
	while(list.hasMoreElements()){
		String key = list.nextElement();
		out.print("<br>"+key+":"+request.getHeader(key));
	}
%>
<hr>
${header}
</body>
</html>

 

 

 

request 객체 정보 추출 예제

이전에 만들어 두었던 src/ 경로에 net.aacii.beans 패키지 아래에 BookBean.java 파일을 생성합니다.

package net.aacii.beans;

public class BookBean {
	private String title;
	private String author;
	private String publisher;
	
	public BookBean() {
		
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	
}

책에 관한 정보를 입력하는 페이지 bookInput.jsp 파일을 WebContent 경로에 생성합니다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book Input</title>
</head>
<body>
<form action="ex22.jsp" method="post">
제목: <input type="text" name="title"><br>
저자: <input type="text" name="author"><br>
출판사: <input type="text" name="publisher"><br>
<input type="submit" value="등록" >
</form>
</body>
</html>

입력받은 request 객체 데이터를 다음 페이지(bookOutput.jsp)로 전달하는 페이지 ex22.jsp를 작성합니다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex22.jsp</title>
</head>
<body>
	<!--  bean 객체 생성 -->
	<jsp:useBean id="book" class="net.aacii.beans.BookBean"/>
	<!--  bean 객체의 setter메소드를 호출해서 멤버변수 이름과 동일한 request parameter 값을 객체에 세팅합니다. -->
	<jsp:setProperty property="*" name="book"/>
	<!-- 다음 페이지로 이동할 때 방금 세팅한 빈객체를 전달하기 위해 request 객체에 bean 객체를 포함시킵니다. -->
	<%
		request.setAttribute("book", book);
	%>
	<!-- 다음 페이지로 이동 -->
	<jsp:forward page="bookOutput.jsp" />
</body>
</html>

bookOutput.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book Output</title>
</head>
<body>
title : ${book.title}<br>
author : ${book.author}<br>
publisher : ${book.publisher}
</body>
</html>

${book} 은 request, session, application 순서대로 getAttribute("book")을 실행합니다.

request에 먼저 book객체를 발견하면 session이나 application에서 더이상 getAttribute("book")을 실행하지 않습니다.

request에 없으면 session 을 찾아보고 session에도 없으면 application에서 book객체를 찾아보게 됩니다.

어느 한 단계에서 book객체가 발견되면 더 이상 getAttribute("book")을 실행하지 않으므로 중복을 피하는 것입니다.

${book.title}은 ((BookBean)request.getAttribute("book")).getTitle(); 을 실행한 것과 같은 동작입니다.

 

만약 전달하는 객체를 request 객체 말고 session 객체로 하고 싶으면 ex22.jsp에서 

session.setAttribute("book",book);

으로 등록하면 되고 application 객체인 경우는

application.setAttribute("book",boo);

이렇게 등록해서 전달하면 됩니다.

 

 

728x90