[ Team LiB ] Previous Section Next Section

Displaying a Value with <%= %>

The earlier example in Listing 1.4 could have been written in a much more compact way that determined the time of day first and printed out its greeting near the end of the file. Listing 1.6 shows a much more compact version.

Listing 1.6 Source Code for Greeting1.jsp
<html>
<body>
<%
    java.util.Calendar currTime = new java.util.GregorianCalendar();

    String timeOfDay = "";

    if (currTime.get(currTime.HOUR_OF_DAY) < 12)
    {
        timeOfDay = "Morning!";
    }
    else if (currTime.get(currTime.HOUR_OF_DAY) < 18)
    {
        timeOfDay = "Afternoon!";
    }
    else
    {
        timeOfDay = "Evening!";
    }
%>
Good <% out.write(timeOfDay); %>
</body>
</html>

As you can see, Listing 1.6 is much easier to read because it doesn't jump back and forth between Java and HTML so rapidly. Down at the bottom of the file, you can see where the timeOfDay variable is written out as part of the display. Notice that even though it is a single statement on the same line as some HTML text, out.write(timeOfDay) must still end with a semicolon because it must be a legal Java statement.

JSP provides a shorthand for printing out variables to save you from having to put out.write() all over the place. The line that prints out the greeting in Listing 1.6 can be replaced with the following line:


Good <%= timeOfDay %>

The <%= tag indicates that you want to write out a Java expression as part of the output sent back to the browser. Notice that you still close the tag with %> and not =%>. You can include any Java expression. Just make sure you don't put a semicolon after the expression—the semicolon is automatically generated for you. The following line shows you a more complex expression:


<%= 2*3+4-5*6+7*8-9 %>

As you can see, it's just as valid to print out a numerical expression as it is to print out a string. In fact, you can also print out an entire object. For example, you can use the following line to print out the string representation of a calendar object:


<%= new java.util.GregorianCalendar() %>

Of course, the result of printing out an object might not be what you expect. Figure 1.2 shows how the GregorianCalendar object looks when the <%= tag is used to display it.

Figure 1.2. The <%= tag can print out an entire object.

graphics/01fig02.gif

<%= Is Not <% =

graphics/watchout_icon.gif

Make sure you don't put any space between the <% and the = sign.


When you use the <%= tag to display an object, the result is the same kind of output you would see if you wrote out the object to System.out in a regular program. Basically, the output routine calls the toString method in the object and displays the results of the method call.

It's All out.write

graphics/bytheway_icon.gif

Anything you put between the <%= and %> ends up inside an out.write() expression. In other words, think of <%= as shorthand for out.write( and its closing %> as shorthand for );.


Listing 1.7 shows a somewhat more practical combination of <% and <%= used to display the current date.

Listing 1.7 Source Code for ShowDate.jsp
<html>
<body>
<% java.util.Calendar currDate = new java.util.GregorianCalendar();
   // add 1 to month because Calendar's months start at 0, not 1
   int month = currDate.get(currDate.MONTH)+1;
   int day = currDate.get(currDate.DAY_OF_MONTH);
   int year = currDate.get(currDate.YEAR);
%>
The current date is: <%= month %>/<%= day %>/<%= year %>
</body>
</html>

The <%= tag is often useful when graphic designers are creating the Web page and Web programmers are adding the server-side Java code. Typically, the Web programmers encase their code in <% %> tags somewhere near the beginning of the JSP file. They put all the interesting data (the data interesting to the graphic designers) into Java variables. Then the graphic designers can lay out the page the way they want it and use the <%= %> tags to insert the data where they want without having to write any Java code. Listing 1.7 is a basic example of this very concept. The graphic designers can insert the month, day, and year variables anywhere they want.

JavaServer Pages make it possible to separate the roles of authors who develop the look and feel of the content, and developers who create reusable software components. As you will see later in this book, there are additional ways that JSPs help to make it easier to develop Web pages when different groups must work on the same JSP.

    [ Team LiB ] Previous Section Next Section