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 "1.0"?> 2 -//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 6 7 |
A Simple JSP Example 11 16 17 | 18
"big">Simple JSP Example 19 "border: 6px outset;"> 20 21 27 28
|
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.]
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover