Caffeinate Your HTML with a Shot of Java
By Michael Trachtman
Java Server Pages (JSP) is a technology that lets you comingle HTML with Java. It's a powerful tool for producing dynamic Web pages. Depending on what you're trying to accomplish, JSP can often be a better alternative to ASP or PHP. Perhaps you'd like to place the contents of a Java database query directly inside an HTML table, or use Java as a bridge between your shopping-cart software and HTML. Possibly you're writing an advanced e-commerce site, like an airline reservations system in Java, and would like to allow its capabilities to be accessed via the Web. These advanced capabilities are typically beyond what can reasonably be done with HTML or JavaScript alone. But with Sun's JSP technology you can manage this easily.
Before you get started, you should know a few facts about JSP. A JSP program produces HTML. JSP programs execute on the Web server, not in the browser. In this way, JSP is very similar to Microsoft's Active Server Pages (ASP). In fact, much of JSP technology borrows concepts from ASP.
The first example that I'll show you is a very simple program that computes and displays all odd numbers between 1 and 20. This will teach you the essentials of the JSP approach to creating Web pages. Then, I'll show you a more sophisticated example that displays jokes. The user can control which joke displays and whether or not the page shows the punch line. This is a good illustration of a dynamic pagethat is, one that can produce a different output each time it runs.