The Role of Tags in JSP

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 1.  Understanding JSP Custom Tags


JSP files that use scriptlet code are a mix of HTML tags and Java source code. One of the reasons JSTL and other custom tag libraries were introduced was to provide more consistency in JSP files. HTML is tag-based, whereas the scriptlet code appears as regular Java code. Tag-based programming allows the JSP page to remain completely tag-based. Further, these custom tag libraries have the ability to hide much of the Java code behind tags that can be inserted directly into the HTML. The result of using JSTL in JSP code is cleaner files that are much easier to modify and extend.

To illustrate the difference between the various programming technologies, let's look at the implementation of a simple example the process of counting from 1 to 10 in each of these technologies. We begin with an HTML implementation.

Using HTML

HTML is static and cannot change. The look of a page is predefined before the user ever sees it. Listing 1.1 shows how you would count to 10 using just a regular HTML page. Here, you can see that counting to 10 is nothing more than formatting the numbers 1 through 10.

Listing 1.1 HTML Counts to 10
<html>   <head>     <title>Count to 10 Example(using HTML)</title>   </head>   <body>1   <br />   2   <br />   3   <br />   4   <br />   5   <br />   6   <br />   7   <br />   8   <br />   9   <br />   10   <br />   </body> </html> 

Using regular HTML offers a couple of advantages. First, regular HTML is easy to generate. Many GUI tools are available that can be used to generate HTML code. Second, HTML pages can be displayed quickly. The main drawback to HTML pages is that they are static. HTML pages cannot change their appearance as the user works with them. For example, you could not build a shopping cart in HTML only, because you have no way to change the appearance to reflect new items being added.

Using JSP Scriptlets

Now, let's see how the HTML page can be made more dynamic by using JSP scriptlet code. Listing 1.2 shows a page that uses scriptlet code intermixed with HTML. This program also counts to 10.

Listing 1.2 A Scriptlet Counts to 10
<html>   <head>     <title>Count to 10 Example(using JSP Scriptlet)</title>   </head>   <body>     <%     for(int i=1;i<=10;i++)     {     %><%=i%>     <br />     <%     }     %>   </body> </html> 

The scriptlet code has several advantages over HTML. Because scriptlet code is actual Java code, the output from the scriptlet page can be generated as needed. This allows you to create dynamic Web pages that change as the user works with the page. For example, a shopping cart application might use scriptlet code to display the contents of the shopping cart. The primary disadvantage to scriptlet code is that it is not tag- based and does not flow well with the HTML.

Programming in Tags

As you can see, the scriptlet example combines HTML tags with Java source code. Tag programming attempts to combine these two approaches by constructing the scriptlet portion of the file with programmatic tags.

Java is not the only language to try to accomplish Web programming using only tags. Macromedia's ColdFusion allows interactive Web applications to be programmed with tags. Listing 1.3 shows how you would use ColdFusion to count to 10. As you can see, the loop is accomplished by using a special tag, called cfoutput, to perform the actual loop.

NOTE

JSTL source code more closely resembles ColdFusion code than the scriptlet code that most JSP programmers are used to.


Listing 1.3 ColdFusion Counts to 10
<html>   <head>     <title>Count to 10 Example(using ColdFusion)</title>   </head>   <body>     <cfloop from="1" to="10" index="i">       <cfoutput>#i#       <br />       </cfoutput>     </cfloop>   </body> </html> 

JSTL-based code resembles ColdFusion code; in both, tags are used for every programming construct, from loops to output. Listing 1.4 shows a JSTL-based page that will count from 1 to 10. As you can see, it resembles the ColdFusion code much more closely than the scriptlet code.

Listing 1.4 JSTL Counts to 10
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html>   <head>     <title>Count to 10 Example(using JSTL)</title>   </head>   <body>     <c:forEach var="i" begin="1" end="10" step="1">       <c:out value="${i}" />       <br />     </c:forEach>   </body> </html> 

Although JSTL is based on Java, JSTL source code does not resemble Java code. You don't necessarily have to decide between scriptlets and JSTL; a single JSP page can intermix both scriptlets and JSTL code. In Chapter 11, "Creating Your Own Tag Libraries," you learn how to create your own tags and use them with both JSTL and scriptlet code.

Another important feature of JSTL is its handling of expressions. JSTL includes an expression language known simply as Expression Language, or EL. In Listing 1.4, EL is being used to display the value of the variable i each time through the loop, as seen in the following line:

<c:out value="${i}" /> 

The EL expression language is always designated by using the "${}" notation. The EL expression that is contained between the curly braces will be evaluated. We cover EL in depth in Chapter 4, "Using the Expression Language." For now, it is enough to know that this line simply prints out the value contained in the variable named i.

Because JSTL tags are processed by the Web server, if the user wanted to view the source of Listing 1.2, 1.3, or 1.4, the output would look very similar to Listing 1.1. This is because the tags are processed at the server, and only the resulting HTML is transmitted. The exact output produced by Listing 1.4 is as follows:

<html>   <head>     <title>Count to 10 Example(using JSTL)</title>   </head>   <body>       1       <br />       2       <br />       3       <br />       4       <br />       5       <br />       6       <br />       7       <br />       8       <br />       9       <br />       10       <br />   </body> </html> 

This output contains blank lines between each of the numbers a side effect of putting the <br/> tag on a different line than the <c:out> tag. Because you do not see the actual HTML output as you work on your JSP pages, it is easy to place extra space into the HTML output that you would probably not have included if you had "hand-crafted" the HTML file. Generally, this does not affect the file size and is ignored. It is more important to have easy-to-read, and therefore easy-to-maintain, JSP pages than it is to make sure that you squeeze everything onto just a few lines.

In summary, Sun introduced JSTL to simplify the structure of JSP pages. JSTL improves the appearance of JSP by allowing tag-based code to be used with the HTML. A tag-based programming language fits better with the tag-based HTML.

Using JSTL has its disadvantages, too. The tags provided in JSTL do not allow you to do everything that scriptlet-based JSTL is capable of. Also, some programmers who are more familiar with a procedural programming language may find tags cumbersome when compared to the Java-based code that they are used to.

Another drawback to using JSTL is that additional overhead is introduced. All custom tag libraries, JSTL included, cause the Web server (or other servlet container) to generate additional code to interface to the tag library. In extreme cases when using a considerable amount of tag logic, it is even possible to exceed the maximum Java class size. This extra overhead can slow down your Web application.

Overall, the benefits of JSTL outweigh the drawbacks. Although JSTL is not capable of doing everything that scriptlet-based JSP is capable of, such complex code is often better isolated in an object, away from the presentation-oriented code that should reside in a JSP page.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch01lev1sec1]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net