10. EL 과 태그 라이브러리 (JSTL)
10.1 EL(Expression Language)
- JSP 2.0 에 추가됨
- <%=변수 %> 를 ${변수}
- 파라미터값 : param, paramValues
- scope : pageScope , reqeustScope, sessionScope, applicationScope
10.1.1 파라미터 값 전달
- 파라미터값 : param, paramValues 사용
form.jsp
<%@ page contentType = "text/html; charset=utf-8"%>
<html>
<head><title>폼 생성</title></head>
<body>
<h1>파라미터 값 전달</h1>
<hr>
<form action="viewParameter.jsp" method="post">
이름: <input type="text" name="name" size="10"> <br>
주소: <input type="text" name="address" size="30"> <br>
좋아하는 동물:
<input type="checkbox" name="pet" value="강아지">강아지
<input type="checkbox" name="pet" value="고양이">고양이
<input type="checkbox" name="pet" value="돼지">돼지
<br>
<input type="submit" value="전송">
</form>
</body>
</html>
elViewParameter.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.Map" %>
<%
request.setCharacterEncoding("utf-8");
%>
<html>
<head><title>요청 파라미터 출력</title></head>
<body>
<b>param 사용</b><br>
name 파라미터 = ${param.name} <br>
address 파라미터 = ${param.address}
<hr>
<b>paramValues 사용 1</b><br>
${ paramValues.pet[0]} <br>
${ paramValues.pet[1]} <br>
${ paramValues.pet[2]} <br>
<hr>
<b>paramValues 사용 2</b><br>
${ paramValues["pet"][0]} <br>
${ paramValues["pet"][1]} <br>
${ paramValues["pet"][2]} <br>
<%
//String[] values = request.getParameterValues("pet");
//if (values != null) {
//for (int i = 0 ; i < values.length ; i++) {
%>
<%//= values[i] %>
<%
//}
//}
%>
</body>
</html>
10.1.2 page scope
- pageScope
page_scope_set.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page_scope_set.jsp</title>
</head>
<body>
<h1>Page Scope Set</h1>
<%
pageContext.setAttribute("scope", "Request");
%>
Scope: <%=pageContext.getAttribute("scope") %><br>
Scope EL: ${pageScope.scope }<br>
Scope EL: ${scope }<br>
<hr>
<a href="page_scope_use.jsp">Scope Use</a>
</body>
</html>
page_scope_use.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>
<h1>Page Scope Use</h1>
<hr>
Scope EL : ${pageScope.scope } <br>
</body>
</html>
10.1.3 request scope
- requestScope
request_scope_set.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>
<h1>Request Scope Set</h1>
<%
request.setAttribute("scope", "Request");
%>
<jsp:forward page="request_scope_use.jsp"></jsp:forward>
</body>
</html>
request_scope_use.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>
<h1>Request Scope Use</h1>
<hr>
Scope EL : ${requestScope.scope } <br>
</body>
</html>
10.1.4 session scope
- sessionScope 사용
session_scope_set.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session_scope_set.jsp</title>
</head>
<body>
<h1>Session Scope Set</h1>
<%
session.setAttribute("scope", "session");
%>
Scope: <%=session.getAttribute("scope") %><br>
Scope EL: ${sessionScope.scope }<br>
Scope EL: ${scope }<br>
<hr>
<a href="session_scope_use1.jsp">Scope Use</a>
</body>
</html>
session_scope_use1.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>
<h1>Session Scope Use1</h1>
<hr>
Scope : ${sessionScope.scope } <br>
<hr>
<a href="session_scope_use2.jsp">Scope Use</a>
</body>
</html>
session_scope_use2.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>
<h1>Session Scope Use2</h1>
<hr>
Scope : ${sessionScope.scope } <br>
<hr>
<a href="session_scope_use1.jsp">Scope Use</a>
</body>
</html>
10.1.5 application scope
- applicationScope 사용
application_scope_set.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>application_scope_set.jsp</title>
</head>
<body>
<h1>Application Scope Set</h1>
<%
application.setAttribute("scope", "application");
%>
Scope: <%=application.getAttribute("scope") %><br>
Scope: ${applicationScope.scope }<br>
Scope: ${scope }<br>
<hr>
<a href="application_scope_use1.jsp">Scope Use</a>
</body>
</html>
application_scope_use1.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>
<h1>Application Scope Use1</h1>
Scope : ${applicationScope.scope } <br>
<hr>
<a href="application_scope_use2.jsp">Scope Use</a>
</body>
</html>
application_scope_use2.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>
<h1>Application Scope Use2</h1>
Scope : ${applicationScope.scope } <br>
<hr>
<a href="application_scope_use1.jsp">Scope Use</a>
</body>
</html>
10.1.6 동일한 key로 저장시 Scope
- page(reqeust) > session > application
sameNameScope.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page_scope_set.jsp</title>
</head>
<body>
<h1>Page Scope Set</h1>
<%
pageContext.setAttribute("scope", "Page");
session.setAttribute("scope", "Session");
application.setAttribute("scope", "Application");
%>
Scope: <%=pageContext.getAttribute("scope") %> <br>
Scope: <%=session.getAttribute("scope") %> <br>
Scope: <%=application.getAttribute("scope") %> <br>
Scope EL: ${pageScope.scope } <br>
Scope EL: ${sessionScope.scope } <br>
Scope EL: ${applicationScope.scope } <br>
Scope EL: ${scope } <br>
<hr>
</body>
</html>
10.2 JSTL (Java Server Page Standard Tag Library)
- 표준 커스텀 태그
- 변수선언
- 제어문(조건문, 반복문) 처리
- 태그 종류 : Core, Formatting 등
- https://hunit.tistory.com/203
10.2.1 JSTL 설치
- down site : http://tomcat.apache.org/taglibs.html
- jstl 사이트에서 jakarta-taglibs-standard-1.1.2.zip 다운로드
- 압축 해지 후 lib 폴더의 jar 파일 WEB-INF/lib/ 폴더로 복사
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 넣고 사용
- 설치 참고 블로그 : https://best421.tistory.com/28
10.2.2 JSTL 활용
AnimalsInput.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="jstl.test.AnimalsVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// class
String name = "tiger";
AnimalsVO animal = new AnimalsVO(name);
request.setAttribute("animal1", animal);
// Array
String[] animalArray = {"tiger", "lion", "mouse"};
request.setAttribute("animal2", animalArray);
// ArrayList
ArrayList<AnimalsVO> animalArrayList = new ArrayList<AnimalsVO>();
animalArrayList.add(animal);
request.setAttribute("animal3", animalArrayList);
%>
<jsp:forward page="AnimalsView.jsp" />
AnimalsView.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="jstl.test.AnimalsVO"%>
<%@ 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>Insert title here</title>
</head>
<body>
<h1>기본 표현식</h1>
<%
AnimalsVO vo = (AnimalsVO)request.getAttribute("animal1");
%>
<h3>class</h3>
<% if(vo != null){ %>
<ul>
<li><%=vo.getName() %></li>
</ul>
<%} %>
<%
String[] arr = (String[])request.getAttribute("animal2");
%>
<h3>String 배열</h3>
<ul>
<%for(String item: arr){ %>
<li><%=item %></li>
<%} %>
</ul>
<h3>ArrayList 배열</h3>
<%
ArrayList<AnimalsVO> arrList = (ArrayList<AnimalsVO>)request.getAttribute("animal3");
%>
<ul>
<% for(AnimalsVO item: arrList){ %>
<li><%=item.getName() %></li>
<%} %>
</ul>
<hr>
<h1>JSTL + EL 표현식</h1>
<h3>class</h3>
<c:if test="${animal1 != null }">
<ul>
<li>${animal1.name }</li>
</ul>
</c:if>
<h3>String 배열</h3>
<ul>
<c:forEach var="animal" items="${animal2}">
<li>${animal }</li>
</c:forEach>
</ul>
<hr>
<h3>ArrayList 배열</h3>
<ul>
<c:forEach var="animal" items="${animal3}">
<li>${animal.name }</li>
</c:forEach>
</ul>
</body>
</html>