note

간단한 표현언어(EL) 예제 본문

JSP/기본

간단한 표현언어(EL) 예제

투한 2012. 2. 14. 09:31





<%@ page contentType="text/html;charset=utf-8"%>
<%
    pageContext.setAttribute("someValue","2011");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>간단한 표현언어(EL)예제</title>
</head>
<body>

<h3>간단한 표현언어(EL)예제</h3>
<br/>
연산자 사용 예 : 

<table border="1" width="80%">
  <tr>
    <td><b>표현식</b></td>
    <td><b>값</b></td>
  </tr>
  <tr>
    <td>\${2 + 5}</td>
    <td>${2 + 5}</td>
  </tr>
  <tr>
    <td>\${"10" + 5}</td>
    <td>${"10" + 5}</td>
  </tr>
  <tr>
    <td>\${"십" + 5}</td>
    <td>에러발생(EL에서의 +는 연산만)</td>
  </tr>
  <tr>
    <td>\${null + 5}</td>
    <td>${null + 5}</td>
  </tr>
  <tr>
    <td>\${4/5}</td>
    <td>${4/5}</td>
  </tr>
  <tr>
    <td>\${5 div 6}</td>
    <td>${5 div 6}</td>
  </tr>
  <tr>   
    <td>\${5 mod 7}</td>
    <td>${5 mod 7}</td>
  </tr>
  <tr>
    <td>\${2 < 3}</td>
    <td>${2 < 3}</td>
  </tr>
  <tr>
    <td>\${2 gt 3}</td>
    <td>${2 gt 3}</td>
  </tr>
  <tr>
    <td>\${3.1 le 3.2}</td>
    <td>${3.1 le 3.2}</td>
  </tr>
  <tr>
    <td>\${(5 > 3) ? 5 : 3}</td>
    <td>${(5 > 3) ? 5 : 3}</td>
  </tr>
  <tr>
    <td>\${someValue == '2011'}</td>
    <td>${someValue == '2011'} (someValue는 2011로 셋팅)</td>
  </tr>
  <tr>
    <td>\${empty someValue2}</td>
    <td>${empty someValue2} (someValue2는 null)</td>
  </tr>       
  <tr>
    <td>\${header["host"]}</td>
    <td>${header["host"]}</td>
  </tr>
  <tr>
    <td>\${header["user-agent"]}</td>
    <td>${header["user-agent"]}</td>
  </tr>
</table>
</body>
</html>












<%@ 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 Test</title>
</head>
<body>
<%
	pageContext.setAttribute("msg", "봄비");
	request.setAttribute("msg2", "여름장마");
	session.setAttribute("msg3", "가을바람");
	application.setAttribute("msg4", "겨울");
	
%>

page영역 : <%=pageContext.getAttribute("msg") %><br/>
page영역(EL) : ${pageScope.msg } <br/>
page영역(EL) : ${msg } <br/><br/>

request영역 : <%=request.getAttribute("msg2") %><br/>
request영역(EL) : ${requestScope.msg2 }<br/>
request영역(EL) : ${msg2 }<br/><br/>

session영역 :<%=session.getAttribute("msg3") %><br/>
session영역(EL) :${sessionScope.msg3 }<br/>
session영역(EL) :${msg3 }<br/><br/>

Application영역 : <%=application.getAttribute("msg4") %><br/>
Application영역(EL) :${applicationScope.msg4 }<br/>
Application영역(EL) :${msg4 }<br/><br/>
</body>
</html>



영역 기재를 하지 않았을 경우 속성명을 다르게 해줘야 바람직하다


 


<%@ 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 Test</title>
</head>
<body>
<%
	pageContext.setAttribute("msg", "봄비");
	request.setAttribute("msg", "여름장마");
	session.setAttribute("msg", "가을바람");
	application.setAttribute("msg", "겨울");
	
%>
page영역(EL) : ${msg } <br/><br/>

request영역(EL) : ${msg }<br/><br/>

session영역(EL) :${msg }<br/><br/>

Application영역(EL) :${msg }<br/><br/>
</body>
</html>













EL은 null에 관대하다
null이라고 출력되야 되는 부분에 빈문자열을 출력한다
알아서 빈문자열로 처리하기 때문에 손이 한번덜간다 


 




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("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>표현언어의 사용예제2</title>
</head>
<body>
<h3>표현언어의 사용예제2 - 파라미터 값 처리</h3>
<br/>
<form action="ELTest03.jsp" method="post">
이름 : <input type="text" name="name" value="${param.name}">
<input type="submit" value="확인" >
</form>
<br/>
이름은 : <%=request.getParameter("name")%> 입니다.<br/>
이름은 : ${param.name} 입니다.
</body>
</html>



순수자바
if(msg != null) && msg.equals(""){

EL
empty msg

'JSP > 기본' 카테고리의 다른 글

MVC패턴 model2  (0) 2012.02.14
properties 국제화(에디터)  (0) 2012.02.14
JSP 파일 업로드 (Servlets.com) cos라이브러리 사용  (0) 2012.02.13
방명록 MODEL1 방식 ORACLE  (0) 2012.02.13
JSP MODEL1 방식  (0) 2012.02.10