1.2 JavaServer Pages
JSP is an extremely powerful choice for Web development. JSP is a technology using server-side scripting that is actually translated into servlets, which are compiled before they are run. This gives developers a scripting interface to create powerful Java servlets.
JSP provides tags that allow developers to perform most dynamic content operations without writing complex Java code. Advanced developers can add the full power of the Java programming language to perform advanced operations in JSP.
1.2.1 Template Pages
Clearly, the most effective way to make a page respond dynamically would be to simply modify the static page. Ideally, special sections to the page could be added that would be changed dynamically by the server. In this case, pages become more like a page template for the server to process before sending. These are no longer normal Web pages; they are now server pages.
On a server page, the client requests a Web page, the server replaces some sections of a template with new data, and sends this newly modified page to the client (see Figure 11).
Figure 11 Server Page
Since the processing occurs on the server, the client receives what appears to be static data. As far as the client is concerned there is no difference between a server page and a standard Web page. This creates a solution for dynamic pages that does not consume client resources and is completely browser-neutral.
1.2.2 Static Data vs. Dynamic Elements
Since JSP is designed around static pages, it can be composed of the same kind of static data as a standard Web page. JSP pages use HTML or XML to build the format and layout of a page. As long as a normal Web page can contain the data, so can a JSP page.
To replace sections of a page, the server needs to be able to recognize the sections it needs to change. A JSP page usually has a special set of "tags" to identify a portion of the page that should be modified by the server. JSP uses the <% tag to note the start of a JSP section and the %> tag to note the end of a JSP section. JSP will interpret anything within these tags as a special section.
JSP pages usually contain a mixture of both static data and dynamic elements. It is important to understand the distinction between the two forms. Static data is never changed in the server page, and dynamic elements will always be interpreted and replaced before reaching the client.
1.2.3 A Simple JSP Page
Often, the easiest way to understand something is to see it. Script 1.1 shows a very simple JSP page.
Don't worry too much about what the JSP page is doing; that will be covered in later chapters. It is important to notice the two types of data in the page: static data and dynamic data. Understanding the difference between these builds an essential foundation for creating JSP pages.
Script 1.1 simpleDate.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <HTML> <HEAD> <TITLE>Stitch Magazine! - A simple date example</TITLE> </HEAD> <BODY COLOR=#ffffff> <font face="Arial"> The current time is <%= new java.util.Date() %> </font> </BODY></HTML>
When the client requests this JSP page, the client will receive a document as HTML. The translation is displayed in Figure 12.
When compiled and sent to the browser, the page should look like Figure 13.
Figure 12 A Server Page into HTML Data
Figure 13 A Simple JSP Page
Essential Note: JSP Pages without JSP Tags
Since a JSP can handle the same static data as an HTML file, any HTML file can be changed to the extension .jsp. If the JSP server is running, these files will now run through the JSP engine. Without specific tags to identify dynamic sections, this document will come out exactly the same as the original HTML file, but it will take more resources because the JSP engine will attempt to parse and execute the file.
1.2.4 JavaServer Pages
Most Web servers that understand JSP will look for a specific filename extension. Typically, any filename that ends in .jsp will be interpreted and processed by the JSP engine. Often, the actual extension is configurable, but for the sake of clarity, this book will use .jsp throughout.