First JSP Example

We begin our introduction to JavaServer Pages with a simple example, clock.jsp (Fig. 27.1), in which the current date and time are inserted into a Web page using a JSP expression.

Figure 27.1. JSP expression inserting the date and time into a Web page.

(This item is displayed on pages 1283 - 1284 in the print version)

"http://www.w3.org/1999/xhtml"> 8 9 "refresh" content = "60" /> 10

1 6 7

A Simple JSP Example 11 16 17 18

"big">Simple JSP Example

19 "border: 6px outset;"> 20 21 27 28
"background-color: black;"> 22

"big" style = "color: cyan;"> 23 24 <%= new java.util.Date() %> 25

26
29 30

As you can see, most of clock.jsp consists of XHTML markup. [Note: We assume that the reader already knows XHTML and Cascading Style Sheets (CSS). For those who are not familiar with XHTML and CSS, we have included three chapters from our book Internet & World Wide Web How to Program, Third Edition as PDF documents on the CD that accompanies this bookIntroduction to XHTML: Part 1, Introduction to XHTML: Part 2 and Cascading Style Sheets (CSS).] In cases like this, JSPs are easier to implement than servlets. In a servlet that performs the same task as this JSP, each line of XHTML markup typically is a separate Java statement that outputs the string representing the markup as part of the response to the client. Writing code to output markup can often lead to errors. Most JSP editors provide syntax coloring to help programmers check that their markup follows proper syntax.

Software Engineering Observation 27.2

JavaServer Pages are easier to implement than servlets when the response to a client request consists primarily of markup that remains constant between requests.

 

The JSP in Fig. 27.1 generates an XHTML document that displays the current date and time. The key line in this JSP (line 24) is the expression

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

 

JSP expressions are delimited by <%= and %>. The preceding expression creates a new instance of class Date (package java.util). By default, a Date object is initialized with the current date and time. When the client requests this JSP, the preceding expression inserts the String representation of the date and time in the response to the client. [Note: Because the client of a JSP could be anywhere in the world, the JSP should return the date in the client locale's format. However, the JSP executes on the server, so the server's locale determines the String representation of the Date. In Fig. 27.10, clock2.jsp determines the client's locale, then uses a DateFormat (package java.text) object to format the date using that locale.]

Software Engineering Observation 27.3

The JSP container converts the result of every JSP expression into a string that is output as part of the response to the client.

 

We use the XHTML meta element in line 9 to set a refresh interval of 60 seconds for the document. This causes the browser to request clock.jsp every 60 seconds. For each request to clock.jsp, the JSP container reevaluates the expression in line 24, creating a new Date object with the server's current date and time.

As in Chapter 26, we use Apache Tomcat to test our JSPs in the jhtp6 Web application we created previously. For details on creating and configuring the jhtp6 Web application, review Section 26.3 and Section 26.4.1. To test clock.jsp, create a new directory called jsp in the jhtp6 subdirectory of Tomcat's webapps directory. Next, copy clock.jsp into the jsp directory. Open your Web browser and enter the following URL to test clock.jsp:

http://localhost:8080/jhtp6/jsp/clock.jsp

 

When you first invoke the JSP, you may notice a brief delay as Tomcat translates the JSP into a servlet and invokes the servlet to respond to your request. [Note: It is not necessary to create a directory named jsp in a Web application. We use this directory to separate the examples in this chapter from the servlet examples in Chapter 26.]

Категории