본문 바로가기

WEB_Programming/JSTL

8. JSTL Set

1. 몸체없이 처리하는 Set
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>

  <body>
  <h3>Set With No Body</h3>

  <c:set var="str" value="Hello World" />

  str =
  <c:out value="${str}" />

  <br />


  </body>
</html>


2. body 가 를 이용한 Set
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>

  <body>
  <h3>Set With Body</h3>

  <c:set var="str">Hello, Again World</c:set>

  str =
  <c:out value="${str}" />

  <br />


  </body>
</html>


3. 변수를 위한 Set Value
3.1 변수 표현 jsp 01
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<table border="1">
  <tr>
    <TH>Scoped Variable</th>

    <TH>Current Value</th>
  </tr>

  <tr>
    <td>
    <b>Page Scope</b>

    (scopeVarPage)</td>

    <td>&#160;
    <c:out value="${scopeVarPage}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Request Scope</b>

    (scopeVarRequest)</td>

    <td>&#160;
    <c:out value="${scopeVarRequest}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Session Scope</b>

    (scopeVarSession)</td>

    <td>&#160;
    <c:out value="${scopeVarSession}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Application Scope</b>

    (applicationVarPage)</td>

    <td>&#160;
    <c:out value="${scopeVarApplication}" />
    </td>
  </tr>
</table>


3.2 변수 표현 jsp 02
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Scope Example</title>
  </head>

  <body>
    <h3>Linked File: linked.jsp</h3>

    <table border="1">
      <tr>
        <TH>Scoped Variable</th>

        <TH>Current Value</th>
      </tr>

      <tr>
        <td>
        <b>Page Scope</b>

        (scopeVarPage)</td>

        <td>&#160;
        <c:out value="${scopeVarPage}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Request Scope</b>

        (scopeVarRequest)</td>

        <td>&#160;
        <c:out value="${scopeVarRequest}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Session Scope</b>

        (scopeVarSession)</td>

        <td>&#160;
        <c:out value="${scopeVarSession}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Application Scope</b>

        (applicationVarPage)</td>

        <td>&#160;
        <c:out value="${scopeVarApplication}" />
        </td>
      </tr>
    </table>
  </body>
</html>


3.3 변수 설정 JSP
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Catch an Exception</title>
  </head>

  <body>
    <c:catch var="e">
    <c:set var="x" value="10" scope="page" />

    <c:set var="y" value="five" scope="page" />

    x divided by y is
    <c:out value="${x/y}" />

    <br />
    </c:catch>

    <br />
    <c:if test="${e!=null}">The caught exception is:
    <c:out value="${e}" />

    <br />
    </c:if>

    <c:if test="${e==null}">No exception was thrown
    <br />
    </c:if>
  </body>
</html>


4. 콤마로 구분된 변수를 스트링을 위한 변수 세팅
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<html>
  <head>
    <title>Count to 10 Example(tracking even and odd)</title>
  </head>

  <body>
    <c:set var="days" value="A,B,C,D,E,F,G" />

    <table border="0">
      <c:forEach var="i" items="${days}" varStatus="status">
        <jsp:useBean id="status"
        type="javax.servlet.jsp.jstl.core.LoopTagStatus" />

        <c-rt:choose>
          <c-rt:when test="<%=status.getCount()%2==0%>">
            <c:set var="color" value="#cccccc" />
          </c-rt:when>

          <c-rt:otherwise>
            <c:set var="color" value="#dddddd" />
          </c-rt:otherwise>
        </c-rt:choose>

        <tr>
          <td width="200" bgcolor="<c:out value="${color}"/>">
          <c:out value="${i}" />
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>


5. 변수 삭제
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>

  <body>
  <h3>Remove Example</h3>

  <c:set var="test" value="Hello World" scope="page" />

  The value in the variable test before remove is
  <c:out value="${test}" />

  <br />

  <c:remove var="test" scope="page" />

  The value in the variable test after remove is
  <c:out value="${test}" />

  <br />
  </body>
</html>


6. Page 스코프에 set 설정
<%@ taglib prefix="c"    uri="http://java.sun.com/jstl/core" %>

<c:set var="names" value="A B C, D" scope="page" />

<html>
  <head>
    <title>forEach and status</title>
  </head>

  <body>
    <c:forEach items="${pageScope.names}"
               var="currentName"
               varStatus="status"
    >
      Family member #<c:out value="${status.count}" /> is
        <c:out value="${currentName}" /> <br />
    </c:forEach>
  </body>
</html>


7. Request 스코프에 set설정
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set in Scope Examples</title>
  </head>

  <body>
    <c:set var="test" value="Request Level Value" scope="request" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>

        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>

        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Request Level</b>
        </td>

        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Session Level</b>
        </td>

        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Application Level</b>
        </td>

        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>


8. Scope 페이지 변수 설정 2
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set in Scope Examples</title>
  </head>

  <body>
    <c:set var="test" value="Page Level Value" scope="page" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>

        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>

        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Request Level</b>
        </td>

        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Session Level</b>
        </td>

        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Application Level</b>
        </td>

        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>


9. Session 스코프 변수 설정
9.1 변수 설정 jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <body>
    This JSP stores the ultimate answer in a session-scoped variable where
    the other JSPs in the web application can access it.
    <p />
    <c:set var="theUltimateAnswer" value="${41+1}" scope="session"  />

     Click <a href="displayAttributes.jsp">here</a> to view it.
  </body>
</html>


9.2 변수 사용 jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <head>
    <title>Retrieval of attributes</title>
  </head>
  <body>
    The ultimate answer is <c:out value="${sessionScope.theUltimateAnswer}" /> <br/>
  </body>
</html>

10. Application 스코프 변수 설정
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set in Scope Examples</title>
  </head>

  <body>
    <c:set var="test" value="Application Level Value" scope="application" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>

        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>

        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Request Level</b>
        </td>

        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Session Level</b>
        </td>

        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>

      <tr>
        <td>
          <b>Application Level</b>
        </td>

        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>

11. Set 내에서 계산 처리
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<html>
<head>
<title>EL Expression Examples</title>
</head>
<body>
<h1>EL Expression Examples</h1>

<h2>Arithmetic Operators in Expressions</h2>
<c:set var="appleCount" value="${1 + 2 * 4 - 6 / 2}"/>


 <c:out value="${appleCount}" />

</body>
</html>

'WEB_Programming > JSTL' 카테고리의 다른 글

10. JSTL의 Scope  (0) 2008.06.20
9. JSTL에서 JavaBeans 사용  (0) 2008.06.20
7. JSTL Collection 예제  (0) 2008.06.20
6. JSTL foreach 문 처리  (0) 2008.06.20
5. JSTL ForTokens 예제  (0) 2008.06.20