JSTL
JSTL:-
-->Developing jsp components as java code less jsp components is best practice , for these we need to use tags and EL
-->The jsp built-in tags , EL are not sufficient in all angles to use in jsp
-->The custom tags development is complex process
-->Arranging 3rd party jsp tags is not possible in all situations
##To overcome all these problems we can use JSTL
-->JSTL(JSP Standard Tag Library) is a specification given by sun micro system having set of tag libraries tags and tld file designing but implementation of these tags functionalities and tag handler classes is done by server vendor companies by supplying jar files
-->Upto Tomcat8 jstl.jar, standard.jar are supplied having jstl tags implementation
-->Tomcat9 gives taglibs-standard-spec-1.2.5.jar & taglibs-standard-impl-1.2.5.jar for the same
-->To develop jsp component as the best component we need to use following tags and the following order:-
(1)Jsp Built-in tags
(2)EL Tags
(3)JSTL Tags
(4)3rd party or framework tags
(5)Custom tags
Note: Avoid the following 3 tags forever( scriptlet, expression & declarative)
JSTL Taglibraries are
Core-->For basic operation(variable declarations, conditional loops and etc...)
SQL-->For DB operations
Formatting-->For internalization (I18N) for formatting numbers , dates and etc...
XML-->For xml data manipulation
Functions-->For string manipulation
-->To use any jsp tag library in our jsp component we need to place it's taglib uri in the jsp component by collecting it from the tld file of taglib url
- Core-->c.tld(tld file)-->http://java.sun.com/jsp/jstl/core(taglib uri)
- SQL-->sql.tld-->http://java.sun.com/jsp/jstl/sql(taglib uri)
- Formatting-->fmt.tld-->http://java.sun.com/jsp/jstl/fmt(taglib uri)
- XML-->x.tld-->http://java.sun.com/jsp/jstl/xml(taglib uri)
- Functions-->fn.tld-->http://java.sun.com/jsp/jstl/functions(taglib uri)
JSTL Core tag library:-
-->Tags for basic operations
-->The tags <set>, <if>, <choose>, <when>, <otherwise>, <catch>, <redirect>, <param>, <out>, <remove>(to remove data from scope), <forEach>, <forTokens>, <url>,<import> and etc...
-->Recommended prefix : "c"
Note: We can use EL in jstl tags
(1)<c:out>
-->It is like expression tag to evaluate the expression and displays the values
Syn:-
- <c:out value="..."(value to be displayed) default="..."(default value to display when value is null)/>
(2)<c:set>
-->To create a data for a variable having scope
Syn:-
- <c:set var=".." value=".." scope=".."/>
-->Creates given variable with value of given scope
Syn(2):-
- <c:set target="<bean id/obj>" property=".."/>
-->To set value to java bean property
(3)<c:remove>
-->To remove variable from the scope
Syn:-
- <c:remove var=".." scope=".."/>
Example:-
JSTLCoreApp
|-->webcontent
|-->ex1.jsp
|-->WEB-INF
|-->lib
|-->*.jar(2)
|-->web.xml
Jars in build path and WEB-INF lib folder
taglibs-standard-spec-1.2.5.jar & taglibs-standard-impl-1.2.5.jar
(4)<c:if>
-->For placing conditional operators in jsp
Syn(1):-
- <c:if test=".."/>
Syn(2):-
- <c:if test=".." var=".." scope=".."/>
-->Stores the condition results in the scoped variable
ex1.jsp
- <c:set var="msg" value="welcome"/>
- <c:if test="${|empty param.uname)}">
- <c:out value="${msg}"/>
- <c:out value="${param.uname}"/>
- </c:if>
Url: http://localhost:1025/JSTLCoreApp/ex1.jsp?uname=teja
-->While working with JSTL tags we can use the regular implicit objects and we can also use EL supplied implicit objects as shown above
(5)<c:catch>
-->Given to catch and handle the exceptions that are raised in the java code placed in scripting tags of jsp
Syn:-
- <c:catch var=".."/>
var=variable name that holds the details of raised exception
ex3.jsp
- <c:catch var="e">
- <jsp:scriptlet>
- int x=Integer.parseInt("a10");
- out.println("value"+x);
- </jsp:scriptlet>
- </c:catch>
- ${e}
(6)<c:url>
-->Defines to place request url in a variable
Syn:
- <c:url var=".(var name)." value=".."/>
(7)<c:import>
-->Useful to import content of file/resource in current jsp
Syn:-
- <c:import url=".(file/resource url)."/>
ex4.jsp
- <c:url var="link1" value="https://jsp2java.blogspot.com"/>
- <a href="${link1}">jsp2java</a>
- <br>
- <c:import url="ex3.jsp"/>
(8)<c:redirect>
-->Useful to perform SendRedirection operation
Syn:
- <c:redirect url=".(destination component)."/>
Note: It is tag based alternate to response.sendRedirect(-) method
ex6.jsp
- <c:redirect url="https://www.google.com/search?q=hello"/>
(9)<c:choose>,<c:when>,<c:otherwise>
-->alternate to switch...case...default
Syn:
- <c:when test=".(condition to test)..">
ex7.jsp
- <c:choose>
- <c:when test="${param.p>0)">
- $(param.p) is +ve
- </c:when>
- <c:when test="${param.p<0)">
- $(param.p) is -ve
- </c:when>
- <c:otherwise>
- ${param.p} is zero
- </c:otherwise>
- </c:choose>
URL: http://localhost:1025/jstlcore/ex7.jsp?p=10
(10)<c:forEach>
-->Alternate to enchanced, traditional for loop
-->Useful to iterate through list collection, arrays and etc...
Syn(1):(traditional for loop)
- <c:forEach var=".." begin=".." end=".." step="..">
- </c:forEach>
E.g:
- <c:forEach var="i" begin="1" end="10" step="1">
- 2*${i}=${2*i}<br>
- </c:forEach>
Syn(2):(Enchanced for loop)
- <c:forEach var=".." items="..">
- </c:forEach>
E.g:
- <jsp:scriptlet>
- String names[]=("bujjamma","roshi","teja");
- request.setAttribute("names", names);
- </jsp:scriptlet>
- <c:forEach var="name(In each iteration it holds one element of names array)"items="${names}">
- ${name}<br>
- </c:forEach>
E.g:Retrieving list collection values
- <jsp:scriptlet>
- <![CDATA[
- List<String>names=new ArrayList<String>();
- names.add("bujjamma");
- names.add("roshi");
- names.add("teja");
- request.setAttribute("names",names);
- ]]>
- </jsp:scriptlet>
- <c:forEach var="name" items="${names}">
- ${name}<br>
- </c:forEach>
(11)<c:forTokens>
-->Alternate to StringTokenizer to split the string into multiple tokens based on the given delimiter
Syn:
- <c:forToken var=".(counter variable)." items=".." delims=".(delimiter)..">
- </c:forToken>
E.g:
- <c:set var="names" value="vinod,roshi,teja" scope="request">
- <c:forTokens var="name" items="${names}" delims=",">
- ${name}
- </c:forTokens>
JSTL SQL Taglibrary:-
-->Here tags are given to DB interaction and operations
-->Recommended prefix: sql
-->Taglib uri: http://java.sun.com/jsp/jstl/sql
-->Tags are
<sql:setDataSource>,<sql:query>,<sql:update>,<sql:param>, <sql:transaction> etc...
(1)<sql:setDataSource>
-->Creates DataSource obj representing connectivity with DB s/w
Syn
- <sql:setDataSource var=".."(datasource obj name) driver=".." url=".." users=".." password=".."/>
<sql:query>
-->To execute select sql and to get ResultSet object
Syn:
- <sql:query dataSource=".."(datasource obj name ${ds} sql=".."(select query) var=".."(ResultSet obj name)/>
JSTLSQLApp
|-->webcontent
|-->ex10.jsp
Jars in build path are:-
Jstl 2 jars and ojdbc6.jar
(2)<sql:update>
-->Given to execute non-select sql queries by sending them to DB s/w
Syn:
- <sql:update dataSource=".." sql=".."(non-select query) var=".."(variable name that holds numeric value representing number of records that are effected)/>
(3)<sql:param>
-->Given to set query param values(?)
Syn
- <sql:param value=".."(value of tha param)/>
Note: In which order params are placed in the query in tha same order values must be set by using <sql:param> tag
(refer ex11.jsp of JSTLSQLApp)
-->According to MVC architecture and layered application development placing java code in jsp is bad practice so working with JSTLSQL tag libraries is not industry standard and it is suggested to avoid them
(4)<sql:transaction>
-->In a jsp multiple non select queries are select quires then instead of placing dataSource object name in every <sql:update>,<sql:query> tags, we can place it only for one time by using <sql:transaction> tag
Syn
- <sql:transaction dataSource="${ds}"
- <sql:update sql=".." var=".."/>
- </sql:transaction>
JSTL Formatting Taglibrary:-
-->Useful to display the content of web pages for different locales, it is like formatting numbers, dates and displaying labels for different locales. This taglibrary is useful to enable internalization(I18N) on the application, enabling I18N is nothing but making our application working for different locales
-->Recommended prefix: fmt
-->Taglib uri: http://java.sun.com/jsp/jstl/fmt
-->Tags are
<fmt:setBundle>, <fmt:setLocale>,<fmt:message>,<fmt:formatNumber>,<fmt:formatDate>and <fmt:timeZone>
(1)<fmt:setLocale>
-->Allows yo specify the locale for this jsp to format the content
Syn:
- <fmt:setLocale value=".."(locale value like fr_CA,fr_FR and etc.../>
(2)<fmt:setBundle>
-->Allows to specify the base properties file name but picks up the file based on base file name and locale
E.g:-If base file name "myfile" and locale is fr_FR then it looks for myfile_fr_FR.properties file
Syn:
- <fmt:setBundle basename=".."(base file name)/>
The properties file names are
- myfile.properties
- myfile_fr_CA.properties(for french)
- myfile_de_DE.properties(for german)
(3)<fmt:message>
-->Get the message from properties file based on the locale that is specified
Syn:
- <fmt:message var=".."(variable that holds message gatheres from properties file) key=".."(key in the properties file)/>
(4)<fmt:formatDate>
-->Formats the give date values based on the locale
- <fmt:formatDate var=".." value=".."(Date to format)/>
(5)<fmt:formatNumber>
-->Formats the given number based on the Locale
Syn:
- <fmt:formatNumber var=".." value=".."(number to format)/>
JSTLFormattingApp
|-->java resources
|-->src
|-->org.commons
|-->myfile.properties
|-->myfile_fr_FR.properties
|-->myfile_de_DE.properties
|-->web content
|-->ex12.jsp
-->While enabling I18N the base file name should be there in every locale property file name and all the properties files must have same keys and different values
JSTL Functions:-
-->Given pre-defined functions for string manipulation
-->Taglib uri: http://java.sun.com/jsp/jstl/functions
-->Recommended prefix: fn
-->The functions are length(), toUpperCase(), toLowerCase(), indexOf(), lastindexOf(), substring(), contains() and etc...
JSTLFunctionsApp
|-->webcontent
|-->ex14.jsp
E.g
- <c:set var="msg" value= "tej kumar chejarla"/>
- length:dollar{fn:length(msg)}<br>
- touppercase: dollar{fn:toUpperCase(msg)}<br>
JSTL XML Taglibrary:-
-->Given to load and process the xml document
-->Taglib URI: http://java.sun.com/jsp/jstl/xml
-->Recommended prefix: "x"
-->Tags are <x:out>,<x:parse>,<x:if>,<x:choose>,<x:when>,<x:otherwise>,<x:forEach> and etc...
JSTLXmlApp
|-->webcontent
|-->ex15.jsp
|-->orders.xml
Jars in WEB-INF\Lib+build path
2 jstl jars+ xalan2.7.1.jar +xerceimpl-2.9.1.jar
--><x:parse> tag checks the content of the given xml document and generates the objects based tree structure having all the tags content
orders.xml
- <orders>
- <order>
- <item>bike...</item>
- <price>100000</price>
- </order>
- <order>
- <item>mobile</item>
- <price>10k</price>
- </order>
- </orders>
Memory Diagram:-
Conclusion:
-->Processing xml document in jsp is bad practice, so don't jstl xml tag library is not industry standard
Note: In real time jstl core, jstl formatting tag libraries are quite regularly used tag libraries
Comments
Post a Comment
Express your feelings