10. EL 과 태그 라이브러리 (JSTL)

10.1 EL(Expression Language)

10.1.1 파라미터 값 전달

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

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

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

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

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

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)

10.2.1 JSTL 설치

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>