In
the development of Web Applications to dealing with dynamic content
is very much necessary. This can be achieved using JSTL. JSTL will
make your pages more readable, maintainable and reusable. Using JSTL
.While JSTL are great enhancement in functionality over the JSP 1.2
specification. For example how many times we can re-write the code to
format date.
Following
are the Advantages of using JSTL:
- It’s very easy to learn and its open source and provider wide range of functions
- Lots of ready to use tags are available
- JSP developer can use JSP Tag Library without much knowledge of java
- JSTL is a standardized collection of tag libraries for supporting iterations and conditionals, XML documents parsing, internationalization, and database access using SQL
JSTL
is composed of four different tag libraries:
- Core Tag libraries:
These include tag libraries used for looping, expression evaluation,
and basic input and output operations.
- XML manipulation tag libraries
XML processing is an important feature of tag libraries. It’s used
to access the elements of XML files.
- SQL or Database Tag libraries
All the operations or interaction that can be done on database will
be done using these tag libraries.
- Internationalization and formatting
These are the tags used to parse data or format data. For example
parsing and formatting of date based on different locale.
How can we Add
JSTL to Web Application:
- Go to http://tomcat.apache.org/taglibs/index.html download JSP Standard Tag Library Specification 1.2 or latest (Binary Release).
- Unzip the downloaded file in some directory
- To install Tag libraries to some web application copy the *.jar files from (Extracted_Directory/lib/*.jar) to Web _Application/WEB-INF/lib/*.jar
- Copy all * .tld files to /WEB-INF/*.tld folder
- Make following changes to the web.xml file,
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
- <taglib-location>/WEB-INF/fmt.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
- <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
- <taglib-location>/WEB-INF/c.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
- <taglib-location>/WEB-INF/c-rt.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
- <taglib-location>/WEB-INF/sql.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
- <taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
- <taglib-location>/WEB-INF/x.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
- <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
- </taglib>
Testing JSTL:Let’s test the setup by creating one sample web application and adding simple tag to it.Following code prints all even numbers between 2 to 10.<%@ taglib uri="http://java.sun.com/jstl/core" prefix="j" %><html><head><title>Even Number between 2 to 10</title></head><body><j:forEach var="i" begin="2" end="10" step="2"><j:out value="${i}" /><br /></j:forEach></body></html>
Comments