Introducing Java Server Pages
Home > Build
> Backend > Java Server
Pages
CHAPTER
7- Introducing Java Server Pages
(order
book from Amazon)
(1)
(2) (3)
(4)
Based on servlet
technology, and currently shaping up at breakneck speed, JavaServer
Pages (JSP) is set to be one of the most important elements of Java
server programming. It's by no means complete yet, but that will change
as the Java 2 Enterprise Edition comes together.
So
what are JavaServer Pages? Well, they combine markup (whether HTML
or XML) with nuggets of Java code to produce a dynamic web page. Each
page is automatically compiled to a servlet by the JSP engine, the
first time it is requested, and then executed. JSP provides a variety
of ways to talk to Java classes, servlets, applets and the web server.
With it, you can split the functionality of your web applications
into components with well-defined public interfaces glued together
by a simple page.
This
model allows tasks to be subdivided - a developer builds custom components
and the page designer assembles the application with a few judicious
method calls. In this 'application assembly' model, the business logic
is separated from the presentation of data. To
give you an idea of the future, this separation of logic and presentation
may become yet more extreme with the use of custom tags slated for
JSP 1.1.
JavaServer Pages
is a specification that is already implemented by several web servers
(for more details, see the JSP FAQ at http://www.esperanto.org.nz/jsp/jspfaq.html),
on which your code will run without change, making it more portable
and the server market more competitive than its rivals. Finally, it's
nice and simple!
In
this chapter, we will:
Ø
Discuss the JavaServer Pages (JSP) architecture
Ø
Look at the elements of a JSP file, and the
tags used to represent them
Ø
Encapsulate logic in a JavaBean component
and integrate it with JSP
Ø
Walk through a detailed example using JSP,
showing a typical web application architecture
Architectural
Overview
A JavaServer Page
is a simple text file consisting of HTML or XML content along with
JSP elements (a sort of shorthand for Java code). When a client requests
a JSP page of the web server and it has not been run before, the page
is first passed to a JSP engine which compiles the page to a servlet,
runs it and returns the resulting content to the client. Thereafter,
the web server's servlet engine will run the compiled page.
It
should be no surprise, given the flexibility of the servlet model,
that the current reference implementation of the JSP engine is itself
a servlet.
It is possible
to view the finished servlet code that is generated by locating it
within the directory structure of the servlet engine. For example,
with JRun, you can find the source code for your JSP files (in servlet
form) in the jrun/jsm-default/services/jse/servlets/jsp
directory. This is very helpful when trying to debug your JSP files.
If
you take a look in the source files for the javax.servlet.jsp package, you'll find the following
classes:
Ø
JSPPage
Ø
HttpJspPage
They
define the interface for the compiled JSP page - namely that it must
have three methods. Not surprisingly they are:
Ø
jspInit()
Ø
jspDestroy()
Ø
_jspService(HttpServletRequest
request, HttpServletResponse response)
The
first two methods can be defined by the JSP author (we'll see how
in a moment), but the third is the compiled version of the JSP page,
and its creation is the responsibility of the JSP engine.