Java Tutorial - Java Script :
JSP
JSP Standard Tag Library
For Java programmers eager to apply their skills on the Web, the development of the language
for server-side programming has been one of those “walk before you can crawl” situations.
When servlets were introduced, they made it possible to use Java to create programs similar to CGI scripts that collect input from web forms and URL parameters, producing HTML output in response. This works great for writing programs to process mail and handle other simple tasks, but as larger web applications require multiple pages, it becomes clear that producing HTML output using Java statements can be cumbersome. Revisions become more difficult, especially for any nonprogrammers involved in the work. Two years later, Sun took another step in the right direction with JSP, which make it easy to combine static HTML output with dynamic output created by Java statements.
Using JSP, Java code can be placed on web pages among HTML markup and edited like any other page. Programmers also can create their own custom JSP markup tags to interact with Java objects. The pages compile into servlets automatically. Unfortunately, the ease with which Java code could be placed on a JSP page proved to be a misstep because it encourages the bad habit of placing a lot of mission-critical application code in a place where it’s hard to maintain, is not secure, and is easily bungled by anyone editing the HTML around the code. For example, statements to create, open, and query a database connection, complete with usernames and passwords to gain access to the data, can be put on a JSP page. A giant step has been taken with the release of the JSP Standard Tag Library (JSTL), a set of custom JSP tags and a data-access language that enable JSP-based web applications to handle presentation without ever resorting to Java code.
Tag libraries, which also are called taglibs, consist of tags that are placed on a JSP page in a format similar to HTML tags. JSTL’s tag library offers functionality common to most web applications. There are tags for loops and conditionals, tags to read Extensible Markup Language (XML) documents and database results using Structured Query Language (SQL), tags for accessing JavaBeans, and support for other tag libraries. JSTL consists of two complementary components:
· A set of five custom JSP tag libraries that provide the core functionality required in web applications
· The JSTL Expression Language, which makes it possible to access data at runtime without using JSP expressions or Java statements
There’s also a separate version of each JSTL library that works with Java expressions using existing JSP syntax. Listing 21.12 contains a demonstration of JSTL and the Expression Language: hello.jsp, a JSP page that uses a parameter called name as part of a greeting.
LISTING 21.12 The full text of hello.jsp
The hello.jsp page looks for a parameter called name specified in the address (URL) of the page. For example, the URL http://example.com/hello.jsp?name=Sailor gives name the value Sailor and causes the page to display the response “Hello, Sailor.” When the name parameter is omitted, “Hello, Stranger” displays instead. The first JSP code in the page is the following directive: <%@ taglib
uri=’http://java.sun.com/jsp/jstl/core ‘ prefix=’c’ %>
Like other tag libraries, each JSTL library must be made available using a directive on the page before any of its tags can be employed. The preceding directive makes the core library available on the page. All tags from this library will be prefaced with the text “c:” followed by the tag’s name, as in this example:
<c:out value=’${param.name}’/>
JSTL tags are called actions, in recognition of the fact that they generate dynamic web content. Like XML elements, actions can stand alone (<tagname/>) or be paired (<tagname>…</
tagname>).The c:out action displays the contents of the local or instance variable indicated by its value attribute. The variable is specified using JSTL’s Expression Language, a simple data-access syntax that borrows from ECMAScript (JavaScript) and XPath. Statements that use the language are contained 