JSTL

  • JSTL stands for JSP Standard Tag Library
  • JSTL is collection of utility tags can be used by developers in JSP
  • JSTL is implemented by SUN

JSTL Types

  • Core tags
  • Formatting tags
  • SQL tags
  • XML tags

JSTL Core Tags

<c:out>

  • It is used to display data to JSP output stream
  • attributes: value
  • Usage:
    • <c:out value="Atul" />
      <c:out value="${msg}" />

<c:set>

  • It is used to set the attribute in the required scope
  • attributes: var, value, scope
  • Usage:
    • <c:set var="name" value="Atul" scope="session" />
      Equivalent to:
      session.setAttribute(name, "Atul");

<c:remove>

  • It is used to remove the attributes from the required scope
  • attributes: var, scope
  • Usage:
    • <c:remove var="name" scope="session" />
      Equivalent to
      session.removeAttribute(“name");

<c:if>

  • It is used to perform conditional checks
  • attributes: test
  • Usage:
    • <c:if test="${name eq “Atul"}">You are Atul</c:if>

<c:choose> <c:when> and <c:otherwise>

  • These are used to perform conditional checks like switch case statements
  • attributes:
    • choose: no attribute available
    • when: test
    • otherwise: no attribute available
  • Usage:
    • <c:choose>
         <c:when test="${id eq 101}">
             id is 101
         </c:when>
         <c:when test="${id eq 102}">
             id is 102
         </c:when>
         <c:otherwise>
             id is not available
         </c:otherwise>
      </c:choose>
      

<c:forEach>

  • It is used to access elements of collection or array
  • attributes: var, items, start, end, step, carStatus
  • Usage: You can access the following type of data
    • Collection of String, Wrappers and Date
    • Collection of Collections
    • Collection of custom or user defined objects
    • Map objects
    • Collection of map objects

<c:import>

  • It is used to include the resources (HTML or JSP)
  • attribute: url
  • Usage:
    • <jsp:include page="header.jsp" />
      <c:import url="header.jsp" />
      <c:import url=http://www.google.com />
  • <jsp:include> can include the resources which are in same server
  • <c:import> can include the resources which are in same and different server

<c:redirect>

  • It is used to redirect request to the specified resource (HTML or JSP)
  • attributes: url
  • Usage
    • <c:redirect url="header.jsp" />
      <c:redirect url=http://www.google.com />
  • <jsp:forward> can forward the request to resources which are in same application
  • <c:redirect> can forward request to resources which are in same and different application

<c:param>

  • It is used to define parameters
  • It must be used along with <c:import> and <c:redirect>
  • Usage:
    • <c:param name="myName" value="Atul" />
  • Examples:
    • <c:import url="header.jsp">
      <c:param name="companyName" value="My Company" />
      </c:import>
      
      <c:redirect url="home.jsp">
      <c:param name="companyName" value="My Company" />
      </c: redirect>
      

<c:url>

  • It is used to encode the url with sessionid
  • attribute: value
  • Usage:
    • <c:url value="hello.jsp" />
      <a href="hello.jsp">Click Here</a>
      <a href='<c:url value="hello.jsp" />'>Click Here</a>
      <a href='<%= response.encodeURL("hello.jsp")%>'>Click Here</a>
 


Footer with Map