Page #474 (Chapter 35. JavaServer Pages)

 
[Page 1210 ( continued )]

35.2. A Simple JSP Page

JSP provides an easy way to create dynamic Web pages and simplify the task of building Web applications. A JavaServer page is like a regular HTML page with special tags, known as JSP tags , which enable the Web server to generate dynamic content. You can create a Web page with HTML script and enclose the Java code for generating dynamic content in the JSP tags. Here is an example of a simple JSP page.

  <! CurrentTime.jsp >    <html>     <head>     <title>   CurrentTime   </title>     </head>     <body>   Current time is    <%= new   java.util.Date()   %>      </body>     </html>   

The dynamic content is enclosed in the tag that begins with <%= and ends with %> . The current time is returned as a string by invoking the toString method of an object of the java.util.Date class.

To display the JSP page, you need to create a text file for the page, name the file CurrentTime.jsp, and store the file in c:\jakarta-tomcat-5.5.9\webapps\liangweb . Assume you have started Tomcat. You can run it from a Web browser using the URL http://localhost:8080/liangweb/CurrentTime.jsp , as shown in Figure 35.1.

Figure 35.1. A JSP page is displayed in a Web browser.

Note

You created the context root directory liangweb under the Tomcat webapps directory in the preceding chapter. The easiest way to run JSP for new JSP programmers is to store all the JSP files in c:\jakarta-tomcat-5.5.9\webapps\liangweb . You can also place JSP files elsewhere. For more information, see Supplement VII.E, " Tomcat Tutorial ."


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net