站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

Chapter 2. Simple JSPs

Chapter 1 presented the case for dynamic sites and surveyed a number of techniques for building such sites, focusing on the strengths of JavaServer Pages technology. With these preliminaries out of the way, everything is in place to start creating some pages! This chapter begins by introducing some of the simpler features of JSPs.

It is a time-honored tradition for computer books to start with an example that allows the system being studied to introduce itself. This book is no exception, so without further delay, Listing 2.1 contains our first JSP.

Listing 2.1 A simple JSP
<html>
<!-- Our first JavaServer Page -->
<body>
Hello, world!
</body>
</html>

This may look like a plain old chunk of HTML, not a very interesting one at that. However, when saved in a file called index.jsp and given to a JSP engine such as Tomcat, this chunk of HTML becomes much more than a static block. In fact, this is a program very similar to the programs in Listings 1.2 and 1.3.

As a program, this file contains a series of instructions that the JSP engine will follow. Written out in English, these instructions are equivalent to the following:[1]

[1] This isn't quite true. For the sake of efficiency, most JSP engines send out contiguous chunks of HTML all at once. However, it is often helpful to think of JSPs as being processed one line at a time.

  1. Send the text <html> to the user.

  2. Send the text <!-- Our first JavaServer page --> to the user.

  3. Send the text <body> to the user.

  4. Send the text Hello, world! to the user.

  5. Send the text </body> to the user.

  6. Send the text </html> to the user.

More technically, a program called the page compiler converts the original file into another little Java program, a servlet as discussed in Section 1.3. This servlet is what gets run. Servlets are an important technology, and they are covered in more detail in Chapter 11. For the time being, these details are unimportant, and it is perfectly reasonable to think of the JSP file itself as the program.

At this point, three different things are all going by the name index.jsp. One is the original file, sitting in a directory from where it can be edited like any other file. Second is the servlet, which is managed by the JSP engine and is generally not meant to be seen directly. Third is the URL and the corresponding page as seen in a browser. To avoid confusion, the specific meaning will always be indicated when it is not clear from the context.

In this particular case, all the extra work of creating and running a program has not accomplished anything. However, the translation has not been pointless, as it has created a program from HTML. This is why JSP authors generally do not need to do much programming themselves. The JSP engine is quite sophisticated and can turn a few simple tags into very complex code. The servlet that is generated and the environment in which this code runs are also very sophisticated, which removes even more of the programming burden.

This program illustrates two of the basic rules for the JSP programming language:

  1. Plain text turns into a command to send that text to the user.

  2. Regular HTML tags turn into a command that sends that tag to the user.

    [ directory ] Previous Section Next Section