Top 20 JSP Standard Tag Library (JSTL) Interview Questions You Must Prepare 19.Mar.2024

JSTL Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. To use JSTL core tags, we should include it in the JSP page like below.

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

  1. Fast Developement JSTL provides many tags that simplifies the JSP.
  2. Code Reusability We can use the JSTL tags in various pages.
  3. No need to use scriptlet tag It avoids the use of scriptlet tag.

Formatting tags: JSTL Formatting tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles. We can include these jstl tags in JSP with below syntax:

<pre class="prettyprint">

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

JSTL Core Tags :

  1. <c:out> : To write something in JSP page, we can use EL also with this tag.
  2. <c:import> : Same as <jsp:include> or include directive.
  3. <c:redirect> : redirect request to another resource.
  4. <c:set> : To set the variable value in given scope.
  5. <c:remove> : To remove the variable from given scope.
  6. <c:catch> : To catch the exception and wrap it into an object.
  7. <c:if> : Simple conditional logic, used with EL and we can use it to process the exception from <c:catch>.
  8. <c:choose> : Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <c:when> and <c:otherwise>.
  9. <c:when> : Subtag of <c:choose> that includes its body if its condition evalutes to 'true'.
  10. <c:otherwise> : Subtag of <c:choose> that includes its body if its condition evalutes to 'false'.

The JavaServer Pages Standard Tag Library (JSTL) is a component of the Java EE Web application development platform. It extends the JSP specification by adding a tag library of JSP tags for common tasks, such as XML data processing, conditional execution, database access, loops and internationalization.

JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL forEach tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :

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

<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
      <c:forEach var="message" items="${errorMsgs}" >
        <li>${message}</li>
      </c:forEach>
  </body>
</html>

Here the attribute items has its value as an EL expression which is a collection of error messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.

JSTL XML Tags: JSTL XML tags are used to work with XML documents such as parsing XML, trforming XML data and XPath expressions evaluation. Syntax to include JSTL XML tags in JSP page is:

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

SQL tags : JSTL SQL Tags: JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries, we include these JSTL tags in JSP with below syntax:

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

JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the when tag evaluates to true, then the content within when tag is evaluated, otherwise the content within the otherwise tag is evaluated.

We can also implement if-else-if construct by using multiple when tag. The when tags are mutually exclusive, that me the first when tag which evaluates to true is evaluated and then, the control exits the choose block. If none of the when condition evaluates to true, then otherwise condition is evaluated. For Example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
    <c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
           <c:choose>    
        <c:when test="${status.count %2 == 0 }">
 <p> Divisible by 2 : ${tutorial.key} </p>
 <br/>
        </c:when>
        <c:when test="${status.count %5 == 0 }">
 <p > Divisible by 5 : ${tutorial.key} </p>
 <br/>
        </c:when>
        <c:otherwise>
 <p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
        </c:otherwise>
     </c:choose>
    </c:forEach>
  </body>
</html>

JSP - The taglib Directive. Advertisements. The JavaServer Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior. ... When you use a custom tag, it is typically of the form <prefix:tagname>.

JSTL catch tag: The JSTL catch tag is used to handle exception and doesn't forward the page to the error page. For Example :

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

<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
    <c:catch>
      <% int a = 0;
         int b = 10;
         int c = b/a;
      %>
     </c:catch>
  </body>
</html>

Formatting Tags Descriptions :

  1. fmt:parseNumber : It is used to Parses the string representation of a currency, percentage or number.
  2. fmt:timeZone : It specifies a parsing action nested in its body or the time zone for any time formatting.
  3. fmt:formatNumber : It is used to format the numerical value with specific format or precision.
  4. fmt:parseDate : It parses the string representation of a time and date.
  5. fmt:bundle : It is used for creating the ResourceBundle objects which will be used by their tag body.
  6. fmt:setTimeZone : It stores the time zone inside a time zone configuration variable.
  7. fmt:setBundle : It loads the resource bundle and stores it in a bundle configuration variable or the named scoped variable.
  8. fmt:message : It display an internationalized message.
  9. fmt:formatDate : It formats the time and/or date using the supplied pattern and styles.

The if tag is a conditional tag used to evaluate conditional expressions. When a body is supplied with if tag, the body is evaluated only when the expression is true. For Example :

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

<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
      <c:if test="${param.name == 'studytonight'}">
     <p>Welcome to ${param.name} </p>
    </c:if>
  </body>
</html>

Function tags: JSTL Functions Tags: JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc. Syntax to include JSTL functions in JSP page is:

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

JSTL out tag: The out tag is used to evaluate an expression and write the result to JspWriter. For Example :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Tag Example</title>
  </head>
  <body>
      <c:out value="${param.name}" default="StudyTonight" />
  </body>
</html>

The value attribute specifies the expression to be written to the JspWriter. The default attribute specifies the value to be written if the expression evaluates null.

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.

JSTL Function Tags List :

  1. fn:contains() : It is used to test if an input string containing the specified substring in a program.
  2. fn:containsIgnoreCase() : It is used to test if an input string contains the specified substring as a case insensitive way.
  3. fn:endsWith() : It is used to test if an input string ends with the specified suffix.
  4. fn:escapeXml() :  It escapes the characters that would be interpreted as XML markup.
  5. fn:indexOf() : It returns an index within a string of first occurrence of a specified substring.
  6. fn:trim() : It removes the blank spaces from both the ends of a string.
  7. fn:startsWith() : It is used for checking whether the given string is started with a particular string value.
  8. fn:split() : It splits the string into an array of substrings.
  9. fn:toLowerCase() : It converts all the characters of a string to lower case.
  10. fn:toUpperCase() : It converts all the characters of a string to upper case.

 

 

 

There JSTL mainly provides 5 types of tags:

  1. Core tags.
  2. Function tags.
  3. Formatting tags.
  4. XML tags.
  5. SQL tags.

  1. sql:setDataSource: It is used for creating a simple data source suitable only for prototyping.
  2. sql:query: It is used for executing the SQL query defined in its sql attribute or the body.
  3. sql:update: It is used for executing the SQL update defined in its sql attribute or in the tag body.
  4. sql:param: It is used for sets the parameter in an SQL statement to the specified value.
  5. sql:dateParam: It is used for sets the parameter in an SQL statement to a specified java.util.Date value.
  6. sql:traction:  It is used to provide the nested database action with a common connection.

JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.