EL(Expression Language) 연산자
- 표현언어 의미
- JSP의 스크립트 태그 <%= %>를 대신하여 편리하게 출력하기위해 제공
→ 객체 프로퍼티 값을 꺼낼때 주로 사용 - 예시
<%= hello %> == ${hello}
${test } == ${['test']} == ${["test"]}
${hello.test } == ${hello.['test' ]} == ${hello.["test" ]} - EL내장 객체
내장 객체 | 의미 |
pageScope | Page 영역에 존재하는 객체를 참조할때 사용 |
requestScope | Request영역에 존재하는 객체를 참조할때 사용 |
sessionScope | Session영역에 존재하는 객체를 참조할때 사용 |
applicationScope | Application영역에 존재하는 객체를 참조할 때 사용 |
param | 파라미터 값을 얻어 올 때 사용 |
paramValues | 파라미터값을 배열로 얻어올 때 사용 |
Header | Header 정보를 얻어 올 때 사용 |
HeaderValues | Header 정보를 배열로 얻어 올 때 사용 |
cookie | 쿠키 객체를 참조할 때 사용 |
initParam | 컨텍스트의 초기화 객체 사용 |
pageContext | pageContext 객체를 참조할 때 사용 |
<예시_1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>eltest_1.jsp / EL연산자 사용</title>
</head>
<body>
<h3>\${5+7}=${5+7}</h3>
<h3>\${5-7}=${5-7}</h3>
<h3>\${5*7}=${5*7}</h3>
<h3>\${5/7}=${5/7}</h3>
<h3>\${5%7}=${5%7}</h3>
<h3>\${55==77}=${55==77}</h3>
<h3>\${5!=77}=${5!=77}</h3>
<h3>\${5>77}=${5>77}</h3>
<h3>\${5>=77}=${5>=77}</h3>
<h3>\${5<77}= ${5<77}</h3>
<h3>\${5<=77}=${5<=77}</h3>
<h3>\${ ( 5 + 3 ) != 8 ? 8 : 99}=${( 5 + 3 ) != 8 ? 8 : 99}</h3>
</body>
</html>
↓결과
<예시_2>
eltest_2.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% session.setAttribute("king", "Session Test 2022"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>eltest_2.jsp / el 내장객체 사용</title>
<style type="text/css">
table{
width:500px;
margin:auto;
border:2px solid black;
text-align: center;
bacground-color:pink;
}
</style>
</head>
<body>
<form action="eltest_3.jsp" method="get">
<table>
<tr>
<td>이름</td>
<td><input type="text" name="name" value="lee"></td>
</tr>
<tr>
<td>전화번호</td>
<td><input type="text" name="tel" value="010-0000-000"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="입력"> &nasp;&nasp;
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</body>
</html>
eltest_3.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%
request.setCharacterEncoding("utf-8");
%>
<%
/* String name2 = request.getParameter("name");
String tel2 = request.getParameter("tel"); */
%>
<title>eltest_3.jsp / el 내장 객체 사용 예</title>
</head>
<body>
<hr size="5" color="red">
<h2>${sessionScope.king}</h2>
<h2>${param.name}</h2>
<h2>${param.tel}</h2>
<hr size="5" color="blue">
<h2>${sessionScope['king']}</h2>
<h2>${param["name"]}</h2>
<h2>${param.tel}</h2>
<h2>${param.kbs}</h2>
<hr size="5" color="green">
<%-- <h3><%=name2 %></h3>
<h3><%=tel2 %></h3> --%>
</body>
</html>
↓결과
<예시_3>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>eltest_4.jsp / el에서 액션 태그 사용</title>
<h2>el에서 액션 태그 사용</h2>
<jsp:useBean id="color" class="java.util.ArrayList">
<%
color.add("red");
color.add("green");
color.add("blue");
color.add("yellow");
color.add("gray");
color.add("#cccc66");
color.add("#bb88bb");
%>
</jsp:useBean>
<ul>
<font size="5" color="${color[0]}"><li> 이 색상은 ${color[0]}색입니다</li></font>
<font size="5" color="${color[1]}"><li> 이 색상은 ${color[1]}색입니다</li></font>
<font size="5" color="${color[2]}"><li> 이 색상은 ${color[2]}색입니다</li></font>
<font size="5" color="${color[3]}"><li> 이 색상은 ${color[3]}색입니다</li></font>
<font size="5" color="${color[4]}"><li> 이 색상은 ${color[4]}색입니다</li></font>
<font size="5" color="${color[5]}"><li> 이 색상은 ${color[5]}색입니다</li></font>
<font size="5" color="${color[6]}"><li> 이 색상은 ${color[6]}색입니다</li></font>
<font size="5" color="${color[7]}"><li> 이 색상은 ${color[7]}색입니다</li></font>
</ul>
</head>
<body>
</body>
</html>
↓결과
<예시_4>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>elempty.jsp / el표현언어 연산자 empty test</title>
</head>
<body>
\${ null } =${ null}<p>
\${ n } =${ n }<p>
\${empty null } = ${empty null }<p>
\${empty n } = ${empty n }<p>
\${param.user} = ${param.user }<p>
\${empty param.user} = ${empty param.user }<p>
</body>
</html>
↓결과
<예시_5_1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>elempty2.jsp / el내장 객체test</title>
</head>
<body>
<%
request.setAttribute("lee", "hello world");
application.setAttribute("kim", "world hello");
%>
<hr size="5" color="red">
<table border="1">
<tr>
<td>EL Implicit Object</td>
<td>Result</td>
<tr>
<td>\${empty param.age }</td>
<td>${empty param.age }</td>
</tr>
<tr>
<td>\${!empty param.age }</td>
<td>${!empty param.age }</td>
</tr>
<tr>
<td>\${pageContext.request.contextPath }</td>
<td>${pageContext.request.contextPath }</td>
</tr>
<tr>
<td>\${requestScope.lee }</td>
<td>${requestScope.lee }</td>
</tr>
<tr>
<td>\${requestScope['lee'] }</td>
<td>${requestScope['lee'] }</td>
</tr>
<tr>
<td>\${requestScope["lee"] }</td>
<td>${requestScope["lee"] }</td>
</tr>
<tr>
<td>\${applicationScope.kim }</td>
<td>${applicationScope.kim }</td>
</tr>
</table>
<hr size="5" color="green">
</body>
</html>
저장객체 접근
- ${parma.name} → request.getParameter("name");
${member} → request.getParameter("member");
→ 저장객체의 attribute에 자동으로 접근하는데
자도검색 순위는 page, request, session, application 순 - ${member.name} → Member m = (Member)request.getAttribute("member");
m.getName();
↓결과
<예시_5_2>
javaBean사용)
univ폴더에 UserEl.java 파일 생성)
package univ;
public class UserEl {
private String uname;
private String uid;
private String unum;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUnum() {
return unum;
}
public void setUnum(String unum) {
this.unum = unum;
}
}
eljavabean_1.jsp파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>eljavabean_1.jsp 파일 생성 / el에서 액션 태그 사용</title>
</head>
<body>
<h2>el에서 자바빈즈 getter호출</h2>
<jsp:useBean id="user" class="univ.UserEl">
<jsp:setProperty name="user" property="uname" value="lee" />
<jsp:setProperty name="user" property="uid" value="lee123" />
<jsp:setProperty name="user" property="unum" value="78" />
</jsp:useBean>
<hr size="5" color="red">
\${user.uname } = ${user.uname }<br>
\${user.uid } = ${user.uid }<br>
\${user.unum } = ${user.unum }<br>
<p>
\${user["uname"] } = ${user["uname"] }<br>
\${user["uid"] } = ${user["uid"] }<br>
\${user["unum"] } = ${user["unum"] }<br>
<hr size="5" color="blue">
<p>
이름 : <%=user.getUname() %><br>
id : <%=user.getUid() %><br>
번호 : <%=user.getUnum() %>
<hr size="5" color="green">
</body>
</html>
↓결과
JSTL(JSP Standard Tag Library)
- 커스텀 태그 : 개발자가 직접 정의할 수 있는 태그
- JSTL 자카르타에서 자주 사용되는 기능들을 모아서 제공하는 커스텀 태그 라이브러리
- 기능
1. core : JSTL core는 JSTL에서 기본적인 기능(컨트롤에 관련된 기능)을 구혀한 라이브러리
2. fmt : 국제화 / 형식화 기능 제공
3. xml : xml문서에서 자주 사용되는 기능을 태그 라이브러리로 모아놓은것
4. sql : sql관련 기능을 제공해 주는 라이브러리
- JSTL core
- 문자열출력, 반복문, 조건문과 같은 내용 저장
- 자바코드를 사용하지 않고도 쉽게 기능 구현 가능
형식 ) <%@ taglib prefix= "c" uri="http://java.sun.jsp/jstl/core" %>
prefix의 c는 태그를 사용할때 항상 붙는 접두어
<c : out >, <c : set>과 같이 사용
출력태그 | <c:out> |
변수 설정 및 삭제 태그 | <c:set>, <c:remove> |
예외 처리 태그 | <c:catch> |
조건 처리 태그 | <c:if>, <c:choose>, <c:when>, <c:otherwise> |
반복 처리 태그 | <c:forEach>, <c:forToken> |
페이지 처리 태그 | <c:import>, <c:redirect>, <c:url>, <c:param> |
<예시_1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jstl_core_1.jsp / jstl core 라이브러리</title>
</head>
<body>
<c:set var="test" value="JSTL Test 2022" />
<h3>
<c:set> 사용후 :
<c:out value="${test }" />
</h3>
<c:remove var="test" />
<h3>
<c:remove> 사용후 :
<c:out value="${test }" />
</h3>
<c:catch var="error">
<%=33 / 0%>
</c:catch>
<h3>
<c:catch> 오류 :
<c:out value="${error }" />
</h3>
<c:if test="${ 5 < 10 }">
<h3>5는 10보다 작다</h3>
</c:if>
<c:if test="${3+6==9 }">
<h3>3+6=9</h3>
</c:if>
<c:choose>
<c:when test="${5+20 ==25 }">
<h3>5+20=25</h3>
</c:when>
<c:otherwise>
<h3>5+20=25 아니다</h3>
</c:otherwise>
</c:choose>
</body>
</html>
↓결과
<예시_2>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jstl_core_2.jsp / jstl core 라이브러리 test</title>
</head>
<body>
<c:forEach var="test" begin="1" end="35" step="3">
<b>${test }</b>
</c:forEach>
<br>
<c:forTokens var="alphabet" items="a,b,c,d;e;f;g" delims=", ;">
<b>${alphabet }</b>
</c:forTokens>
<br>
<c:set var="data" value="1,2,3,4" />
<c:forTokens items="${data }" delims="," var="varData">
<b>${varData}</b>
</c:forTokens>
<br>
</body>
</html>
↓결과
<예시_3>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jstl_core_3.jsp / jstl core tag : set target test</title>
</head>
<body>
<h2>jstl core tag : set target test</h2>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="book" value="<%=new java.util.HashMap()%>" />
<c:set target="${book }" property="java" value="Java Java " />
<c:set target="${book }" property="javascript"
value="Javascript Javasciprt" />
<c:set target="${book }" property="jsp" value="JSP JSP" />
\${book.java } =${book.java }
<p>\${book.javascript } =${book.javascript }
<p>\${book.jsp } =${book.jsp }
<p>
<c:set target="${pageScope }" property="name" value="lee" />
\${pageScope.name } =${pageScope.name }
<p>
<jsp:useBean id="today" class="java.util.Date" />
<c:set target="${today }" property="year" value="2022" />
\${today.year } = ${today.year }
<p>\${today.month } = ${today.month+1 }
<p>\${today.date } = ${today.date }
<p>
</body>
</html>
↓결과
'JSP' 카테고리의 다른 글
221019_JSP_JDBC_국비_썸네일/ trasaction/ JQUERY (0) | 2022.10.19 |
---|---|
221018_JSP_JSTL_국비_if/ choose/ import/ redirec/ xml/ sql (0) | 2022.10.18 |
221014_JSP_DBCP/ 게시판만들기 (0) | 2022.10.14 |
221013_JSP_JDBC_국비_Connection Pool (0) | 2022.10.13 |
221011_JSP_JDBC_국비_기초 (0) | 2022.10.11 |