Java Tutorial - Java Script : JSP

Java Tutorial - Java Script :

JSP
within “${“ and “}” characters. In the preceding example, the variable param is one of several standard Expression Language variables that contain information about the page, web application, and servlet container. The param variable is a collection that holds all of the page parameters, each represented as a string. The rest of the page contains three core actions used to create a conditional block:

<c:choose>
<c:when test=’${!empty param.name}’>
<h2>Hello, <c:out value=’${param.name}’/></h2>
</c:when>
<c:otherwise>
<h2>Hello, stranger</h2>
</c:otherwise>
</c:choose>

The c:choose-c:when-c:otherwise block mirrors the functionality of a switch-casedefault Java statement, displaying enclosed HTML output only for the first c:when action that has a test attribute with the value true. If none of the actions is true, the c:otherwise contents are displayed. JSTL is composed of five tag libraries:
·         The core library (prefix c, default URI http://java.sun.com/jsp/jstl/core) contains general features: output, conditional display of content, looping, variable creation in several scopes, JavaBeans access, exception handling, URL imports, and URL redirection.
·         The SQL library (prefix sql, default URI http://java.sun.com/jsp/jstl/sql) covers database access: data source selection, queries, updates, transactions, and looping through results.
·         The internationalization and formatting library (prefix fmt, default URI http://java.sun.com/jsp/jstl/fmt) offers these actions: locale and resource bundle use, text localization, and number and date formatting.
·         The XML processing library (prefix x, default URI http://java.sun.com/jsp/jstl/HYPERLINK "http://java.sun.com/jsp/jstl/xml"xml) supports XML: parsing, XPath access, XSLT transformation, looping through nodes, and conditional processing.
·         The function library (prefix fn, default URI http://java.sun.com/jsp/jstl/functions) contains useful functions to manipulate strings and collections.
The Expression Language has operators to retrieve information from instance variables (${object.varName}), data structures (${object[“name”]}), and indexed arrays or lists (${object[1]}). There also are operators for arithmetic (“+”, “-”, “*”, “/”, and “%”), comparisons (“==”, “!=”, “<”, “<=”, “>”, and “>=”), logic (“&&”, “||”, and “!”), and empty, which detects
null objects and empty strings or collections. Parentheses can be used to group subexpressions. The language offers automatic type conversion and five kinds of literals: strings, which are enclosed within single or double quotes, integers, floating-point numbers, boolean values (true or false), and null. There are seven special variables available in any expression:

·         cookie, a collection of all request cookies, each as an instance of the Cookie class from the javax.servlet.http package
·         header, a collection of all request headers, each as a string
·         headerValues, a collection of all request headers, each as a string array
·         initParam, a collection of all application initialization parameters, each as a string
·         pageContext, an instance of jspPageContext from the javax.servlet package
·         param, a collection of all request parameters, each as a string
·         paramValues, a collection of all request parameters, each as a string array

When using one of these collections or other data structures, the c:foreach action makes it easy to loop through each element. The following example displays all of the header variables sent with a JSP page:

<c:forEach items=’${header}’ var=’head’>
<ul>
<li>Name: <c:out value=’${head.key}’/></li>
<li>Value: <c:out value=’${head.value}’/></li>
</ul>
</c:forEach>

Four variables can be used to make explicit references to variable scope:

·         applicationScope, a collection of all application scope objects
·         pageScope, a collection of all page scope objects
·         requestScope, a collection of all request scope objects
·         sessionScope, a collection of all session scope objects

For example, the expression ${sessionScope.price} retrieves an object named price in session scope. If no other scope has an object named price, the expression ${price} also works. Today’s final project shows some of the power and flexibility of JSTL. This page makes use of XML actions to present data from an RSS newsfeed, an XML format for offering web content in machine-readable form. JSTL can import and parse XML, HTML, or any other data available at a URL, even if it isn’t on the same server as the page making use of it. RSS, which stands for Really Simple Syndication, enables websites to share headlines, links, and other content with each other (and with readers using software called an RSS aggregator). Listing 21.13 contains an example: a simplified version of the RSS feed for the SportsFilter website.
SportsFilter, a popular sports community weblog at http://www.sportsfilter.com, shares the latest news in an RSS 2.0 feed. The XML data from the feed can be read into a variable using the c:import action from the core library and parsed with the x:parse action from the XML library. The parsed data can be examined, filtered, transformed, and displayed. Listing 21.14 contains a JSP application that uses simple XPath statements to extract and display parts of the XML data.
Comments on the page describe the JSTL actions that it contains. The most challenging part of JSTL is the Expression Language, which can be avoided by using versions of the tag libraries that use JSP syntax for Java statements and expressions. JSTL’s Expression Language is distinguished from the other JSP syntax by the limits on what it can do. There are no assignment operators or conditional logic, which forces developers to use external Java classes and JSTL actions for these tasks. Though some Java programmers might blanch at the thought of learning another language simply for web applications, the Expression Language is simple enough to be picked up quickly, especially by those who are comfortable with JavaScript. By keeping Java code out of JSP pages, the Expression Language offers reusability and reliability. In conjunction with JSTL, it brings JSP much closer to the promise of separating the presentation of a web application from the code required to make it happen. JSTL serves as a nice complementary offering to Struts, an open source web application framework that’s also the work of the Apache Jakarta project.