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.)
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 When a page includes an expression, it’s evaluated to produce a value and displayed on the page. If the expression produces different values each time the page is displayed, as time.jsp does in line 7 of Listing 21.4, this is reflected in the page when loaded in a web browser. There are several servlet objects you can refer to in expressions and other elements of a JSP page using the following variable names:
· out—The servlet output stream
· request—The HTTP servlet request
· response—The HTTP servlet responses
· session—The current HTTP session
· application—The servlet context used to communicate with the web server
· config—The servlet configuration object used to see how the servlet was initialized
Using these variables, you can call the same methods from within a page that are available in a servlet. Listing 21.5 contains the text of the next page you’ll create, environment.jsp, which shows how the request variable can be used on a page. This variable represents an object of the HttpServletRequest class, and you can call the object’s getHeader(String) method to retrieve HTTP headers that describe the request in more detail.
In lines 7–15 of the environment.jsp page, each line contains a call to getHeader() that retrieves a different HTTP request header. Figure 21.5 shows an example of the output. The values reported for each header depend on your web server and the web browser you’re using, so you won’t see the same values for User-Agent, Referer, and other headers.