Why Use JSP?

In the early days of the Web, the Common Gateway Interface (CGI) was the only tool for developing dynamic web content. However, CGI is not an efficient solution. For every request that comes in, the web server has to create a new operating system process, load an interpreter and a script, execute the script, and then tear it all down again. This is very taxing for the server and doesn’t scale well when the amount of traffic increases.

Numerous CGI alternatives and enhancements, such as FastCGI, mod_ perl from Apache, NSAPI from Netscape, ISAPI from Microsoft, and Java Servlets from Sun Microsystems, have been created over the years. While these solutions offer better performance and scalability, all of these technologies suffer from a common problem: they generate web pages by embedding HTML directly in programming language code. This pushes the creation of dynamic web pages exclusively into the realm of programmers. JavaServer Pages, however, changes all that.

Embedding Elements in HTML Pages

JSP tackles the problem from the other direction. Instead of embedding HTML in programming code, JSP lets you embed specialized code (sometimes called scripting code) into HTML pages. Java is the default scripting language of JSP, but the JSP specification allows for other languages as well, such as JavaScript, Perl, and VBScript. We will begin looking at all the JSP elements in detail later, but at this point let’s introduce you to a simple JSP page:

<html>
  <body bgcolor="white">

  <% java.util.Date clock = new java.util.Date( ); %>
  <% if (clock.getHours( ) < 12) { %>
    <h1>Good morning!</h1>
  <% } else if (clock.getHours( ) < 18) { %>
    <h1>Good day!</h1>
  <% } else { %>
    <h1>Good evening!</h1>
  <% } %>
  Welcome to our site, open 24 hours a day.
  </body>
</html>

This page inserts a different message to the user based on the time of day: “Good morning!” if the local time is before 12:00 P.M., “Good day!” if between 12:00 P.M. and 6:00 P.M., and “Good evening!” if after 6:00 P.M. When a user asks for this page, the JSP-enabled web server executes all the highlighted Java code and creates a static page that is sent back to the user’s browser. For example, if the current time is 8:53 P.M., the resulting page sent from the server to the browser looks like this:

<html>
  <body bgcolor="white">
<h1>Good evening!</h1>
  Welcome to our site, open 24 hours a day.
  </body>
</html>

A screen shot of this result is shown in Figure 1.2. If you’re not a programmer, don’t worry if you didn’t pick up what happened here. Everything will become clear as you progress through this book.

The output of a simple JSP page

Figure 1-2. The output of a simple JSP page

Of course, embedding too much code in a web page is no better than programming too many HTML tags in server-side code. Fortunately, JSP servers provide a number of reusable action elements that perform common tasks, as we’ll see starting in Chapter 3. These action elements look similar to HTML elements, but behind the scenes they are componentized Java programs that the server executes when the page is requested by a user. Action elements are a powerful feature of JSP, as they give web page authors the ability to perform complex tasks without having to do any programming.

In addition to the standard action elements, in-house programmers and third parties can develop custom action elements (known as custom actions or custom tags, and packaged in custom tag libraries) that web page authors can use to handle even more complex and specialized tasks. This book includes a large set of custom actions for conditional processing, database access, internationalization, and more. Custom tag libraries are also available from various open source organizations and commercial companies.

Using the Right Person for Each Task

As I alluded to earlier, JSP allows you to separate the markup language code, such as HTML, from the programming language code used to process user input, access databases, and perform other application tasks. One way this separation takes place is through the use of the JSP standard and custom action elements: the elements are implemented with programming code and used the same way as page markup elements in regular web pages. Another way is to combine JSP with other Java Enterprise technologies. For example, Java servlets can handle input processing, Enterprise JavaBeans (EJB) can take care of the application logic, and JSP pages can provide the user interface.

This separation means that with JSP, a typical business can divide its efforts among two camps that excel in their own areas of expertise, and comprise a JSP web development team with programmers who create the actions for the logic needed by the application, and page authors who craft the specifics of the interface and use the complex actions without having to do any programming. I’ll talk more about this benefit as we move through the book, although I should reiterate that the first half of the book is devoted more to those without programming experience, while the second half is for programmers who wish to use the JSP libraries to create their own actions.

Precompilation

Another benefit that is important to mention is that a JSP page is always compiled before it’s processed by the server. Remember that older technologies such as CGI/Perl require the server to load an interpreter and the target script each time the page is requested. JSP gets around this problem by compiling each JSP page into executable code the first time it is requested, and invoking the resulting code directly on all subsequent requests. When coupled with a persistent Java virtual machine on a JSP-enabled web server, this allows the server to handle JSP pages much faster.

Integration with Enterprise Java APIs

Finally, because JavaServer Pages is built on top of the Java Servlets API, JSP has access to all of the powerful Enterprise Java APIs, including:

  • JDBC

  • Remote Method Invocation (RMI) and OMG CORBA support

  • JNDI ( Java Naming and Directory Interface)

  • Enterprise JavaBeans (EJB)

  • JMS ( Java Message Service)

  • JTA ( Java Transaction API)

This means that you can easily integrate JavaServer Pages with your existing Java Enterprise solutions, or take advantage of many aspects of enterprise computing if you’re starting from scratch.

Other Solutions

At this point, let’s digress and look at some other solutions for dynamic web content. Some of these solutions are similar to JSP, while others are descendants of older technologies. Many do not have the unique combination of features and portability offered by JavaServer Pages.

Active Server Pages (ASP)

Microsoft’s Active Server Pages (ASP) is a popular technology for developing dynamic web sites. Just like JSP, ASP lets a page author include scripting code, such as VBScript and JScript, in regular web pages to generate the dynamic parts. For complex code, COM (ActiveX) components written in a programming language such as C++ can be invoked by the scripting code. The standard distribution includes components for database access and more, and other components are available from third parties. When an ASP page is requested, the code in the page is executed by the server. The result is inserted into the page and the combination of the static and dynamic content is sent to the browser.

ASP+, currently in beta, will add a number of new features to ASP. As an alternative to scripting, dynamic content can be generated by HTML/XML-like elements similar to JSP action elements. For improved performance, ASP+ pages will be compiled instead of interpreted, and compiled languages such as C++, C#, and VisualBasic will be added to the current list of scripting languages that can be embedded in a page.

ASP is bundled with Microsoft’s Internet Information Server (IIS). Due to its reliance on native COM code as its component model, it’s primarily a solution for the Windows platform. Limited support for other platforms, such as the Apache web server on Unix, is available through third-party products such as Chili!Soft (Chili!Soft), InstantASP (Halcyon Software), and OpenASP (ActiveScripting.org). You can read more about ASP and ASP+ on Microsoft’s web site, http://www.microsoft.com.

PHP

PHP[1] is an open source web scripting language. Like JSP and ASP, PHP allows a page author to include scripting code in regular web pages to generate dynamic content. PHP has a C-like syntax with some features borrowed from Perl, C++, and Java. Complex code can be encapsulated in both functions and classes. A large number of predefined functions are available as part of PHP, such as accessing databases, LDAP directories, and mail servers, creating PDF documents and images, and encrypting and decrypting data. A PHP page is always interpreted by the server when it’s requested, merging the result of executing the scripts with the static text in the page, before it’s returned to the browser. The latest version is PHP 4, which uses compiled pages instead of interpreted pages to improve performance.

PHP is supported on a wide range of platforms, including all major web servers, on operating systems like Windows, Mac OS, and most Unix flavors, and with interfaces to a large number of database engines. More information about PHP is available at http://www.php.net.

ColdFusion

Allaire’s ColdFusion product is another popular alternative for generating dynamic web content. The dynamic parts of a page are generated by inserting HTML/XML-like elements, known as the ColdFusion Markup Language (CFML), into web pages. CFML includes a large set of elements for tasks like accessing databases, files, mail servers, and other web servers, as well as conditional processing elements like loops. The latest version of ColdFusion also includes elements for communication with Java servlets and Enterprise JavaBeans. Custom elements can be developed in C++ or Java to encapsulate application-specific functions, and CFML extensions are available from third parties. ColdFusion did not initially support scripting languages, but in ColdFusion 4.5, JavaScript-like code can be embedded in the web pages in addition to the CFML tags.

The ColdFusion 4.5 Enterprise Edition is supported on Windows, Solaris, HP/UX, and Linux for all major web servers and databases. For more information, visit Allaire’s web site at http://www.allaire.com.

Java servlet template engines

A Java servlet template engine is another technology for separating presentation from processing. When servlets became popular, it didn’t take long before developers realized how hard it was to maintain the presentation part when the HTML code was embedded directly in the servlet’s Java code.

As a result, a number of so-called template engines have been developed as open source products to help get HTML out of the servlets. These template engines are intended to be used together with pure code components (servlets) and use only web pages with scripting code for the presentation part. Requests are sent to a servlet that processes the request, creates objects that represent the result, and calls on a web page template to generate the HTML to be sent to the browser. The template contains scripting code similar to the alternatives described earlier. The scripting languages used by these engines are less powerful, however, because scripting is intended only for reading data objects and generating HTML code to display their values. All the other products and technologies support general-purpose languages, which can (for better or for worse) be used to include business logic in the pages.

Two popular template engines are WebMacro (http://www.webmacro.org) and FreeMarker (http://freemarker.sourceforge.net).

The JSP Advantage

JSP 1.1 combines the most important features found in the alternatives:

  • JSP supports both scripting and element-based dynamic content, and allows programmers to develop custom tag libraries to satisfy application-specific needs.

  • JSP pages are precompiled for efficient server processing.

  • JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

In addition, JSP has a couple of unique advantages that make it stand out from the crowd:

  • JSP is a specification, not a product. This means vendors can compete with different implementations, leading to better performance and quality.

  • JSP is an integral part of J2EE, a complete platform for Enterprise class applications.



[1] The precursor to PHP was a tool called Personal Home Page. Today PHP is not an acronym for anything; it’s simply the name of the product.

Get Java Server Pages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.