Displaying a Value with <%= %>

The earlier example in Listing 14.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 14.6 shows a much more compact version.

Code Listing 14.6. Source Code for Greeting2.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 14.6 is much easier to read because it doesn't jump back and forth between ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.