save up to 40%

JSTL Core Library

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

JSTL Core Library

Rating:

JSP Standard Tag Library (JSTL) is an extension of the JSP language. It includes various operations needed in almost all JSP pages, such as internationalization, conditional processing, XML parsing, making a database connection, etc.

The important tags in the JSTL core library are as follows:


    General purpose tags
  • <c:set> : This tag is used to set the value of an attribute, a bean, or a map. The <c:set> tag has the following two forms:
    1. For setting an attribute
      <c:set var= "variableName" value= "" />
    2. For setting properties of a bean or a map
      <c:set target ="" property="propertyName" value="valueName" />

    If the value attribute of the <c:set> tag evaluates to null, the attribute pointed by var will be removed. However, if the var attribute does not exist, it will be created if the specified value is not null.

  • <c:remove> : This tag is used to remove the value of an attribute or the properties of a bean or a map that was set by the <c:set> tag. The syntax of the <c:remove> tag is as follows:

    <c:remove var="VariableName" scope="ScopeName"/>

    Here, VariableName should be a String literal. The scope attribute is optional and defaults to the page scope.
  • <c:out>: This tag evaluates an expression and writes its value to the output stream. The syntax of the <c:out> tag is as follows:

    <c:out value="TagValue">
    default value
    </c:out>


    The attributes of the <c:out> tag are as follows:
    • value: This is a mandatory attribute. It declares the expression to be evaluated.

    • escapeXml: This is an optional attribute that determines whether characters < , > , &, ', and " in the resulting string should be converted to their corresponding character entity codes.

      If escapeXml is true, the following character conversions are applied:

      Character Character Encoding Code
      <&lt;
      > &gt;
      &&amp;
      '&#039
      "&#034

    • default: If the value of default is null, it means that the value of the expression should be written to the output.
Conditional tags
  • <c:choose>, <c:when>, and <c:otherwise>: The <c:when> and <c:otherwise> tags are used with the <c: choose> tag to perform the function similar to the if-else condition. If the condition specified in the <c:when> tag evaluates to true, the expression following the tag is evaluated. Otherwise, the condition specified after the <c:otherwise> tag is evaluated.
  • <c:if>: This tag evaluates a single test expression. It processes the body content only if that expression evaluates to true. The syntax of the <c:if> tag is as follows:

    <c:if test="expression" var="VariableName" scope="ScopeName">
    Body content
    </c:if>


    The scope attribute is optional and defaults to the page scope.
  • <c:catch>:Like the try-catch block, the <c:catch> tag is used to handle exceptions. However, unlike the try-catch block, there is no separate try block in the <c:catch> tag. This tag is capable of catching any exception that is a subclass of java.lang.Throwable. The syntax of the <c:catch> tag is as follows:

    <c:catch var="exc">
        ---Exception code---
    </c:catch var="exc">


    Here, the var attribute will store a copy of the exception being thrown as exc. If no exception is thrown, the var attribute will store null.
URL tags
  • <c:import>: The <c:import> tag is similar to the <jsp:import> tag. However, the <jsp:import> tag imports resources from within the same servlet container, while the <c:import> tag can import data from within the same container as well as from other servers. The syntax of the <c:import> tag is as follows:

    <c:import url="urlValue" context="expression"
       charEncoding="encodingType" var="varName" scope="scopeName">
    <c:param name="paramName" value="Value"/>
           ...
    </c:import>


    Here, urlValue specifies the resource to import.


  • <c:url>: The <c:url> tag is used for URL rewriting in JSP. The syntax for the <c:url> tag is as follows:

    <c:url value= "urlName" />

    Here, urlName can be an absolute url or a relative url.
  • <c:param>: This tag adds a request parameter to a URL, and is valid only with the <c:import>, <c:redirect>, and <c:url> tags.

    The syntax of the <c:param> tag is as follows:

    <c:param name="paramName" value="paramValue">

  • <c:redirect>: This tag is the JSTL equivalent of the sendRedirect() method of javax.servlet.http.HttpServletResponse. It is used to redirect an HTTP redirect response to a client. The syntax of the <c:redirect> tag is as follows:

    <c:redirect url="urlValue" context="contextValue">
       <c:param name="exprValue" value="expression"/>
          ........
       </c:redirect>
    Iteration tags
  • <c:forEach>: This tag is used to iterate over an integral range or a collection.

    To iterate over an integral range, the <c:forEach> tag has the following syntax:

    <c:forEach var="name" varStatus="name"
       begin="expression" end="expression" step="expression">
          body content
    </c:forEach>


    To iterate over a collection, the <c:forEach> tag has the following syntax:

    <c:forEach items="collection" var="name" varStatus="name"
       begin="expression" end="expression" step="expression">
          body content
    </c:forEach>


    The attributes of the <c:forEach> tag are as follows:
    • var: This is the name of the variable that represents the iteration item.

    • varStatus: This attribute is optional and specifies the status of the variable inside the loop.

    • items: It represents the type of item to iterate over. It can be an array, a collection, or a Map.

    • begin: This optional attribute represents the starting index for the loop.

    • end: This optional attribute represents the ending index for the loop.

    • step: This optional attribute represents the increment for the loop

  • <c:forTokens>:
  • This tag is used to loop over the tokenized elements of a string. It is similar to using a <c:forEach> over a list of StringTokenizer items.

    The attributes of the <c:forTokens> tag are as follows:
    • var: This is the name of the variable that represents the iteration item.

    • varStatus: This attribute is optional and specifies the status of the variable inside the loop.

    • items: It represents a string to iterate over.

    • begin: This optional attribute represents the starting index for the loop.

    • end: This optional attribute represents the ending index for the loop.

    • step: This optional attribute represents the increment for the loop

    • delims: It represents a delimiter, and is similar to a delimiter of a StringTokenizer.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.