준비하기

http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/ 에서 1.1.2.zip 을 다운 받고, 압축을 풀어 lib 폴더의 jstl.jar 과 standard.jar 파일을 복사한다. 복사한 파일을 apache 를 설치한 폴더 안의 lib 폴더에 붙여넣는다.

JSTL 라이브러리

  • Core: <c:tag
  • XML Processing: <x:tag
  • I18N formatting: <fmt:tag
  • SQL: <sql:tag
  • Functions: <fn:function()

Core 라이브러리

출력, 제어문, 반복문 등의 기능 포함

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

  • 출력: <c:out value="${varName}"/>
  • 변수 설정: <c:set var="varName" value="varValue"/>
  • 변수 제거: <c:remove var="varName"/>

  • 예외 처리:
      <c:catch var="error">
      <%=2/0%>
      </c:catch>
      <br />
      <c:out value="${error}"/>
    
  • 제어문(if):
      <c:if test="${1+2==3}">
          1 + 2 = 3
      </c:if>
    	
      <c:if test="${1+2!=3}">
          1 + 2 != 3
      </c:if>
    
  • 제어문(switch):
      <c:set var="i" value="3"/>
      <c:choose>
          <c:when test="${i==1}"> i is 1	</c:when>
          <c:when test="${i==2}"> i is 2	</c:when>
          <c:when test="${i==3}"> i is 3	</c:when>
          <c:otherwise> i is unknown </c:otherwise>
      </c:choose>
    
  • 반복문(for):
      <c:forEach var="fEach" begin="0" end="30" step="3">
          ${fEach}
      </c:forEach>
    
  • 페이지 이동: <c:redirect url="url">

  • 파라미터 전달: <c:param name="pName" value="pValue">