JSP - Page 19
November 11, 2002
JavaServer Pages (JSP) are
written in Java, which (unlike VBScript and PHP) is an object-oriented programming
language that can be used to build enterprise-strength applications. Java
is arguably the most powerful platform for server-side web development today.
Portability, multithreading, extensive class libraries, object-oriented code,
strong safety features, robust security measures, elegance, and extensibility
are just a few of Java's advantages.
Java was designed to be platform-independent and very portable. Therefore,
a web application developed in Java can be packaged as a WAR (web application
archive) file and installed on any Java-enabled application server, on any
platform.
The disadvantage is that Java is not very easy to learn. If you just need
to get a more simple site up and working quickly, and are not serious about
learning an object-oriented language, a simpler language like PHP might be
a better choice. By using JavaBeans and tag libraries, however, web designers
can quickly learn to create JSPs that retrieve data from a database, process
XML, and carry out other powerful functions, without having to know anything
about the underlying technology. You don't need to be a sophisticated Java
programmer to utilize the power of JSP.
JSP files can also be a little slower than ASP or PHP because of the way
they work. The first time JSPs are called, they are converted into servlets
(special Java classes that produce outputs for sending over HTTP), which are
stored by the JSP engine. After this, requests for the JSP files are served
from the converted servlet (JSP engines also double as servlet engines). The
most readily available JSP engine is Tomcat, which also serves static content
at a slower rate. If you want a fast web site, get a dedicated web server
such as Apache or IIS to serve the static content, while Tomcat (or another
engine) serves the JSP content.
Advantages
-
It's free. You can download the Tomcat application server from
the Jakarta web site, and start coding in minutes (assuming you've got Java
installed, which is also free).
-
It's cross-platform.
-
It has a very active user community.
-
It's extremely powerful and scalable.
Drawbacks
-
A steep learning curve.
-
Third-party hosting isn't common, and can cost extra for installation.
XML Support
There is enormous XML support in Java. There are loads of parsers and XSLT
and XPath processors available, and most of them are open source. To name
them all would be pointless, but the following is a short overview of some
of the products available (the list includes the most popular products in
each category).
XML Parsers
-
Xerces: Xerces is a high-performance,
fully compliant XML parser from the Apache XML Project. It is a fully conforming
XML Schema processor. It is free, and available both in sourcecode and precompiled
binary (JAR file) form. For more information, visit http://xml.apache.org/xerces2-j/index.html.
-
XML4J: XML Parser for Java is a validating
XML parser and processor written in 100% pure Java – a library for parsing
and generating XML documents, available as freeware from IBM. For more information,
visit http://www.alphaworks.ibm.com/tech/xml4j.
-
XP: XP is an XML 1.0 parser written in
Java, fully conforming, which detects all non well-formed documents. It is
currently not a validating XSLT processor, but it can parse all external entities:
external DTD subsets, external parameter entities, and external general entities.
For more information, visit http://www.jclark.com/xml/xp/.
-
MXP1: MXPl, or Maximum Perf. Minimum Size
XML Parser, is a Java-based, non-validating pull
parser that implements the Common Application Programming Interface (API)
for XML Pull Parsing (http://www.xmlpull.org)
specification. MXP1 was designed for minimal footprint (less than 20k) and
maximum speed (it claims up to 20% better performance than the nearest competitor)
and is suited for fast serialization and deserialization of Simple Object
Access Protocol (SOAP)-based XML objects. For more information, visit http://www.extreme.indiana.edu/xgws/xsoap/xpp/mxp1/.
XSLT Processors
-
Xalan: Xalan is an XSLT processor for
transforming XML documents, from the Apache XML Project. It implements the
W3C Recommendations for XSL Transformations (XSLT) and the XML Path Language
(XPath). It can be used from the command line, in an applet or a servlet,
or as a module in other programs. For more information, visit http://xml.apache.org/xalan-j/index.html.
-
XT: XT is a fast, free implementation
of XSLT in Java. For more information, visit http://www.blnz.com/xt/index.html.
-
SAXON: The SAXON package is a collection
of tools for processing XML documents. It contains an XSLT processor, and
Java libraries for access to the processor from Java applications. For more
information, visit http://saxon.sourceforge.net/.
Java-Specific Document Object Models
These are frameworks that provide a more Java-centric coding approach to
parsing, transforming, etc. than the DOM and SAX interfaces. They can be configured
to use DOM or SAX for parsing, but provide a much more convenient API for
Java programs.
-
JDOM: JDOM is, quite simply, a Java representation
of an XML document. JDOM provides a way to represent that document for easy
and efficient reading, manipulation, and writing. It has a straightforward
API, is lightweight, fast, and is optimized for the Java programmer. It's
an alternative to DOM and SAX, although it integrates well with both of them.
For more information, visit http://www.jdom.org.
-
dom4j: dom4j is an easy to use, open source
library for working with XML, XPath, and XSLT on the Java platform using the
Java Collections Framework and with full support for DOM, SAX, JAXP, TrAX,
and XSLT. dom4j is distributed under an open source,
Apache-style license that does not restrict users to creation of open source
products only. For more information, visit http://www.dom4j.org.
Other
-
Java XML Pack: The Java XML Pack is an
all-in-one download of Java technologies for XML from SUN. Java XML Pack brings
together several of the key industry standards for XML – such as SAX,
DOM, XSLT, SOAP, UDDI, ebXML, and WSDL – into one convenient download, thereby
giving developers the technologies needed to get started with web applications
and Web Services. Included in the bundle are: Java API for XML Processing
(JAXP), Java Architecture for XML Binding (JAXB), Java API for XML Messaging
(JAXM), Java API for XML-based RPC (JAX-RPC), and Java API for XML Registries
(JAXR). For more information, visit http://java.sun.com/xml/javaxmlpack.html.
-
Cocoon: Cocoon is an XML framework that
allows easy integrated usage of XML and XSLT technologies for server applications,
around pipelined SAX processing, with a centralized configuration system to
make things simple. It is available for usage under the Apache Software License.
For more information, visit http://xml.apache.org/cocoon/index.html.
There are also lots of utility packages available, such as XML tag libraries
(XTags) for JSP from Jakarta, which give web designers with limited Java knowledge
the full powers of XML processing through simple tags. For more information,
visit http://jakarta.apache.org/taglibs/doc/xtags-doc/intro.html.
Server Used for Examples
For the examples in this chapter, we used Tomcat 4.0.4
on Windows 2000 Professional. Tomcat is available from http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.4/bin/
(sourcecode is also available from http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.4/src/
if you want to compile Tomcat yourself).
As well as Tomcat, we also used XTags,
nightly builds of which are available from http://jakarta.apache.org/builds/jakarta-taglibs/nightly/projects/xtags/
(this is a work in progress), and dom4j, available from http://dom4j.org/download.html.
The JAR files for these two resources are provided in the code download.
Overview
The following table summarizes these three server-side languages:
| |
ASP
|
PHP
|
JSP
|
| Language
|
VBScript, JavaScript (amongst
others)
|
PHP
|
Java
|
| Platforms
|
Windows (other platforms need
third-party porting software).
|
Any platform for which the sourcecode
or binaries are available, which is most.
|
Any platform for which the sourcecode
or binaries for a JSP/servlet engine such as Tomcat are available, which
is any with Java.
|
| Web Servers
|
Microsoft IIS (other servers
need third party software).
|
Apache, IIS, Netscape, etc.
|
JSP files are served by a JSP/servlet
engine (such as Tomcat). Any web server, including Apache, IIS, and
Netscape, can be configured to send requests for JSP files to the JSP
engine. Any J2EE-compliant application server should have a JSP/servlet
engine.
|
| Portability
|
Poor
|
Excellent
|
Excellent
|
| Scalability
|
Good
|
Poor
|
Good
|
| Component Support
|
COM objects
|
None
|
Java classes, JavaBeans, Enterprise
JavaBeans
|
| Learning curve
|
Low
|
Medium
|
High
|
The only major vendor for ASP is Microsoft
(Sun market an opensource version of ASP called Sun ONE Active Server Pages
– formerly known as Chili!Soft ASP.) We won't go
into this here – see http://wwws.sun.com/software/chilisoft/
for more details). PHP is open source, so there is no vendor to deal with.
JSP is a set of standards and interfaces that can be implemented by anyone
interested. Sun provides a reference implementation of a Java application
server, which uses Tomcat as the JSP engine, but there is currently a variety
of implementations (both commercial and open source) on the market.
PHP - Page 18
Practical XML for the Web
Basic Techniques - Page 20
|