In this tutorial, you will learn how to start using this JSP standard library and its numerous methods and tags. Also, you can have some knowledge on its advantages and how to install JST Library along with the JSTL Core Tags with Examples.
- What is JSTL (JSP Standard Tag Library)
- How to download and install JSTL
- Advantages of JSTL
- Classification of Tags in JSTL
- JSTL Core Tags
- JSTL Formatting Tags
- JSTL Function Tags
- JSTL XML Tags
- JSTL SQL Tags
JSTL stands for JSP Standard Tag Library. It can be used only on JSP pages. JSTL tag can be used for conditional, iteration tags for manipulating the XML document, internationalization tags, and SQL tags. It is used to remove scriptlet code from a JSP page.
The following steps will help you understand how to install the JSTL library within seconds:
- Firstly, Download JSTL.jar and Standarad.jar file (or you will obtain these from your native Apache tomcat installation too!).
- Later place both the files into your ‘WEB-INF/lib’ folder.
- Once you are done with the above step, add them to the classpath.
- At last, you can use JSTL in your project.
- Fast Development: we can provide many tags which simplify the JSP.
- Automatic JavabeansInterospection Support
- Code Reusability: Use the JSTL tags on the previous page.
- Easier for computers to understand
- No need to use a scriptlet tag: It avoids the scriptlet tag.
- Easier for humans to read
JSTL is a standard library tag utilized to perform functionalities in JSP programs. They range under tag library groups depends on their functionality. They are:
- Core tags
- Formatting tags
- Function tags
- XML tags
- SQL tags
JSTL Core tags include those which are related to variables, and flow control, as well as it is a generic way to access URL-based resources.
The following are the JSTL Core Tags:
it is used to display the result of an expression. It is just like an expression tag.
It is used to set the value of a variable in a specified scope.
This JSTL core tag is used to remove an attribute from a specified scope or from all scope.
It is used for exception handling in JSP.
It is used for iteration. It is just like for loop in java.
It is used to add a parameter and its values to the output of these tags.
It is used for redirecting the current page to another URL.
To rewrite the URL returned from a JSP page, we can use URL tag.
It is used to testing the condition. It is just like if statement in java.
It is used for importing the content from another file/page to the current JSP page.
JSTL formatting tags are used to formatting the text, date, number, etc. The JSTL Formatting tags are also used to formatting the date, and time of internationalization websites.
The following are the JSTL Formatting Tags:
i) fmt:timeZone: It specifies the time zone for any time formatting or parsing action nested in its body.
ii) fmt:setTimeZone: Stores the given time zone in the time zone configuration variable.
iii) fmt:formatDate: Formats a date and/or time using the supplied styles and pattern.
iv) fmt:message: used to display an internationalized message.
v) fmt:formatNumber: It is used to render numerical value with a specific precision or format.
vi) fmt:parseNumber: Parses the string representation of number, currency, or percentage.
vii) fmt:parseDate: Parses the string representation of the date and/or time.
viii) fmt:bundle: Load a resource bundle to be used by its tag body.
ix) fmt:setBundle: Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
This tag provides a number of predefined functions that can be used to perform a common operation such as String concatenation, String split, etc. It is used for String manipulation.
The following are the JSTL Function Tags:
i) fn:contains(): It tests if an input string contains the specified substring.
ii) fn:containsIgnoreCase(): It tets if an input string contains the specified substring in a case-insensitive way.
iii) fn:endsWith(): It tets if an input string ends with the specified suffix.
iv) fn:indexOf(): Return the index writing a string of the first occurrence of the specified string.
v) fn:join(): It joins all the elements of an array into a String.
vi) fn:length(): It returns the number of characters in a String or the number of collections in a String.
vii) fn:replace(): It is used to replacing an input string with all occurrences with the given String.
viii) fn:split(): Split a string into an array of a substring.
ix) fn:startsWith(): It tests if an input string starts with the specified prefix.
x) fn:substring(): It returns a subset of a string
xi) fn:substringAfter(): It returns a subset of a string after a specific substring.
xii) fn:substringBefore(): It returns a subset of a String before a specific substring.
It converts all of the characters of a string to a lower case.
xiv) fn:toUpperCase(): It converts all of the characters of a string to the upper case.
xv) fn:trim(): It removes white spaces from both ends of a string.
JSTL XML Tags are used to work with XML documents.It is used for manipulating and creating XML documents. It provides flow control, Transformation, etc.
The following are the JSTL XML Tags:
i) x:out: It evaluates the expression of XPath. It is just Like <%= %>, but for XPath expression.
ii) x:parse: It is used to parse the XML data specified either via an attribute or in the tag body.
iii) x:set: It sets the value of a variable of an XPath expression.
iv) x:choose: Subtag of that will include its body if the condition evaluated to be ‘true.
v) x:when: Subtag of that includes its body if its expression evaluates to ‘true.
vi) x:otherwise: subtag of that follows the tags and run only if all of the prior conditions evaluated to ‘false.
vii) x:if: It evaluates a test XPath expression and if it is true, it processes its body. If the condition is false, then the body is ignored.
viii) x:param: It is used along with the transform tag to set a parameter in the XSLT stylesheet.
ix) x: transform: It applies an XSL transformation to an XML document.
This tag provides SQL support. It is used for interacting with RDBS(Relational Database Management System) such as MySql, Oracle, etc. we can run database queries using SQL tags.
The following are the JSTL SQL Tags:
i) sql:setDataSource: It creates a simple data source suitable for prototyping.
ii) sql:query: It is used to executes the SQL queries defined in its body or through the SQL attribute.
iii) sql:update: It executed the SQL update defined in its body or through the SQL attribute.
iv) sql:param: It sets a parameter in an SQL statement to the specified value.
v) sql:dateParam: It sets a parameter in an SQL statement to the specified java.util.Date value.
vi) sql:transaction: It provides nested database action elements with a shared connection, set up to execute all statements as one transaction.