Advanced Java Server Pages (JSP)
Advanced Java Server Pages (JSP)
Advanced Java Server Pages (JSP)
Custom Tags
4-Create a JSP file, import your custom tag then use it.
• The tlibversion element specifies the version number of the tag library in the
following format:
([0-9])* ("." [0-9])? ("." [0-9])? ("." [0-9])?
1.1.1.1 , 22 , 2.3 , 3.3.6
• The jspversion element specifies the version number of JSP used.
• The shortname element specifies a short name for the tag library. The value for this
element must begin with a letter and must not contain blank space.
• The info element contains the information used for documentation purposes. Its
default value is an empty string.
• The uri element specifies the link to an additional source of documentation for the tag
library.
• The tag element is the most important element in a tag library. You can specify more
than one tag element in the same TLD file.
– Tagclass (mandatory): specifies the fully qualified name of the Java class
that handles the processing of this tag.
– Teiclass : specifies the helper class for this tag, if there is one.
– Bodycontent: specifies the type of the body content of the tag, if any. A
body content is what appears between the opening and closing tags. This
element can have one of the following values: empty, JSP, or tag
dependent.
<taglib>
<tlibversion> 1.0 </tlibversion>
<jspversion> 2.0 </jspversion>
<shortname> Mytag </shortname>
<tag>
<name> myTag </name>
<tagclass> mylibrary.MyCustomTag </tagclass>
</tag>
</taglib>
• The JSP container can find the name and location of the
TLD file by looking up the deployment descriptor
• This is preferable but not obligatory
• Example:
• <taglib>
– <taglib-uri> /myTld</taglib-uri>
– <taglib-location>/WEB-INF/taglib.tld </taglib-location>
</taglib>
– <%@ taglib uri="/myTld" prefix="easy"%>
– <easy:myTag/>
<taglib>
<tlibversion>1.0</tlibversion>
<shortname>Mytags</shortname>
<tag>
<name>myTag</name>
<tagclass>mylibrary.DoublerTag</tagclass>
<attribute>
<name>number</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
</tag>
</taglib>
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 23
Complete Example (cont’d)
<JETS:myTag number="12"/>
JSTL
What is JSTL ?
Core Package
Database Package.
• Simply by :
– ${param.name}
– ${paramValues.name}
• Example
<p>Wow, I know a lot about you...</p>
<p>Your name is ${param.username}/>.</p>
<p>Your password is ${param.pw}/>.</p>
<p>You are ${param.gender}/>.</p>
• Example:
<c:if test="${fatalError}">
I’m sorry,
<c:if test="${user.education==’doctorate’}">
Dr.
</c:if>
<c:out value="${user.name}"/>,
but you have committed a fatal error.
</c:if>
<c:choose>
<c:when test="${num==2}">
<li>Error 1 has occurred.</li>
</c:when>
<c:when test="${num==3}">
<li>Error 2 has occurred.</li>
</c:when>
<c:when test="${num==4}">
<li>Error 3 has occurred.</li>
</c:when>
<c:otherwise>
<li>Everything is fine.</li>
</c:otherwise>
</c:choose>
<c:choose>
<p> hiiiiiiiiiiiiiiiiiii </p>
<c:when> … </c:when>
<c:otherwise> … </c:otherwise>
</c:choose>
<c:choose>
<c:otherwise> … </c:otherwise>
<c:when> … </c:when>
</c:choose>
• lets you loop over nearly any sensible collection of items that the
expression language returns
• Example:
– <c:forEach items="${user.medicalConditions}" var="ailment">
– <c:out value="${ailment}"/>
– </c:forEach>
• <c:forEach> accepts:
– Arrays
– Collection variables (including Lists and Sets),
– Maps, Iterators, and Enumerations.
– simple strings.
• <c:import url="target.jsp"/>
• <c:import url="http://www.cnn.com"/>
• We have to use :
– <%@ taglib prefix="sql"
uri="http://java.sun.com/jstl/sql" %>
<sql:setDataSource
driver=―sun.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc:dsn2"
user=―jstl― password=―iti"
var="databaseOne"
scope="session" />
<sql:setDataSource
dataSource="${databaseOne}"
scope="request" />
– <sql:query var="result">
SELECT * FROM CUSTOMERS
</sql:query>
– <sql:query var="result" sql="SELECT * FROM
CUSTOMERS"/>
• Example
• Sun presentations
• Oracle presentations