Servlets and JSP: An Overview

by ServerWatch Staff

Servlets are Java technology's answer to CGI programming; JSP is a technology that lets users mix regular, static HTML with dynamically-generated HTML. This tutorial discusses the basics of Java Servlets and JSP, and the advantages of using each of these technologies.

What Are Java Servlets?

For example, a weather-report or news headlines page might build the page dynamically, perhaps returning a previously built page if it is still up to date

  • The Web page uses information from corporate databases or other such sources.For example, an organization making a Web page at an online store that lists current prices and the number of items in stock would use this feature

    Next: What Are the Advantages of Servlets Over Traditional CGI?

What Are the Advantages of Servlets Over Traditional CGI?

  • Convenient. Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such tasks.

  • Powerful. Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs cannot). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations.

  • Portable. Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say iPlanet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebSTAR. Servlets are supported directly or via a plug-in on almost every major Web server.

  • Inexpensive.Various free or very inexpensive Web servers available are good for "personal" use or low-volume Web sites. But with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. But once you have a Web server, no matter the cost of that server, adding servlet support to it (if it does not come preconfigured to support servlets) is usually inexpensive or even free.

    Next: What Is JSP?

What is JSP?

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML.

Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations.

But most CGI variations, including servlets, make you generate the entire page via your program, even though most of it is always the same.

JSP lets you create the two parts separately.

Here's an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HEAD><TITLE>Welcome to Our Store</TITLE></HEAD>
<H1>Welcome to Our Store</H1>
<SMALL>Welcome back, 
To access your account settings, click
<A HREF="Account-Settings.html">here.</A></SMALL>
Regular HTML for all the rest of the on-line store's Web page.

What Are the Advantages of JSP?

  • JSP vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including externally-defined pieces into a static Web page. JSP is better because it lets you use servlets instead of a separate program to generate that dynamic part. Besides, SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.

  • JSP vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript cannot access server-side resources like databases, catalogs, pricing information, and the like.

  • JSP vs. Static HTML.Regular HTML, of course, cannot contain dynamic information. JSP is so easy and convenient that it is quite feasible to augment HTML pages that only benefit marginally by the insertion of small amounts of dynamic data. Previously, the cost of using dynamic data would preclude its use in all but the most valuable instances.
This article was originally published on Tuesday Dec 7th 1999
Mobile Site | Full Site