Upload
golden-sunshine
View
12
Download
2
Embed Size (px)
Citation preview
Ver. 1.0 Slide 1 of 25
Web Component Development With Servlet and JSP™ TechnologiesObjectives
In this session, you will learn to:Describe the Java EE job roles involved in web application developmentDesign a web application using custom tagsUse JSTL tags in a JSP page
Ver. 1.0 Slide 2 of 25
Web Component Development With Servlet and JSP™ Technologies
The following roles are involved in web application development:
Web designers:Are responsible for creating the views of the application, which are primarily composed of HTML pages.
Web component developers:Are responsible for creating the control elements of the application, which are mostly Java technology code.
Business component developers:Are responsible for creating the model elements of the application, which might reside on the web server or on a remote server.
The Java EE Job Roles Involved in Web Application Development
Ver. 1.0 Slide 3 of 25
Web Component Development With Servlet and JSP™ Technologies
A custom tag is an XML tag used in a JSP page to represent some dynamic action or the generation of content within the page during runtime.Custom tags are used to eliminate scripting elements in a JSP page.JSTL tags provides the following advantages:
Java technology code is removed from the JSP pageCustom tags are reusable componentsStandard job roles are supported
Designing JSP Pages With Custom Tag Libraries
Ver. 1.0 Slide 4 of 25
Web Component Development With Servlet and JSP™ Technologies
A custom tag library is a collection of custom tag handlers and the tag library descriptor file. A custom tag library is a web component that is part of the web application.Custom tag handlers uses the pageContext object to:
Access page, request, session, and application attribute scopes.Access all implicit objects in the JSP page.
Custom Tag Library Overview
Ver. 1.0 Slide 5 of 25
Web Component Development With Servlet and JSP™ Technologies
All the custom tags must follow the following XML rules:Standard tag syntax must conform to the following structure:<prefix:name { attribute={”value”|’value’}}*>body</prefix:name>
For example:<c:forEach var=”message” items=”${errorMsgs}”><li>${message}</li></c:forEach>
Empty tag syntax must conform to the following structure:<prefix:name { attribute={”value”|’value’}}* />
For example:<c:url value=”addLeague.do” />
Custom Tag Syntax Rules
Ver. 1.0 Slide 6 of 25
Web Component Development With Servlet and JSP™ Technologies
Tag names, attributes, and prefixes are case sensitive.For example:<c:forEach> is different from <c:ForEach><c:forEach> is different from <C:forEach><c:forEach items=”collection”> is different from <c:forEach ITEMS=”collection”>
Tags must follow nesting rules, as shown in the following structure:<tag1> <tag2> </tag2></tag1>
Custom Tag Syntax Rules (Contd.)
Ver. 1.0 Slide 7 of 25
Web Component Development With Servlet and JSP™ Technologies
The following code is an example of correct nesting:<c:if test=”${not empty errorMsgs}”><c:forEach var=”message” items=”${errorMsgs}”><%-- JSP code showing a single error message--%></c:forEach></c:if>
Custom Tag Syntax Rules (Contd.)
Ver. 1.0 Slide 8 of 25
Web Component Development With Servlet and JSP™ Technologies
The JSTL core library contains the following tags that reduce the scripting in a JSP page:
seturlout
JSTL Sample Tags
Ver. 1.0 Slide 9 of 25
Web Component Development With Servlet and JSP™ Technologies
JSTL set Tag:The set tag is used to store a variable in a named scope, or update the property of a JavaBeans instance or Map.The two ways of using the set tag to store a variable are:
<c:set var=”varName” value=”value” [scope=”{page|request|session|application}”]/> </c:set>
<c:set var=”varName” [scope=”{page|request|session|application}”]> value in body </c:set>
JSTL Sample Tags (Contd.)
Ver. 1.0 Slide 10 of 25
Web Component Development With Servlet and JSP™ Technologies
The following code demonstrates how the set tag is used to create the variable pageTitle, which is later used in an EL expression to specify the title of the JSP page:<%-- Set page title --%><c:set var=”pageTitle”>Duke’s Soccer League: Registration</c:set><%-- Generate the HTML response --%><html><head><title>${pageTitle}</title></head>
JSTL Sample Tags (Contd.)
Ver. 1.0 Slide 11 of 25
Web Component Development With Servlet and JSP™ Technologies
The two ways to use the set tag to modify the property of a JavaBeans instance or Map are:
<c:set target=”targetName” property=”propertyName”
value=”value” /> </c:set>
<c:set target=”targetName” property=”propertyName”>
value in body </c:set>
JSTL Sample Tags (Contd.)
Ver. 1.0 Slide 12 of 25
Web Component Development With Servlet and JSP™ Technologies
JSTL url Tag:The url tag is used to provide a URL with appropriate rewriting for session management. The syntax of the url tag is:<c:url value=”value” [var=”varName”] [scope=”{page|request|session|application}”] />
The following code demonstrates how the url tag can be used in a JSP page to perform URL-rewriting:<%-- Present the form --%> <form action=’<c:url value=”enter_player.do”/>’ method=’POST’>
JSTL Sample Tags (Contd.)
Ver. 1.0 Slide 13 of 25
Web Component Development With Servlet and JSP™ Technologies
JSTL out Tag:The out tag is used to evaluate an expression and write the result to the current JspWriter.The two forms of the out tag are:
<c:out value=”value” [escapeXml=”{true|false}”] [default=”defaultValue”] /> </c:out>
<c:out value=”value” [escapeXml=”{true|false}”]> default value </c:out>
For example, you can use the following code to display the request parameter, email, or the string, no email provided, if the parameter does not exist:
<c:out value=”${param.email}” default=”no email provided” />
JSTL Sample Tags (Contd.)
Ver. 1.0 Slide 14 of 25
Web Component Development With Servlet and JSP™ Technologies
The custom tag library is made up of the following two parts: The JAR file of tag handler classes The Tag Library Descriptor (TLD)
The TLD is an XML file that names and declares the structure of each custom tag in the library.A JSP page can use a tag library by directing the JSP technology translator to access the TLD. This is specified using the JSP technology taglib directive.This directive includes the TLD URI and a custom tag prefix.
Using a Custom Tag Library in JSP Pages
Ver. 1.0 Slide 15 of 25
Web Component Development With Servlet and JSP™ Technologies
A JSP page may include several tag libraries. The prefix is used to distinguish custom tags from each of these libraries. The prefix is prepended to every custom tag name. Every custom tag from a given library which is used in the JSP page must use the prefix assigned to that library.The TLD file is usually stored in the WEB-INF directory.The JAR file is stored in the WEB-INF/lib directory. The symbolic name can also be specified within the tag library’s JAR file.
Using a Custom Tag Library in JSP Pages (Contd.)
Ver. 1.0 Slide 16 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table lists the five functional categories of tags in JSTL, their URI values, and the typical prefix used for each.
JSTL Tags
Functional Area
URI Prefix
Core actions http://java.sun.com/jsp/jstl/core c
XML processing actions
http://java.sun.com/jsp/jstl/xml x
Formatting actions
http://java.sun.com/jsp/jstl/fmt fmt
Relational database access
http://java.sun.com/jsp/jstl/sql sql
Function actions
http://java.sun.com/jsp/jstl/functions fn
Ver. 1.0 Slide 17 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table describes some of the core tag library tags.
JSTL Tags (Contd.)
Tag Purpose
c:out Evaluates an expression and outputs the result tothe current JspWriter
c:set Sets the value of a scoped variable or property
c:remove Removes a scoped variable
c:catch Catches a java.lang.Throwable that occurs in the body of the tag
c:if Evaluates the body of the tag if the expression specified by the test attribute is true
Ver. 1.0 Slide 18 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table describes some of the xml tag library tags.
JSTL Tags (Contd.)
Tag Purpose
x:parse Parses an XML document
x:out Evaluates the XPath expression and outputs the result to the current JspWriter
x:set Evaluates the XPath expression and stores the result in a scoped variable
x:choose Provides for a mutually exclusive conditional
x:otherwise Provides the final alternative within an x:choose element
Ver. 1.0 Slide 19 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table describes some of the format tag library tags.
JSTL Tags (Contd.)
Tag Purpose
fmt:setLocale Stores the specified locale in the localeconfiguration variable
fmt:bundle Creates an i18n localization context that is used in the tag body
fmt:setBundle Creates an i18n localization context and stores it in the scoped variable or the localization context configuration variable
fmt:message Finds the localized message in the resource bundle
fmt:param Supplies a parameter for replacement within a fmt:message element
Ver. 1.0 Slide 20 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table describes some of the database tag library tags.
JSTL Tags (Contd.)
Tag Purpose
sql:query Queries the database, storing the result set in a scoped variable
sql:update Executes an INSERT, DELETE, UPDATE, or SQL DDL statement, storing the result in a scoped variable
sql:transaction Establishes a transaction context for the sql:query and sql:update elements
sql:setDataSource Exports a data source either as a scoped variable or as the data source configuration variable
sql:param Sets the values for parameter markers (used with the sql:query and sql:update elements)
Ver. 1.0 Slide 21 of 25
Web Component Development With Servlet and JSP™ Technologies
The following table describes some of the standard EL functions.
JSTL Tags (Contd.)
Tag Purpose
fn:contains Performs a case-sensitive test for a specifiedsubstring, returning true or false
fn:containsIgnoreCase Performs a case-insensitive test for aspecified substring, returning true orfalse
fn:endsWith Tests if a string ends with a specified suffix,returning true or false
fn:escapeXml Escapes characters that would beinterpreted as XML markup
fn:indexOf Returns the position of the first occurrenceof a specified substring
Ver. 1.0 Slide 22 of 25
Web Component Development With Servlet and JSP™ Technologies
Investigate the use of core JSTL tags.
Demo 8-1: Developing JSP Pages Using Custom Tags
Ver. 1.0 Slide 23 of 25
Web Component Development With Servlet and JSP™ Technologies
Solution:1. Investigate the c:out core tag.2. Investigate the c:remove core tag.
Demo 8-1: Developing JSP Pages Using Custom Tags (Contd.)
Ver. 1.0 Slide 24 of 25
Web Component Development With Servlet and JSP™ Technologies
In this session, you learned that:Custom tags are fundamentally the same as standard tags, but you can acquire tag libraries from third parties and even build your own application-specific tags.JSTL provides a collection of general purpose tags.A tag library can be added to a JSP pages by using the <%@ taglib %> directive.Custom tags use standard XML tag syntax.Custom tags, standard tags, and EL eliminate all the scriptlet code in the JSP pages.
Summary
Ver. 1.0 Slide 25 of 25
Web Component Development With Servlet and JSP™ Technologies
The next session covers Lab@Home. In this session, you need to perform exercises given in the following table.
Ensure to take the required input files from the faculty for performing these exercises.
What’s Next?
Chapter Number Exercise NumberChapter 5 (Book 2) Exercise 1Chapter 5 (Book 4) Exercise 1