JSPs belong in the 'CGI space' - that part of the system
where the web server connects to the middle tier, passing in the
client request and getting back the response. JSPs are built on top
of Java servlets, which provide a Java framework for dynamic page
generation in response to an HTTP request. Servlets require
programming skills, but a JSP is simply a template for page output,
whether that's HTML, or XML, or some other file format. Segments of
Java code are used to add data dynamically to the page before it is
returned to the client.
JSPs are similar to ASPs (Active Server Pages) in more than just
name, but they use Java rather than a scripting language. If you're
familiar with ASPs, you'll find JSPs very easy to get used to, but
some people argue that JSPs are a superior technology because:
Ø Java is more powerful than
the scripting languages in ASP: it's multi-threaded, network-savvy,
and security conscious
Ø On first use, the JSP is
compiled into a Java servlet class, and after that the compiled
code is used every time the page is invoked. This makes JSPs more
efficient than ASPs that use a scripting language
We don't aim to proselytize either technology: we simply prefer
JSPs because we know Java much better than Visual Basic.
What makes up a JavaServer Page?
A JSP is an HTML or XML page that may contain JSP
elements. An HTML page with no JSP elements is also a
legitimate JSP (just change the extension from .html to .jsp) and
will be displayed normally, after processing.
In processing a page, the JSP processor leaves HTML/XML material
untouched, but acts on JSP elements. JSP elements can contain Java
code (either Java expressions or Java statements). Java
expressions are evaluated, and the value is inserted into the
HTML/XML page in their place. Java statements can control
page content: a stretch of HTML/XML material can be repeated if it
is inside a Java loop, or included conditionally if it is placed
inside a Java conditional statement.
JSP elements are delimited by JSP tags. All of these tags
have standard XML syntax, within the jsp: namespace, but some also
have a JSP-specific syntax that is different from XML.
In version 1.0, all non-XML tags have equivalent XML tags
defined in the JSP DTD, so that a JSP can be valid XML. With
version 1.1 (which is the current version at the time of writing),
JSP processors are required to accept and validate JSPs in purely
XML syntax. In effect, JSP 1.1 processors contain a validating XML
parser.
The purpose of JSP tags is to include Java code in the page, and
to perform servlet-related tasks: request processing and
forwarding, session maintenance, and communication with business
objects. Describing these tasks within elements with XML syntax can
be awkward because Java code often contains characters that have to
be escaped in XML (such as <); as a result, you have to use
CDATA sections a lot. In manually composed JSPs, it is common to
use non-XML tags, but XML tags are expected to be very useful as
JSP editors like JRun 3.0 Studio become available.
What Do They Look Like?
Here's a short example, sample.jsp. You can use standard XML
comments within a JSP document:
<html>
<head>
<title>JSP example
page</title>
<%! int i=5,j=2;
%>
<!-- a declaration -->
<%@ page
import="java.util.Date"
%> <!-- a directive
-->
</head>
<body>
<h1>The Famous JSP Hello
Program</h1>
<% String s = "GNU" + "JSP";
%>
<!-- a code fragment -->
The following line should contain
the text "Hello GNUJSP World!".
If that's not the case, start
debugging...
<p>Hello <%= s %>
World!<br>
<!-- an expression -->
The current date is <%= new
java.util.Date() %>.<br>
The integer value is <%= ++i+j
%> <!--
another expression -->
<% if(i<12){
%>
<!-- code fragment -->
<br>less than a
dozen
<!-- template data -->
<% }else
%>
<!-- code fragment -->
<br>a dozen or
more
<!-- template data -->
<% ; // end if
%>
<!-- code fragment -->
</body>
</html>
If you have this document served to you by a JSP-enabled server
(a server that has a JSP engine), this is what you will
see:

The HTML source for this page is shown below. Notice how all the
JSP elements have disappeared, but the comments have remained:
<html>
<head>
<title>JSP example
page</title>
<!-- a declaration -->
<!-- a directive -->
</head>
<body>
<h1>The Famous JSP Hello
Program</h1>
<!-- a code fragment
-->
The following line should contain
the text "Hello GNUJSP World!".
<br>If thats not the case
start debugging ...
<p>Hello GNUJSP
World!<br>
<!-- an expression -->
The current date is Fri Apr 07
08:59:07 EDT 2000.<br>
The integer value is
8
<!-- another expression -->
<!-- code fragment -->
<br>less than a
dozen
<!-- template data -->
<!-- code fragment -->
</body>
</html>
Click the Reload/Refresh button several times, and you will see
the integer value (and eventually the message that follows it)
change.