Java Reference
In-Depth Information
Writing a JSP Page
A JSP page consists of three kinds of elements, each with its own special markup tag
that's similar to HTML:
Scriptlets —Java statements executed when the page is loaded. Each of these state-
ments is surrounded by <% and %> tags.
n
Expressions —Java expressions that are evaluated, producing output displayed on
the page. These are surrounded by <%= and %> tags.
n
Declarations —Statements to create instance variables and handle other setup tasks
required in the presentation of the page. These are surrounded by <%! and %> tags.
n
Using Expressions
Listing 21.4 contains a JSP page that includes one expression, a call to the
java.util.Date() constructor. This constructor produces a string containing the current
time and date. Enter this file with any text editor that can save files as plain text. (The
editor you've been using to create Java source code will work for this purpose as well.)
LISTING 21.4
The Full Text of time.jsp
1: <html>
2: <head>
3: <title>Clock</title>
4: </head>
5: <body>
6: <h1 align=”Center”>
7: <%= new java.util.Date() %>
8: </h1>
9: </body>
10: </html>
After saving the file, upload it to your web server in a folder where other web pages are
stored. Unlike Java servlets, which must be in a folder that has been designated for
servlets, a JSP page can be placed in any folder that's accessible on the Web.
In Tomcat 5.5, you can place the page in any folder inside the webapps folder. If you
stored the page in webapps\jsp-examples , it would be available at /jsp-examples/
time.jsp , as in http://localhost:8080/jsp-examples/time.jsp.
When you load the page's URL for the first time with a web browser, the web server
compiles the page into a servlet automatically. This causes the page to load slowly for
the first time, but subsequent requests run much more quickly.
 
Search WWH ::




Custom Search