|[ Team LiB ]|
A "Hello World" JavaServer Page
JavaServer Pages (JSPs) are, in the most basic sense, Web pages with embedded Java code. The embedded Java code is executed on the server before the page is returned to the browser. If a picture is worth a thousand words, in this case an example is worth a thousand explanations. Listing 1.1 shows a basic JSP.
<html> <body> <% out.println("<h1>Hello World!</h1>"); %> </body> </html>
As you can see, the page is composed of standard HTML with a dash of Java contained between the <% and %> character sequences. The <% and %> along with the code inside is called a scriptlet.
Although you can probably guess what the browser will display when this page runs, Figure 1.1 should remove any doubt because it shows the output from this page.
Trying Out the Example
In Case of Trouble
When the browser asks the Web server for a JSP, the Web server passes control to a JSP container. A container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to understand the special elements that are part of JSPs. Because this is the first time this JSP has been invoked, the JSP container converts it into an executable unit called a servlet. The entire page, including the parts that are in HTML, is translated into source code. After the code has been translated, the JSP container compiles the servlet, loads it automatically, and then executes it. Typically, the JSP container checks to see whether a servlet for a JSP file already exists and whether the modification date on the JSP is older than the servlet. If the JSP is older than its generated servlet, the JSP container assumes that the JSP hasn't changed and that the generated servlet still matches the JSP's contents. Because it takes some time to generate a servlet and compile it, the JSP container wants to minimize the number of compiles it has to perform, so it tries to avoid unnecessary compiles. We will discuss servlets in some more detail later in the book.
The other interesting piece of this equation is the actual HTML code that was sent to the browser. Most browsers enable you to view the HTML source for the Web page you are viewing. Listing 1.2 shows you the HTML code generated by the JSP in Listing 1.1.
<html> <body> <h1>Hello World!</h1> </body> </html>
As you can see, the text from the out.println statement in the JSP file is inserted directly into the HTML output. Of course, printing out a text string is not a practical use for JSP. Typically, you want to print out things that might change every time you run the JSP. The current time and date are trivial examples of items more suited for JSP.
The out object used to write the "Hello World" string is an object of type JspWriter. For now, you just need to know that this out object has approximately the same methods as the standard Java class PrintWriter. There are print and println methods for writing out various types of data. One word of caution, however: JspWriter is not a PrintWriter, so you cannot pass the out object to methods that expect a PrintWriter. You'll learn more about the JspWriter object in Hour 8, "Core JavaServer Page Components."
As mentioned earlier, when the browser asks the Web server for HelloWorld.jsp, the JSP container first checks to see whether HelloWorld.jsp has already been compiled into a servlet. If not, it creates a servlet that prints out the HTML in your JSP file and executes the code contained within the JSP file. Listing 1.3 shows the portion of the generated servlet that produces the HTML code in Listing 1.1.
Listing 1.3 Java Code to Produce the HTML from Listing 1.2
out.write("<html>\r\n"); out.write("<body>\r\n"); out.println("<h1>Hello World!</h1>"); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n");
Typically, you won't need to look at the generated servlet; however, sometimes it is useful. If your JSP throws an exception and you print out the Java stack trace from the exception, the line numbers displayed in the stack trace will be line numbers from the generated servlet. There is no standard indicating where the generated servlets must be placed. It varies from JSP container to JSP container. In Tomcat you will find the generated source code and the servlet binary class files under the work subdirectory of your Tomcat installation directory. In the current release of Tomcat 5.0, they are usually in the work/Catalina/localhost/_/org/apache/jsp subdirectory.
What's a Stack Trace?
|[ Team LiB ]|