JSPBOOK
JSPBOOK
JSPBOOK
The purpose of this book is to provide the beginning JSP (Java Server
Pages) an introduction to the topic and some more indepth understand
over and above that gained from reading the specification.
With the release of the JSP 1.0 public review, JSP has moved from re-
quiring a solid grounding in HTML, and Java Servlets to also requiring
knowledge of XML. Although we do assume a certain amount of
knowledge about XML and HTML, we do cover the Servlet interface as
it of particular importance to JSP - as all Java Server Pages are compiled
into Servlets before running.
There are many topics covered in this book, we range from a JSP history
introduction, comparing the technology with ASP, a basic introduction
to JSP pages, use of beans, and we further look into using databases as
well as the n-tier technology of EJB and CORBA.
This book effort is an outgrowth of the JSP FAQ which is currently lo-
cated at http://www.esperanto.org.nz/jsp/jspfaq.html.
MVSXVH%HDQ!-638VLQJ%HDQ
This is some text about the <jsp:useBean> tag.
XML Equivalent: <jsp:useBean> etc
This symbol indicates that this is an action you should do. It appears
when an example starts, or when you need to do something to ensure
that something happens properly further on
When you see this symbol, it is a key point. Take special note.
This idea wasn’t new - the Microsoft camp had come across the idea and
enshrouded it into Active Server Pages (ASP) which Microsoft first in-
troduced in IIS 3.0. ASP performed terribly in 3.0, but with the release
of IIS 4.0, ASP became the central focus for the development of dynam-
ic content on the server side in the Microsoft world, and most “Micro-
soft shops” focus on ASP development as it ties quite well into the
Microsoft COM model.
Other models for deploying dynamic content abound for the Internet -
the strongest contender in times past being CGI. But there are others of
note, CGI/Perl, PHP, Cold Fusion, JHTML, and of course, ASP.
JSP vs CGI
The other problem of CGI is that of state management. With the JSP/
Servlet host running all of the time, the host can store state easily and
make sure that it is provided to the JSP page in an easy, effective man-
ner. With CGI applications, if libraries are not already written or other-
wise available, they have to be written to manage all of the state
management mechanisms.
The future of JSP is not entirely clearly mapped out. As of the time of
writing, we are working with the JSP 1.0 Public Draft. This has intro-
duced a number of things (particularly XML syntax and buffered output
to help with redirection) and removed other items (particularly exten-
sion tags for HTML which has a number of developers wanting to stick
with the 0.92 specification). There is an indication in the Public Draft
that XML will become required in the 1.1 specification, and that JSP
will be tied more heavily into the Enterprise Java architecture (J2EE -
Java 2 Enterprise Edition) that Sun is working on. At present, there is
no integration with any of the Enterprise features
To run it, you need to run the STARTSERVER.BAT batch file - but this
assumes that you have the 1.2 JDK installed. If you don’t have it, then
go and get it. Make sure that the java.exe program is on your path.
@echo off
rem $Id: startup.bat,v 1.10 1999/04/22 23:48:27 akv
Exp $
rem Startup batch file for servlet runner.
rem This batch file written and tested under
Windows NT
rem Improvements to this file are welcome
if "%CLASSPATH%" == "" goto noclasspath
rem else
set _CLASSPATH=%CLASSPATH%
set
CLASSPATH=server.jar;servlet.jar;classes;examples
\WEB-
The other thing that you have to be careful about when running this
batch file under Windows 95 is to increase the space available for envi-
ronment variables. There are instructions in the README.TXT that ca-
ter for this problem.
You should be able to start the server from Explorer without having to
drop to a DOS prompt. If you are having trouble getting it going then it
is best to drop to the DOS prompt and edit the STARTSERVER.BAT
file and comment out the line that says @ECHO OFF - this will let you
see what is going on. By default, the server will start on port 8080, look-
ing something like:
when it is running.
There are a number of parameters that you can change in the configura-
tion file. The file is called DEFAULT.CFG and should look like this:
STARTSERVER -P 80
for example will start the server at port 80, overriding the
information in the default.cfg file.
• server.docbase=webpages - this line specifies which direc-
tory (off the directory that you are running the server from)
that the web pages are located. In this case, all of the web
pages that come with the server for the examples and so forth
are in the /jsp/webpages directory.
• server.workdir=work - this specifies the directory that the
JSP engine will use to compile the JSP page from a JSP to a
servlet. It also specifies the directory for storage of the classes
which are compiled from the servlet code. Be careful this
directory path does not contain values that would not be valid
Java identifiers, as JSP servers often use their path as part of
the package name. Do not, for example, make a directory
server.webapp.esperanto.mapping=/esperanto
server.webapp.esperanto.docbase=esperanto
Finally, there is one more configuration option that configures the JSP
engine rather than the web server and that is a command line flag:
-Djsp.keepgenerated=false
This option tells JSP to not keep the servlet source that was generated.
It is worthwhile to make this true as we want to actually see what is be-
ing generated by the servlet. So change the STARTSERVER.BAT file
to:
java -Djsp.keepgenerated=true
com.sun.web.shell.Startup %1 %2 %3 %4 %5 %6 %7 %8
%9
http://localhost:8080/
req.jsp?searchName=bob&wantWorksFor=true
This will cause the JSP page to be loaded with two parameters
- searchName being equal to “bob” and wantWorksFor being
“true”. This is a feature primarily of HTML Forms which we
won’t cover here (see Appendix A) as it is covered in much
better detail in many books and other resources. We assume in
this text that you have a basic grasp of HTML Forms.
• Cookies - cookies are the ability for a web server to store
some information on a clients machine, which gets presented
back to the web server again. We cover the creation of your
own cookies later on (??? where?), but take it as given that the
Java Servlet engine uses Cookies to keep track of who is who
so that we can provide “state” on the server side.
This example is going to tell us what the current time is. It is a very sim-
ple example that does not make use of any input from the client and in-
troduces one JSP tag.
/jsp/esperanto/example1/jsp1.jsp
• The package name includes the full directory path of the loca-
tion of the JSP. Remember the warning above about using
directories that are not valid Java identifiers.
• The Servlet only imports what it needs to import - by specify-
ing the full path to the Date class we got around importing
java.util.Date (or java.util.*). We will show you how you can
import packages later in the book.
• The JSP page compiler generated a servlet descended from
HttpJspBase. This is a special class which takes over the proc-
essRequest/doGet/doPost mechanism used in Servlets and
calls a single method - _jspService to process the request.
• There are a number of variables created (session, application,
config, etc) that we won’t discuss. One we will is the out
object. The out object is created for the express purpose of
sending data back down the wire to the client browser. It is a
special form of a PrintWriter (similar in use to System.out)
which actually talks to a buffered output buffer - rather than
directly to the output stream. We will talk about the signifi-
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
import com.sun.jsp.runtime.*;
import java.beans.*;
import com.sun.jsp.JspException;
• Perhaps one last key thing to note is that the servlet will throw
a new JspException if something goes wrong. We will cover
We did use a couple of tags though, so lets explain what those are:
!-636FULSOHWV
JSP allows you to embed arbitrary blocks of Java code inside your
JSP files. These blocks of Java code get added directly into the servlet
code that is to be generated. This means that you can get compile time
errors as well as runtime errors in your JSP page.
The scriptlet tags start with <% after which any valid Java code can
appear. The code can be multi line, and it ends with %>
These tags must start and end in the same file - you cannot issue an
include (see later) which may open a scriptlet and then close back in
your main code.
XML Equivalent: <jsp:scriptlet> Java Code </jsp:scriptlet>
!-63([SUHVVLRQV
JSP Expressions are a quick mechanism to avoid having to write the
full code for out.println. The <%= is a replacement for the out.println
statement and it is directly converted into that.
XML Equivalent: <jsp:expr> Java Code </jsp:expr>
Given that we now know the syntax for XML, we can give the equiva-
/jsp/esperanto/example1/jsp2.jsp
XML has been introduced into the JSP 1.0 specification (it wasn’t in
earlier versions) because Sun want to provide a standard mechanism for
tag extensibility and for page developers to be able to use the ever wid-
ening range of XML tools. It is envisioned that JSP will eventually be
able to be done with a smart, JSP aware software tool. Unfortunately,
the JSP 1.0 public draft does not come with a DTD (Document Type
Definition) which makes it difficult to code for by hand.
JSP 1.0 does specifically cater for non-XML tags primarily because JSP
is still at an early stage in its acceptance by the developer community.
Most pages require hand coding, and the use of XML tags with their
cumberson CDATA mechanisms for inserting program code mean that
it is important for the specification to enable those not using XML edi-
tor.
That said, the current JSP tags tend to confuse such editors (for exam-
ple, they change <% into <%) so you have to use non-graphical edi-
tors for JSP, like vi, emacs, HomeSite or Notepad.
The following example will enable you to add two numbers together. It
uses HTML forms
An introduction to useBean