A Tutorial on Java Servlets and Java Server pages JSP (web).

advertisement
A Tutorial on Java Servlets and Java Server Pages (JSP)
CONTENTS
Intro & TOC
Servlet Overview
Setup
Servlets and
JavaServer Pages
(JSP) 1.0: A Tutorial
First Servlets
Table of Contents
Handling FORM Data
Request Headers
■
CGI Variables
■
Response Status Codes
Response Headers
Cookies
Session Tracking
■
JavaServer Pages (JSP)
Servlet/JSP Book. Sun
Press book that grew
from this tutorial.
■
Servlet/JSP short
courses Public or onsite courses.
■
Extended Table of Contents
(This page)
Sun Microsystems Press
book on servlets 2.2 and
JSP 1.1 This online tutorial
served as a very early
outline for the book, but of
course the book covers
many more topics and
includes much more detail.
Overview of Servlets and
JSP
● What is a Java
servlet?
● What advantage
does a Java servlet
have over a
traditional CGI
program?
● What is JSP?
● What are the
advantages of JSP?
Getting Started: Installation
and Setup
● Obtaining and
Installing the Servlet
and JSP Classes
● Getting a ServletCapable Web Server
or Servlet Engine
First Servlets
■
■
■
Accessing the Standard CGI
Variables
● The CGI Variables,
Their Meaning, and
the Servlet
Equivalent
● Example: Making a
Table of All CGI
Variables
Generating the Response:
HTTP Status Codes
● Overview: Status
Codes and Messages
● Setting Status Codes
from Servlets
● HTTP 1.1 Status
Codes and Their
Meanings
● Example: A Search
Engine Front End
Generating the Response:
HTTP Response Headers
● Overview
● Common Response
Headers and Their
Meaning
● Example: Starting a
Long-Running
Calculation,
Showing Partial
Results, and
Periodically
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....apl.jhu.edu/~hall/java/Servlet-Tutorial/index.htm (1 of 3) [29/07/2002 15:01:07]
A Tutorial on Java Servlets and Java Server Pages (JSP)
Basic Servlet
Structure
● A Simple Servlet
Generating Plain
Text
● Compiling and
Invoking the Servlet
● A Simple Servlet
Generating HTML
● Some Simple HTML
Utilities for Servlets
Processing the Request:
Form Data
● Introduction
(Format, URLencoding, GET,
POST)
● Example: Reading
Specific Parameters
● Example: Making
Table of All
Parameters
Processing the Request:
HTTP Request Headers
● Common Request
Headers and Their
Meaning
● Reading Headers
from Servlets
● Example: Making
Table of All Headers
●
■
■
■
■
■
Updating Display
with New Data.
Handling Cookies
● Intro: How Cookies
Can Be Used (and
Abused)
● The Servlet Cookie
API
● Some Cookie
Utilities
● Example: A
Customized Search
Engine Interface
Session Tracking
● Overview of Session
Tracking
● The Servlet Session
Tracking API
● Example
JavaServer Pages (JSP)
● JSP Overview
● JSP Syntax
Summary
● Template Text
(Static HTML)
● JSP Scripting
Elements:
Expressions,
Scriptlets, and
Declarations
● JSP Directives
● Example using JSP
Scripting Elements
and Directives
● Predefined Variables
● JSP Actions
● JSP Comments and
Character Escaping
Conventions
● Creating Custom JSP
Tag Libraries
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....apl.jhu.edu/~hall/java/Servlet-Tutorial/index.htm (2 of 3) [29/07/2002 15:01:07]
A Tutorial on Java Servlets and Java Server Pages (JSP)
Hall. All source code freely available for
unrestricted use.
This tutorial was a very early outline of the
material that eventually became Core Servlets
and JavaServer Pages (Sun Microsystems Press
and Prentice Hall, May 2000). See
http://www.coreservlets.com/ for the topics
covered, table of contents, index, reader
reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....apl.jhu.edu/~hall/java/Servlet-Tutorial/index.htm (3 of 3) [29/07/2002 15:01:07]
Java Servlet Tutorial
Servlets and JavaServer
Pages (JSP) 1.0: A Tutorial
Table of Contents
■
■
■
■
■
■
Extended Table of Contents (This page)
Sun Microsystems Press book on servlets
2.2 and JSP 1.1 This online tutorial served
as a very early outline for the book, but of
course the book covers many more topics
and includes much more detail.
Overview of Servlets and JSP
● What is a Java servlet?
● What advantage does a Java servlet
have over a traditional CGI
program?
● What is JSP?
● What are the advantages of JSP?
Getting Started: Installation and Setup
● Obtaining and Installing the Servlet
and JSP Classes
● Getting a Servlet-Capable Web
Server or Servlet Engine
First Servlets
● Basic Servlet Structure
● A Simple Servlet Generating Plain
Text
● Compiling and Invoking the
Servlet
● A Simple Servlet Generating
HTML
● Some Simple HTML Utilities for
Servlets
Processing the Request: Form Data
● Introduction (Format, URLencoding, GET, POST)
● Example: Reading Specific
Parameters
● Example: Making Table of All
■
■
■
■
■
Accessing the Standard CGI Variables
● The CGI Variables, Their Meaning,
and the Servlet Equivalent
● Example: Making a Table of All
CGI Variables
Generating the Response: HTTP Status
Codes
● Overview: Status Codes and
Messages
● Setting Status Codes from Servlets
● HTTP 1.1 Status Codes and Their
Meanings
● Example: A Search Engine Front
End
Generating the Response: HTTP Response
Headers
● Overview
● Common Response Headers and
Their Meaning
● Example: Starting a Long-Running
Calculation, Showing Partial
Results, and Periodically Updating
Display with New Data.
Handling Cookies
● Intro: How Cookies Can Be Used
(and Abused)
● The Servlet Cookie API
● Some Cookie Utilities
● Example: A Customized Search
Engine Interface
Session Tracking
● Overview of Session Tracking
● The Servlet Session Tracking API
● Example
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ll/java/Servlet-Tutorial/Servlet-Tutorial-Intro.html (1 of 2) [29/07/2002 15:01:24]
Java Servlet Tutorial
■
Parameters
Processing the Request: HTTP Request
Headers
● Common Request Headers and
Their Meaning
● Reading Headers from Servlets
● Example: Making Table of All
Headers
■
JavaServer Pages (JSP)
● JSP Overview
● JSP Syntax Summary
● Template Text (Static HTML)
● JSP Scripting Elements:
Expressions, Scriptlets, and
Declarations
● JSP Directives
● Example using JSP Scripting
Elements and Directives
● Predefined Variables
● JSP Actions
● JSP Comments and Character
Escaping Conventions
● Creating Custom JSP Tag Libraries
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All
source code freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became
Core Servlets and JavaServer Pages (Sun Microsystems Press and Prentice
Hall, May 2000). See http://www.coreservlets.com/ for the topics covered,
table of contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ll/java/Servlet-Tutorial/Servlet-Tutorial-Intro.html (2 of 2) [29/07/2002 15:01:24]
Servlets and JSP: An Overview
Servlets and JSP: An
Overview
1. What are Java Servlets?
2. What are the Advantage of Servlets Over
"Traditional" CGI?
3. What is JSP?
4. What are the Advantages of JSP?
5. Servlet and JSP Tutorial: Top
1. What are Java Servlets?
Servlets are Java technology's answer to CGI programming. They are programs that run on a Web
server and build Web pages. Building Web pages on the fly is useful (and commonly done) for a
number of reasons:
● The Web page is based on data submitted by the user. For example the results pages from
search engines are generated this way, and programs that process orders for e-commerce sites
do this as well.
● The data changes frequently. 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, you would use this for making a Web page at an on-line store that lists current prices
and number of items in stock.
2. What are the Advantage of Servlets Over
"Traditional" CGI?
Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than
traditional CGI and than many alternative CGI-like technologies. (More importantly, servlet
developers get paid more than Perl programmers :-).
● Efficient. With traditional CGI, a new process is started for each HTTP request. If the CGI
program does a relatively fast operation, the overhead of starting the process can dominate the
execution time. With servlets, the Java Virtual Machine stays up, and each request is handled
by a lightweight Java thread, not a heavyweight operating system process. Similarly, in
traditional CGI, if there are N simultaneous request to the same CGI program, then the code for
the CGI program is loaded into memory N times. With servlets, however, there are N threads
but only a single copy of the servlet class. Servlets also have more alternatives than do regular
CGI programs for optimizations such as caching previous computations, keeping database
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...va/Servlet-Tutorial/Servlet-Tutorial-Overview.html (1 of 3) [29/07/2002 15:01:26]
Servlets and JSP: An Overview
●
●
●
●
connections open, and the like.
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 utilities.
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
can't). 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 I-Planet Enterprise Server can run virtually unchanged on Apache,
Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every
major Web server.
Inexpensive. There are a number of free or very inexpensive Web servers available that are
good for "personal" use or low-volume Web sites. However, with the major exception of
Apache, which is free, most commercial-quality Web servers are relatively expensive.
Nevertheless, once you have a Web server, no matter the cost of that server, adding servlet
support to it (if it doesn't come preconfigured to support servlets) is generally free or cheap.
3. What is JSP?
Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamicallygenerated 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">
<HTML>
<HEAD><TITLE>Welcome to Our Store</TITLE></HEAD>
<BODY>
<H1>Welcome to Our Store</H1>
<SMALL>Welcome,
<!-- User name is "New User" for first-time visitors -->
<% out.println(Utils.getUserNameFromCookie(request)); %>
To access your account settings, click
<A HREF="Account-Settings.html">here.</A></SMALL>
<P>
Regular HTML for all the rest of the on-line store's Web page.
</BODY></HTML>
4. What are the Advantages of JSP?
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...va/Servlet-Tutorial/Servlet-Tutorial-Overview.html (2 of 3) [29/07/2002 15:01:26]
Servlets and JSP: An Overview
●
●
●
●
●
vs. Active Server Pages (ASP). ASP is a similar technology from Microsoft. The advantages
of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MSspecific language, so it is more powerful and easier to use. Second, it is portable to other
operating systems and non-Microsoft Web servers.
vs. Pure Servlets. JSP doesn't give you anything that you couldn't in principle do with a
servlet. But it is more convenient to write (and to modify!) regular HTML than to have a
zillion println statements that generate the HTML. Plus, by separating the look from the
content you can put different people on different tasks: your Web page design experts can
build the HTML, leaving places for your servlet programmers to insert the dynamic content.
vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including externallydefined 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.
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 can't access server-side resources like
databases, catalogs, pricing information, and the like.
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 page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All
source code freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became
Core Servlets and JavaServer Pages (Sun Microsystems Press and Prentice
Hall, May 2000). See http://www.coreservlets.com/ for the topics covered,
table of contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...va/Servlet-Tutorial/Servlet-Tutorial-Overview.html (3 of 3) [29/07/2002 15:01:26]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...w.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html
Getting Started: Software
and Server Setup
1. Obtain and Install the Servlet and JSP Development
Kits
2. Install a Servlet-Capable Web Server
3. Servlet and JSP Tutorial: Top
1. Obtain and Install the Servlet and JSP
Development Kits
Your first step is to download software that implements the Java Servlet 2.1 or 2.2 and Java Server
Pages 1.0 or 1.1 specifications. You can get a free version from Sun, known as the JavaServer Web
Development Kit (JSWDK), at http://java.sun.com/products/servlet/.
Next, you need to tell javac where to find the servlet and JSP classes when you compile a servlet
file. The JSWDK installation instructions explain this, but it basically amounts to putting
servlet.jar and jsp.jar (which come with the JSWDK) on your CLASSPATH. If you've
never dealt with the CLASSPATH before, it is the variable that specifies where Java looks for classes.
If it is unspecified, Java looks in the current directory and the standard system libraries. If you set it
yourself, you need to be sure to include ".", signifying the current directory. Here's a quick summary
of how to set it on a couple of different platforms:
Unix (C Shell)
setenv CLASSPATH .:servlet_dir/servlet.jar:servlet_dir/jsp.jar
Add ":$CLASSPATH" to the end of the setenv line if your CLASSPATH is already set, and you
want to add more to it, not replace it. Note that you use colons to separate directories, while you use
semicolons on Windows. To make this setting permanent, you'd typically put this statement in your
.cshrc file.
Windows 95/98/NT
set CLASSPATH=.;servlet_dir/servlet.jar;servlet_dir/jsp.jar
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html (1 of 3) [29/07/2002 15:01:28]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...w.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html
Add ";%CLASSPATH%" to the end of the above line if your CLASSPATH is already set. Note that
you use semicolons to separate directories, while you use colons on Unix. To make this setting
permanent in Windows 95/98 you'd typically put this statement in your autoexec.bat file. On
Windows NT, you'd go to the Start menu, select Settings, select Control Panel, select System, select
Environment, then enter the variable and value.
Finally, as you'll see in the next section, you probably want to put your servlets into packages to avoid
name conflicts with servlets other people write for the same Web or application server. In that case,
you may find it convenient to add the top-level directory of your package hierarchy to the
CLASSPATH as well. See the section on first servlets for details.
2. Install a Servlet-Capable Web Server
Your next step is to obtain and install a Web server that supports Java servlets, or to install a servlet
package in your existing Web server. If you are using an up-to-date Web or application server, there is
a good chance that it already has everything you need. Check your server documentation or see the
latest list of servers that supports servlets at http://java.sun.com/products/servlet/industry.html.
Although you’ll eventually want to deploy in a commercial-quality server, when first learning it is
useful to have a free system that you can install on your desktop machine for development and testing
purposes. Here are some of the most popular options:
● Apache Tomcat.
Tomcat is the official reference implementation of the servlet 2.2 and JSP 1.1 specifications. It
can be used as a small stand-alone server for testing servlets and JSP pages, or can be
integrated into the Apache Web server. As of early 2000, it was the only server to support the
servlet 2.2 or JSP 1.1 specifications. However, many other servers have announced upcoming
support. Tomcat, like Apache itself, is free. However, also like Apache (which is very fast,
highly reliable, but a bit hard to configure and install), Tomcat requires significantly more
effort to set up than do the commercial servlet engines. For details, see
http://jakarta.apache.org/.
● JavaServer Web Development Kit (JSWDK).
The JSWDK is the official reference implementation of the servlet 2.1 and JSP 1.0
specifications. It is used as a small stand-alone server for testing servlets and JSP pages before
they are deployed to a full Web server that supports these technologies. It is free and reliable,
but takes quite a bit of effort to install and configure. For details, see
http://java.sun.com/products/servlet/download.html.
● Allaire JRun.
JRun is a servlet and JSP engine that can be plugged into Netscape Enterprise or FastTrack
servers, IIS, Microsoft Personal Web Server, older versions of Apache, O’Reilly’s WebSite, or
StarNine WebSTAR. A limited version that supports up to five simultaneous connections is
available for free; the commercial version removes this restriction and adds capabilities like a
remote administration console. For details, see http://www.allaire.com/products/jrun/.
● New Atlanta’s ServletExec.
ServletExec is a fast servlet and JSP engine that can be plugged into most popular Web servers
for Solaris, Windows, MacOS, HP-UX and Linux. You can download and use it for free, but
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html (2 of 3) [29/07/2002 15:01:28]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...w.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html
●
●
many of the advanced features and administration utilities are disabled until you purchase a
license. New Atlanta also provides a free servlet debugger that works with many of the popular
Java IDEs. For details, see http://newatlanta.com/.
Gefion's LiteWebServer (LWS). LWS is a small free Web server that supports servlets version
2.2 and JSP 1.1. They also have a free plugin called WAICoolRunner that adds servlet 2.2 and
JSP 1.1 support to Netscape FastTrack and Enterprise servers. They also sell a number of
custom servlet and JSP components in a package called InstantOnline. For details, see
http://www.gefionsoftware.com/.
Sun’s Java Web Server.
This server is written entirely in Java and was one of the first Web servers to fully support the
servlet 2.1 and JSP 1.0 specifications. Although it is no longer under active development
because Sun is concentrating on the Netscape/I-Planet server, it is still a popular choice for
learning servlets and JSP. For a free trial version, see
http://www.sun.com/software/jwebserver/try/. For a free non-expiring version for teaching
purposes at academic institutions, see http://freeware.thesphere.com/.
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All
source code freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became
Core Servlets and JavaServer Pages (Sun Microsystems Press and Prentice
Hall, May 2000). See http://www.coreservlets.com/ for the topics covered,
table of contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l/java/Servlet-Tutorial/Servlet-Tutorial-Setup.html (3 of 3) [29/07/2002 15:01:28]
Servlet Tutorial: First Servlets
First Servlets
1. Basic Servlet Structure
2. A Simple Servlet Generating Plain Text
3. A Servlet that Generates HTML
4. Simple HTML-Building Utilities
5. Servlet and JSP Tutorial: Top
1. Basic Servlet Structure
Here's the outline of a basic servlet that handles GET requests. GET requests, for those unfamiliar with HTTP,
are requests made by browsers when the user types in a URL on the address line, follows a link from a Web
page, or makes an HTML form that does not specify a METHOD. Servlets can also very easily handle POST
requests, which are generated when someone creates an HTML form that specifies METHOD="POST". We'll
discuss that in later sections.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
// Use "out" to send content to browser
}
}
(Download template source code -- click with the right mouse on the link or hold down SHIFT while clicking
on the link.)
To be a servlet, a class should extend HttpServlet and override doGet or doPost (or both), depending on
whether the data is being sent by GET or by POST. These methods take two arguments: an HttpServletRequest
and an HttpServletResponse. The HttpServletRequest has methods that let you find out about
incoming information such as FORM data, HTTP request headers, and the like. The
HttpServletResponse has methods that lets you specify the HTTP response line (200, 404, etc.),
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (1 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: First Servlets
response headers (Content-Type, Set-Cookie, etc.), and, most importantly, lets you obtain a
PrintWriter used to send output back to the client. For simple servlets, most of the effort is spent in println
statements that generate the desired page. Note that doGet and doPost throw two exceptions, so you are
required to include them in the declaration. Also note that you have to import classes in java.io (for
PrintWriter, etc.), javax.servlet (for HttpServlet, etc.), and javax.servlet.http (for
HttpServletRequest and HttpServletResponse). Finally, note that doGet and doPost are
called by the service method, and sometimes you may want to override service directly, e.g. for a
servlet that handles both GET and POST request.
2. A Simple Servlet Generating Plain Text
Here is a simple servlet that just generates plain text. The following section will show the more usual case
where HTML is generated.
2.1 HelloWorld.java
You can also download the source or try it on-line.
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
2.2 Compiling and Installing the Servlet
Note that the specific details for installing servlets vary from Web server to Web server. Please refer to your
Web server documentation for definitive directions. The on-line examples are running on Java Web Server
(JWS) 2.0, where servlets are expected to be in a directory called servlets in the JWS installation
hierarchy. However, I placed this servlet in a separate package (hall) to avoid conflicts with other servlets on
this server; you'll want to do the same if you are using a Web server that is used by other people and doesn't
have a good infrastructure for "virtual servers" to prevent these conflicts automatically. Thus,
HelloWorld.java actually goes in a subdirectory called hall in the servlets directory. Note that
setup on most other servers is similar, and the servlet and JSP examples in the tutorial have also been tested
using BEA WebLogic and IBM WebSphere 3.0. WebSphere has an excellent mechanism for virtual servers,
and it is not necessary to use packages solely to prevent name conflicts with other users.
If you've never used packages before, there are two main ways to compile classes that are in packages.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (2 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: First Servlets
One way is to set your CLASSPATH to point to the directory above the one actually containing your servlets.
You can them compile normally from within the directory. For example, if your base directory is
C:\JavaWebServer\servlets and your package name (and thus subdirectory name) is hall, and
you were on Windows, you'd do:
DOS> set CLASSPATH=C:\JavaWebServer\servlets;%CLASSPATH%
DOS> cd C:\JavaWebServer\servlets\hall
DOS> javac YourServlet.java
The first part, setting the CLASSPATH, you probably want to do permanently, rather than each time you start a
new DOS window. On Windows 95/98 you'd typically put the "set CLASSPATH=..." statement in your
autoexec.bat file somewhere after the line that set the CLASSPATH to point to servlet.jar and
jsp.jar. On Windows NT, you'd go to the Start menu, select Settings, select Control Panel, select System,
select Environment, then enter the variable and value. Note also that if your package were of the form
name1.name2.name3 rather than simply name1 as here, you'd still have the CLASSPATH point to the
top-level directory of your package hierarchy (the one containing name1).
A second way to compile classes that are in packages is to go to the directory above the one containing your
servlets, and then do "javac directory\YourServlet.java" (Windows; note the backslash) or
"javac directory/YourServlet.java" (Unix; note the forward slash). For example, suppose
again that your base directory is C:\JavaWebServer\servlets and your package name (and thus
subdirectory name) is hall, and you were on Windows. In that case, you'd do the following:
DOS> cd C:\JavaWebServer\servlets
DOS> javac hall\YourServlet.java
Note that, on Windows, most JDK 1.1 versions of javac require a backslash, not a forward slash, after the
directory name. This is fixed in JDK 1.2, but since many Web servers are configured to use JDK 1.1, many
servlet authors stick with JDK 1.1 for portability.
Finally, another advanced option is to keep the source code in a location distinct from the .class files, and use
javac's "-d" option to install them in the location the Web server expects.
2.3 Running the Servlet
With the Java Web Server, servlets are placed in the servlets directory within the main JWS installation
directory, and are invoked via http://host/servlet/ServletName. Note that the directory is
servlets, plural, while the URL refers to servlet, singular. Since this example was placed in the hall
package, it would be invoked via http://host/servlet/hall.HelloWorld. Other Web servers
may have slightly different conventions on where to install servlets and how to invoke them. Most servers also
let you define aliases for servlets, so that a servlet can be invoked via http://host/any-path/anyfile.html. The process for doing this is completely server-specific; check your server's documentation for
details.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (3 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: First Servlets
3. A Servlet that Generates HTML
Most servlets generate HTML, not plain text as in the previous example. To do that, you need two additional
steps: tell the browser that you're sending back HTML, and modify the println statements to build a legal
Web page. The first step is done by setting the Content-Type response header. In general, headers can be
set via the setHeader method of HttpServletResponse, but setting the content type is such a
common task that there is also a special setContentType method just for this purpose. Note that you need
to set response headers before actually returning any of the content via the PrintWriter. Here's an
example:
3.1 HelloWWW.java
You can also download the source or try it on-line.
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (4 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: First Servlets
3.2 HelloWWW Result
4. Simple HTML-Building Utilities
It is a bit cumbersome to generate HTML with println statements. The real solution is to use Java Server
Pages (JSP), which is discussed later in this tutorial. However, for standard servlets, there are two parts of the
Web page (DOCTYPE and HEAD) that are unlikely to change and thus could benefit from being incorporated
into a simple utility file.
The DOCTYPE line is technically required by the HTML spec, and although most major browsers ignore it, it
is very useful when sending pages to formal HTML validators. These validators compare the HTML syntax of
pages against the formal HTML specification, and use the DOCTYPE line to determine which version of
HTML to check against. Their use is very highly recommended both for static HTML pages and for pages
generated via servlets, so the use of DOCTYPE is well worth the effort, especially if it can be easily
incorporated into a servlet utilities class.
In many Web pages, the HEAD line contains nothing but the TITLE, although advanced developers may want
to include META tags and style sheets. But for the simple case, I'll create a method that takes a title as input and
returns the DOCTYPE, HEAD, and TITLE entries as output. Here's the code:
4.1 ServletUtilities.java (Download source code)
package hall;
public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (5 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: First Servlets
// Other utilities will be shown later...
}
4.2 HelloWWW2.java (Download source code)
Here's a rewrite of the HelloWWW class that uses this.
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(ServletUtilities.headWithTitle("Hello WWW") +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source
code freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became Core
Servlets and JavaServer Pages (Sun Microsystems Press and Prentice Hall, May
2000). See http://www.coreservlets.com/ for the topics covered, table of contents,
index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutori...ervlet-Tutorial/Servlet-Tutorial-First-Servlets.html (6 of 6) [29/07/2002 15:01:32]
Servlet Tutorial: Handling Form Data
Handling Form Data
1. Introduction
2. Example: Reading Three Parameters
3. Example: Listing All Form Data
4. Servlet and JSP Tutorial: Top
1. Introduction
If you've ever used a Web search engine, visited an on-line bookstore, tracked stocks on-line, or asked a Web-based site for quotes on plane
tickets, you've probably seen funny looking URLs like http://host/path?user=Marty+Hall&origin=bwi&dest=lax. The
part after the question mark (i.e. user=Marty+Hall&origin=bwi&dest=lax) is known as form data, and is the most common way to
get data from a Web page to a server-side program. It can be attached to the end of the URL after a question mark (as above), for GET requests,
or sent to the server on a separate line, for POST requests.
Extracting the needed information from this form data is traditionally one of the most tedious parts of CGI programming. First of all, you have
to read the data one way for GET requests (in traditional CGI, this is usually via the QUERY_STRING environment variable), and another way
for POST requests (usually by reading the standard input). Second, you have to chop the pairs at the ampersands, then separate the parameter
names (left of the equals signs) from the parameter values (right of the equals signs). Third, you have to URL-decode the values. Alphanumeric
characters get sent unchanged, but spaces get converted to plus signs and other characters get converted to %XX where XX is the ASCII (or
ISO Latin-1) value of the character, in hex. For example, if someone entered a value of "~hall, ~gates, and ~mcnealy" into a
textfield with the name "users" in an HTML form, the data would get sent as
"users=%7Ehall%2C+%7Egates%2C+and+%7Emcnealy". Finally, the fourth reason that parsing form data is tedious is that values
can be omitted (e.g. param1=val1&param2=&param3=val3) and a parameter can have more than one value in that the same parameter
can appear more than once (e.g. param1=val1&param2=val2&param1=val3).
One of the nice features of Java servlets is that all of this form parsing is handled automatically. You simply call the getParameter method
of the HttpServletRequest, supplying the parameter name as an argument. Note that parameter names are case sensitive. You do this
exactly the same way when the data is sent via GET as you do when it is sent via POST. The return value is a String corresponding to the
uudecoded value of the first occurrence of that parameter name. An empty String is returned if the parameter exists but has no value, and
null is returned if there was no such parameter. If the parameter could potentially have more than one value, as in the example above, you
should call getParameterValues instead of getParameter. This returns an array of strings. Finally, although in real applications
your servlets probably have a specific set of parameter names they are looking for, for debugging purposes it is sometimes useful to get a full
list. Use getParameterNames for this, which returns an Enumeration, each entry of which can be cast to a String and used in a
getParameter call.
2. Example: Reading Three Parameters
Here's a simple example that reads parameters named param1, param2, and param3, listing their values in a bulleted list. Note that,
although you are required to specify response settings (content type, status line, other HTTP headings) before beginning to generate the content,
there is no requirement that you read the request parameters at any particular time.
Also note you can easily make servlets that can handle both GET and POST data, simply by having its doPost method call doGet or by
overriding service (which calls doGet, doPost, doHead, etc.). This is good standard practice, since it requires very little extra work and
permits flexibility on the part of the client. If you're used to the traditional CGI approach where you read POST data via the standard input, you
should note that there is a similar way with servlets by first calling getReader or getInputStream on the HttpServletRequest.
This is a bad idea for regular parameters, but might be of use for uploaded files or POST data being sent by custom clients rather than via
HTML forms. Note, however, that if you read the POST data in that manner, it might no longer be found by getParameter.
2.1 ThreeParams.java
You can also download the source or try it on-line. Note: also uses ServletUtilities.java, shown earlier.
package hall;
import java.io.*;
import javax.servlet.*;
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (1 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Handling Form Data
import javax.servlet.http.*;
import java.util.*;
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Three Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n"
"<UL>\n" +
" <LI>param1: "
+ request.getParameter("param1") + "\n"
" <LI>param2: "
+ request.getParameter("param2") + "\n"
" <LI>param3: "
+ request.getParameter("param3") + "\n"
"</UL>\n" +
"</BODY></HTML>");
}
+
+
+
+
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.2 ThreeParams Output
3. Example: Listing All Form Data
Here's an example that looks up all the parameter names that were sent and puts them in a table. It highlights parameters that have zero values as
well as ones that have multiple values. First, it looks up all the parameter names via the getParameterNames method of
HttpServletRequest. This returns an Enumeration. Next, it loops down the Enumeration in the standard manner, using
hasMoreElements to determine when to stop and using nextElement to get each entry. Since nextElement returns an Object, it
casts the result to a String and passes that to getParameterValues, yielding an array of Strings. If that array is one entry long and
contains only an empty string, then the parameter had no values, and the servlet generates an italicized "No Value" entry. If the array is more
than one entry long, then the parameter had multiple values, and they are displayed in a bulleted list. Otherwise the one main value is just placed
into the table.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (2 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Handling Form Data
3.1 ShowParameters.java
You can also download the source or try it on-line. Note: also uses ServletUtilities.java, shown earlier.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
/** Shows all the parameters sent to the servlet via either
* GET or POST. Specially marks parameters that have no values or
* multiple values.
*
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class ShowParameters extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.print("<I>No Value</I>");
else
out.print(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (3 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Handling Form Data
}
3.2 Front End to ShowParameters
Here's an HTML form that sends a number of parameters to this servlet. Right click on the source code link to download the HTML. Left click
on the link to try it out on-line. It uses POST to send the data (as should all forms that have PASSWORD entries), demonstrating the value of
having servlets include both a doGet and a doPost. However, just for the sake of illustration, a version using GET can also be downloaded
or tried out on-line.
PostForm.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>A Sample FORM using POST</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">A Sample FORM using POST</H1>
<FORM ACTION="/servlet/hall.ShowParameters"
METHOD="POST">
Item Number:
<INPUT TYPE="TEXT" NAME="itemNum"><BR>
Quantity:
<INPUT TYPE="TEXT" NAME="quantity"><BR>
Price Each:
<INPUT TYPE="TEXT" NAME="price" VALUE="$"><BR>
<HR>
First Name:
<INPUT TYPE="TEXT" NAME="firstName"><BR>
Last Name:
<INPUT TYPE="TEXT" NAME="lastName"><BR>
Middle Initial:
<INPUT TYPE="TEXT" NAME="initial"><BR>
Shipping Address:
<TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA><BR>
Credit Card:<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Visa">Visa<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Master Card">Master Card<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Amex">American Express<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Discover">Discover<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Java SmartCard">Java SmartCard<BR>
Credit Card Number:
<INPUT TYPE="PASSWORD" NAME="cardNum"><BR>
Repeat Credit Card Number:
<INPUT TYPE="PASSWORD" NAME="cardNum"><BR><BR>
<CENTER>
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</CENTER>
</FORM>
</BODY>
</HTML>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (4 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Handling Form Data
3.3 Submission Result
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (5 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Handling Form Data
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for
unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer Pages (Sun
Microsystems Press and Prentice Hall, May 2000). See http://www.coreservlets.com/ for the topics covered, table of
contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...a/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html (6 of 6) [29/07/2002 15:01:37]
Servlet Tutorial: Reading HTTP Request Headers
Reading HTTP Request Headers
1. An Overview of Request Headers
2. Reading Request Headers from Servlets
3. Example: Printing all Headers
4. Servlet and JSP Tutorial: Top
1. An Overview of Request Headers
When an HTTP client (e.g. a browser) sends a request, it is required to supply a request line (usually GET or
POST). If it wants to, it can also send a number of headers, all of which are optional except for ContentLength, which is required only for POST requests. Here are the most common headers:
● Accept The MIME types the browser prefers.
● Accept-Charset The character set the browser expects.
● Accept-Encoding The types of data encodings (such as gzip) the browser knows how to decode.
Servlets can explicitly check for gzip support and return gzipped HTML pages to browsers that support
them, setting the Content-Encoding response header to indicate that they are gzipped. In many
cases, this can reduce page download times by a factor of five or ten.
● Accept-Language The language the browser is expecting, in case the server has versions in more
than one language.
● Authorization Authorization info, usually in response to a WWW-Authenticate header from
the server.
● Connection Use persistent connection? If a servlet gets a Keep-Alive value here, or gets a
request line indicating HTTP 1.1 (where persistent connections are the default), it may be able to take
advantage of persistent connections, saving significant time for Web pages that include several small
pieces (images or applet classes). To do this, it needs to send a Content-Length header in the
response, which is most easily accomplished by writing into a ByteArrayOutputStream, then
looking up the size just before writing it out.
● Content-Length (for POST messages, how much data is attached)
● Cookie (one of the most important headers; see separate section in this tutorial on handling cookies)
● From (email address of requester; only used by Web spiders and other custom clients, not by browsers)
● Host (host and port as listed in the original URL)
● If-Modified-Since (only return documents newer than this, otherwise send a 304 "Not Modified"
response)
● Pragma (the no-cache value indicates that the server should return a fresh document, even if it is a
proxy with a local copy)
● Referer (the URL of the page containing the link the user followed to get to current page)
● User-Agent (type of browser, useful if servlet is returning browser-specific content)
● UA-Pixels, UA-Color, UA-OS, UA-CPU (nonstandard headers sent by some Internet Explorer
versions, indicating screen size, color depth, operating system, and cpu type used by the browser's
system)
For full details on HTTP headers, see the specifications at http://www.w3.org/Protocols/.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (1 of 6) [29/07/2002 15:01:41]
Servlet Tutorial: Reading HTTP Request Headers
2. Reading Request Headers from Servlets
Reading headers is very straightforward; just call the getHeader method of the HttpServletRequest,
which returns a String if the header was supplied on this request, null otherwise. However, there are a
couple of headers that are so commonly used that they have special access methods. The getCookies
method returns the contents of the Cookie header, parsed and stored in an array of Cookie objects. See the
separate section of this tutorial on cookies. The getAuthType and getRemoteUser methods break the
Authorization header into its component pieces. The getDateHeader and getIntHeader
methods read the specified header and then convert them to Date and int values, respectively.
Rather than looking up one particular header, you can use the getHeaderNames to get an Enumeration
of all header names received on this particular request.
Finally, in addition to looking up the request headers, you can get information on the main request line itself.
The getMethod method returns the main request method (normally GET or POST, but things like HEAD,
PUT, and DELETE are possible). The getRequestURI method returns the URI (the part of the URL that
came after the host and port, but before the form data). The getRequestProtocol returns the third part of
the request line, which is generally "HTTP/1.0" or "HTTP/1.1".
3. Example: Printing all Headers
Here's a servlet that simply creates a table of all the headers it receives, along with their associated values. It
also prints out the three components of the main request line (method, URI, and protocol).
3.1 ShowRequestHeaders.java
You can also download the source or try it on-line.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
public class ShowRequestHeaders extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Servlet Example: Showing Request Headers";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<B>Request Method: </B>" +
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (2 of 6) [29/07/2002 15:01:41]
Servlet Tutorial: Reading HTTP Request Headers
request.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
request.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
request.getProtocol() + "<BR><BR>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Header Name<TH>Header Value");
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println("
<TD>" + request.getHeader(headerName));
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3.2 ShowRequestHeaders Output
Here are the results of two typical requests, one from Netscape and one from Internet Explorer. You'll see the
reason Netscape shows a Cookie header when you get to the tutorial section on cookies.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (3 of 6) [29/07/2002 15:01:41]
Servlet Tutorial: Reading HTTP Request Headers
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (4 of 6) [29/07/2002 15:01:41]
Servlet Tutorial: Reading HTTP Request Headers
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for
unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer
Pages (Sun Microsystems Press and Prentice Hall, May 2000). See http://www.coreservlets.com/ for the topics
covered, table of contents, index, reader reviews, and complete source code archive.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (5 of 6) [29/07/2002 15:01:41]
Servlet Tutorial: Reading HTTP Request Headers
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...let-Tutorial/Servlet-Tutorial-Request-Headers.html (6 of 6) [29/07/2002 15:01:41]
Servlet and JSP Tutorial: Accessing the Standard CGI Variables
Accessing the Standard CGI Variables
1. Introduction to CGI Variables
2. Servlet Equivalent of Standard CGI Variables
3. Example: Reading the CGI Variables
4. Servlet and JSP Tutorial: Top
1. Introduction to CGI Variables
If you come to Java servlets from traditional CGI, you are probably used to the idea of "CGI Variables". These are a somewhat eclectic
collection of information about the request. Some are derived from the HTTP request line and headers (e.g. the part of the URI that came
after the question mark and typically contains the form data, or the Content-Length header), some are derived from the socket
itself (e.g. the name and IP address of the requesting host), and some are derived from server installation parameters (e.g. the mapping of
URLs to actual paths).
2. Servlet Equivalent of Standard CGI Variables
Although it probably makes more sense to think of different sources of data (request data, server data, etc.) as distinct, experienced CGI
programmers may find the following table useful. Assume that request is the HttpServletRequest supplied to the doGet
and doPost methods.
CGI Variable
Meaning
Accessing it from doGet or doPost
AUTH_TYPE
If an Authorization header
was supplied, this is the scheme
specified (basic or digest)
request.getAuthType()
CONTENT_LENGTH
For POST requests only, the
number of bytes of data sent.
Technically, the equivalent is
String.valueOf(request.getContentLength()) (a
String) but you'll probably want to just call
request.getContentLength(), which returns an int.
CONTENT_TYPE
MIME type of attached data, if
specified.
request.getContentType()
DOCUMENT_ROOT
Path to directory corresponding to getServletContext().getRealPath("/") Note that this
http://host/
was request.getRealPath("/") in older servlet specifications.
HTTP_XXX_YYY
Access to arbitrary HTTP headers request.getHeader("Xxx-Yyy")
PATH_INFO
Path information attached to the
URL. Since servlets, unlike
standard CGI programs, can talk to
the server, they don't need to treat request.getPathInfo()
this separately. Path info could be
sent as part of the regular form
data.
The path information mapped to a
real path on the server. Again, with
PATH_TRANSLATED
request.getPathTranslated()
servlets there is no need to have a
special case for this.
QUERY_STRING
For GET requests, this is the
attached data as one big string,
with values still URL-encoded.
You rarely want the raw data in
request.getQueryString()
servlets; instead use
request.getParameter to
access individual parameters.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...rvlet-Tutorial/Servlet-Tutorial-CGI-Variables.html (1 of 4) [29/07/2002 15:01:45]
Servlet and JSP Tutorial: Accessing the Standard CGI Variables
REMOTE_ADDR
The IP address of the client that
made the request, as a String. E.g. request.getRemoteAddr()
"192.9.48.9".
REMOTE_HOST
The fully qualified domain name
(e.g. "java.sun.com") of the
request.getRemoteHost()
client that made the request. The
IP address is returned if this cannot
be determined.
REMOTE_USER
If an Authorization header
was supplied, the user part.
REQUEST_METHOD
The request type, which is usually
GET or POST, but is occasionally
request.getMethod()
HEAD, PUT, DELETE,
OPTIONS, or TRACE.
SCRIPT_NAME
Path to servlet.
request.getServletPath()
SERVER_NAME
Web server's name
request.getServerName()
Port server is listening on.
Technically, the equivalent is
String.valueOf(request.getServerPort()), which
returns a String. You'll usually just want
request.getServerPort(), which returns an int.
SERVER_PORT
Name and version used in the
SERVER_PROTOCOL request line (e.g. HTTP/1.0 or
HTTP/1.1).
SERVER_SOFTWARE
Identifying information about the
Web server
request.getRemoteUser()
request.getProtocol()
getServletContext().getServerInfo()
3. Example: Reading the CGI Variables
Here's a servlet that creates a table showing the values of all the CGI variables other than HTTP_XXX_YYY, which are just the HTTP
request headers shown in the previous section.
3.1 ShowCGIVariables.java
You can also download the source or try it on-line. Note: also uses ServletUtilities.java, shown earlier.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
/** Creates a table showing the values of all the CGI variables.
*
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class ShowCGIVariables extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...rvlet-Tutorial/Servlet-Tutorial-CGI-Variables.html (2 of 4) [29/07/2002 15:01:45]
Servlet and JSP Tutorial: Accessing the Standard CGI Variables
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String[][] variables =
{ { "AUTH_TYPE", request.getAuthType() },
{ "CONTENT_LENGTH", String.valueOf(request.getContentLength()) },
{ "CONTENT_TYPE", request.getContentType() },
{ "DOCUMENT_ROOT", getServletContext().getRealPath("/") },
{ "PATH_INFO", request.getPathInfo() },
{ "PATH_TRANSLATED", request.getPathTranslated() },
{ "QUERY_STRING", request.getQueryString() },
{ "REMOTE_ADDR", request.getRemoteAddr() },
{ "REMOTE_HOST", request.getRemoteHost() },
{ "REMOTE_USER", request.getRemoteUser() },
{ "REQUEST_METHOD", request.getMethod() },
{ "SCRIPT_NAME", request.getServletPath() },
{ "SERVER_NAME", request.getServerName() },
{ "SERVER_PORT", String.valueOf(request.getServerPort()) },
{ "SERVER_PROTOCOL", request.getProtocol() },
{ "SERVER_SOFTWARE", getServletContext().getServerInfo() }
};
String title = "Servlet Example: Showing CGI Variables";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>CGI Variable Name<TH>Value");
for(int i=0; i<variables.length; i++) {
String varName = variables[i][0];
String varValue = variables[i][1];
if (varValue == null)
varValue = "<I>Not specified</I>";
out.println("<TR><TD>" + varName + "<TD>" + varValue);
}
out.println("</TABLE></BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3.2 ShowCGIVariables Output
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...rvlet-Tutorial/Servlet-Tutorial-CGI-Variables.html (3 of 4) [29/07/2002 15:01:45]
Servlet and JSP Tutorial: Accessing the Standard CGI Variables
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for
unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer
Pages (Sun Microsystems Press and Prentice Hall, May 2000). See http://www.coreservlets.com/ for the topics
covered, table of contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...rvlet-Tutorial/Servlet-Tutorial-CGI-Variables.html (4 of 4) [29/07/2002 15:01:45]
Servlet Tutorial: Setting HTTP Status Codes
Setting HTTP Status Codes
1. Overview
2. Specifying Status Codes
3. HTTP 1.1 Status Codes and Their Meaning
4. Example: Search Engine Front End
5. Servlet and JSP Tutorial: Top
1. Overview
When a Web server responds to a request from a browser or other Web client, the response typically consists of a status
line, some response headers, a blank line, and the document. Here is a minimal example:
HTTP/1.1 200 OK
Content-Type: text/plain
Hello World
The status line consists of the HTTP version, an integer that is interpreted as a status code, and a very short message
corresponding to the status code. In most cases, all of the headers are optional except for Content-Type, which
specifies the MIME type of the document that follows. Although most responses contain a document, some don't. For
example, responses to HEAD requests should never include a document, and there are a variety of status codes that
essentially indicate failure, and either don't include a document or only include a short "error message document".
Servlets can perform a variety of tasks by manipulating the status line and the response headers. For example, they can
forward the user to other sites; indicate that the attached document is an image, Adobe Acrobat file, or (most commonly)
HTML file; tell the user that a password is required to access the document; and so forth. This section will discuss the
various different status codes and what can be accomplished with them, while the following section will discuss the
response headers.
2. Specifying Status Codes
As described above, the HTTP response status line consists of an HTTP version, a status code, and an associated message.
Since the message is directly associated with the status code and the HTTP version is determined by the server, all the
servlet needs to do is to set the status code. The way to do that is by the setStatus method of
HttpServletResponse. The setStatus method takes an int (the status code) as an argument, but instead of
using explicit numbers, it is clearer and more reliable to use the constants defined in HttpServletResponse. The
name of each constant is derived from the standard HTTP 1.1 message for each constant, all uppercase with a prefix of SC
(for Status Code) and spaces changed to underscores. Thus, since the message for 404 is Not Found, the equivalent
constant in HttpServletResponse is SC_NOT_FOUND. There are two exceptions however. For some odd reason
the constant for code 302 is derived from the HTTP 1.0 message, not the HTTP 1.1 message, and the constant for code 307
is missing altogether.
Setting the status code does not always mean that you don't need to return a document. For example, although most servers
will generate a small "File Not Found" message for 404 responses, a servlet might want to customize this response.
However, if you do this, you need to be sure to call response.setStatus before sending any content via the
PrintWriter.
Although the general method of setting status codes is simply to call response.setStatus(int), there are two
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (1 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
common cases where a shortcut method in HttpServletResponse is provided. The sendError method generates
a 404 response along with a short message formatted inside an HTML document. And the sendRedirect method
generates a 302 response along with a Location header indicating the URL of the new document.
3. HTTP 1.1 Status Codes and Their Meaning
Following is a list of all the available HTTP 1.1 status codes, along with their associated message and interpretation. You
should be cautious in using the status codes that are available only in HTTP 1.1, since many browsers still only support
HTTP 1.0. If you do use status codes specific to HTTP 1.1, in most cases you want to either explicitly check the HTTP
version of the request (via the getProtocol method of the HttpServletRequest) or reserve it for situations
when no HTTP 1.0 status code would be particularly meaningful to the client anyhow.
Status
Code
Associated
Message
Meaning
100
Continue
Continue with partial request. (New in HTTP 1.1)
101
Switching
Protocols
Server will comply with Upgrade header and change to different protocol. (New in HTTP
1.1)
200
OK
Everything's fine; document follows for GET and POST requests. This is the default for
servlets; if you don't use setStatus, you'll get this.
201
Created
Server created a document; the Location header indicates its URL.
202
Accepted
Request is being acted upon, but processing is not completed.
203
NonAuthoritative
Information
Document is being returned normally, but some of the response headers might be incorrect
since a document copy is being used. (New in HTTP 1.1)
204
No Content
No new document; browser should continue to display previous document. This is a useful if
the user periodically reloads a page and you can determine that the previous page is already
up to date. However, this does not work for pages that are automatically reloaded via the
Refresh response header or the equivalent <META HTTP-EQUIV="Refresh" ...>
header, since returning this status code stops future reloading. JavaScript-based automatic
reloading could still work in such a case, though.
205
Reset Content
No new document, but browser should reset document view. Used to force browser to clear
CGI form fields. (New in HTTP 1.1)
206
Partial Content
Client sent a partial request with a Range header, and server has fulfilled it. (New in HTTP
1.1)
300
Multiple Choices
Document requested can be found several places; they'll be listed in the returned document.
If server has a preferred choice, it should be listed in the Location response header.
301
Moved
Permanently
Requested document is elsewhere, and the URL for it is given in the Location response
header. Browsers should automatically follow the link to the new URL.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (2 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
302
Found
Similar to 301, except that the new URL should be interpreted as a temporary replacement,
not a permanent one. Note: the message was "Moved Temporarily" in HTTP 1.0, and the
constant in HttpServletResponse is SC_MOVED_TEMPORARILY, not
SC_FOUND.Very useful header, since browsers automatically follow the link to the new
URL. This status code is so useful that there is a special method for it,
sendRedirect. Using response.sendRedirect(url) has a couple of
advantages over doing
response.setStatus(response.SC_MOVED_TEMPORARILY) and
response.setHeader("Location", url). First, it is easier. Second, with
sendRedirect, the servlet automatically builds a page containing the link (to show to
older browsers that don't automatically follow redirects). Finally, sendRedirect can
handle relative URLs, automatically translating them to absolute ones.
Note that this status code is sometimes used interchangeably with 301. For example, if you
erroneously ask for http://host/~user (missing the trailing slash), some servers will
send 301 and others will send 302.
Technically, browsers are only supposed to automatically follow the redirection if the
original request was GET. See the 307 header for details.
See Other
Like 301/302, except that if the original request was POST, the redirected document (given
in the Location header) should be retrieved via GET. (New in HTTP 1.1)
304
Not Modified
Client has a cached document and performed a conditional request (usually by supplying an
If-Modified-Since header indicating that it only wants documents newer than a
specified date). Server wants to tell client that the old, cached document should still be used.
305
Use Proxy
Requested document should be retrieved via proxy listed in Location header. (New in
HTTP 1.1)
307
Temporary
Redirect
This is identical to 302 ("Found" or "Temporarily Moved"). It was added to HTTP 1.1 since
many browsers erroneously followed the redirection on a 302 response even if the original
message was a POST, even though it really ought to have followed the redirection of a
POST request only on a 303 response. This response is intended to be unambigously clear:
follow redirected GET and POST requests in the case of 303 responses, only follow the
redirection for GET requests in the case of 307 responses. Note: for some reason there is no
constant in HttpServletResponse corresponding to this status code. (New in HTTP
1.1)
400
Bad Request
Bad syntax in the request.
401
Unauthorized
Client tried to access password-protected page without proper authorization. Response
should include a WWW-Authenticate header that the browser would use to pop up a
username/password dialog box, which then comes back via the Authorization header.
403
Forbidden
Resource is not available, regardless of authorization. Often the result of bad file or directory
permissions on the server.
404
Not Found
No resource could be found at that address. This is the standard "no such page" response.
This is such a common and useful response that there is a special method for it in
HttpServletResponse: sendError(message). The advantage of sendError
over setStatus is that, with sendError, the server automatically generates an error
page showing the error message.
405
Method Not
Allowed
The request method (GET, POST, HEAD, DELETE, PUT, TRACE, etc.) was not allowed for
this particular resource. (New in HTTP 1.1)
406
Not Acceptable
Resource indicated generates a MIME type incompatible with that specified by the client via
its Accept header. (New in HTTP 1.1)
303
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (3 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
407
Proxy
Authentication
Required
408
Request Timeout The client took too long to send the request. (New in HTTP 1.1)
409
Conflict
Usually associated with PUT requests; used for situations such as trying to upload an
incorrect version of a file. (New in HTTP 1.1)
410
Gone
Document is gone; no forwarding address known. Differs from 404 in that the document is is
known to be permanently gone in this case, not just unavailable for unknown reasons as with
404. (New in HTTP 1.1)
411
Length Required
Server cannot process request unless client sends a Content-Length header. (New in
HTTP 1.1)
412
Precondition
Failed
Some precondition specified in the request headers was false. (New in HTTP 1.1)
413
Request Entity
Too Large
The requested document is bigger than the server wants to handle now. If the server thinks it
can handle it later, it should include a Retry-After header. (New in HTTP 1.1)
414
Request URI Too
The URI is too long. (New in HTTP 1.1)
Long
415
Unsupported
Media Type
416
Requested Range
Client included an unsatisfiable Range header in request. (New in HTTP 1.1)
Not Satisfiable
417
Expectation
Failed
Value in the Expect request header could not be met. (New in HTTP 1.1)
500
Internal Server
Error
Generic "server is confused" message. It is often the result of CGI programs or (heaven
forbid!) servlets that crash or return improperly formatted headers.
501
Not Implemented
Server doesn't support functionality to fulfill request. Used, for example, when client issues
command like PUT that server doesn't support.
502
Bad Gateway
Used by servers that act as proxies or gateways; indicates that initial server got a bad
response from the remote server.
503
Service
Unavailable
Server cannot respond due to maintenance or overloading. For example, a servlet might
return this header if some thread or database connection pool is currently full. Server can
supply a Retry-After header.
504
Gateway Timeout
Used by servers that act as proxies or gateways; indicates that initial server didn't get a
response from the remote server in time. (New in HTTP 1.1)
505
HTTP Version
Not Supported
Server doesn't support version of HTTP indicated in request line. (New in HTTP 1.1)
Similar to 401, but proxy server must return a Proxy-Authenticate header. (New in
HTTP 1.1)
Request is in an unknown format. (New in HTTP 1.1)
4. Example: Search Engine Front End
Here's an example that makes use of the two most common status codes other than 200: 302 and 404. The 302 code is set
via the shorthand sendRedirect method, and 404 is set via sendError.
In this application, an HTML form first displays a page that lets the user choose a search string, the number of results per
page, and the search engine to use. When the form is submitted, the servlet extracts those three parameters, constructs a
URL with the parameters embedded in a way appropriate to the search engine selected, and redirects the user to that URL.
If the user fails to choose a search engine or a different front end sends an unrecognized search engine name, a 404 error
page is returned saying that the search engine is missing or unknown.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (4 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
If you want to try it out yourself, start with the HTML front-end.
4.1 SearchEngines.java (Download source code)
Note: makes use of the SearchSpec class, shown below, which incorporates information on how to construct URLs to
perform searches at various search sites.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
public class SearchEngines extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// The URLEncoder changes spaces to "+" signs and other
// non-alphanumeric characters to "%XY", where XY is the
// hex value of the ASCII (or ISO Latin-1) character.
// The getParameter method decodes automatically, but since
// we're just passing this on to another server, we need to
// re-encode it.
String searchString =
URLEncoder.encode(request.getParameter("searchString"));
String numResults =
request.getParameter("numResults");
String searchEngine =
request.getParameter("searchEngine");
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length; i++) {
SearchSpec searchSpec = commonSpecs[i];
if (searchSpec.getName().equals(searchEngine)) {
// encodeURL is just planning ahead in case this servlet
// is ever used in an application that does session tracking.
// If cookies are turned off, session tracking is usually
// accomplished by URL rewriting, so all URLs returned
// by servlets should be sent through encodeURL.
String url =
response.encodeURL(searchSpec.makeURL(searchString,
numResults));
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,
"No recognized search engine specified.");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (5 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
doGet(request, response);
}
}
4.2 SearchSpec.java
package hall;
class SearchSpec {
private String name, baseURL, numResultsSuffix;
private static SearchSpec[] commonSpecs =
{ new SearchSpec("google",
"http://www.google.com/search?q=",
"&num="),
new SearchSpec("infoseek",
"http://infoseek.go.com/Titles?qt=",
"&nh="),
new SearchSpec("lycos",
"http://lycospro.lycos.com/cgi-bin/pursuit?query=",
"&maxhits="),
new SearchSpec("hotbot",
"http://www.hotbot.com/?MT=",
"&DC=")
};
public SearchSpec(String name,
String baseURL,
String numResultsSuffix) {
this.name = name;
this.baseURL = baseURL;
this.numResultsSuffix = numResultsSuffix;
}
public String makeURL(String searchString, String numResults) {
return(baseURL + searchString + numResultsSuffix + numResults);
}
public String getName() {
return(name);
}
public static SearchSpec[] getCommonSpecs() {
return(commonSpecs);
}
}
4.3 SearchEngines.html
Right click on the source code link to download the HTML. Left click on the link to try it out on-line.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (6 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
<HEAD>
<TITLE>Searching the Web</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">Searching the Web</H1>
<FORM ACTION="/servlet/hall.SearchEngines">
<CENTER>
Search String:
<INPUT TYPE="TEXT" NAME="searchString"><BR>
Results to Show Per Page:
<INPUT TYPE="TEXT" NAME="numResults"
VALUE=10 SIZE=3><BR>
<INPUT TYPE="RADIO" NAME="searchEngine"
VALUE="google">
Google |
<INPUT TYPE="RADIO" NAME="searchEngine"
VALUE="infoseek">
Infoseek |
<INPUT TYPE="RADIO" NAME="searchEngine"
VALUE="lycos">
Lycos |
<INPUT TYPE="RADIO" NAME="searchEngine"
VALUE="hotbot">
HotBot
<BR>
<INPUT TYPE="SUBMIT" VALUE="Search">
</CENTER>
</FORM>
</BODY>
</HTML>
4.4 Initial Front End
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (7 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
4.5 Search Result
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (8 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Setting HTTP Status Codes
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely
available for unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and
JavaServer Pages (Sun Microsystems Press and Prentice Hall, May 2000). See
http://www.coreservlets.com/ for the topics covered, table of contents, index, reader reviews, and
complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...torial/Servlet-Tutorial-Response-Status-Line.html (9 of 9) [29/07/2002 15:01:52]
Servlet Tutorial: Specifying HTTP Response Headers
Specifying HTTP Response
Headers
1. Overview
2. Common Response Headers and their
Meaning
3. Example: Automatically Reloading Pages as Content
Changes
4. Servlet and JSP Tutorial: Top
1. Overview
A response from a Web server normally consists of a status line, one or more response headers, a blank line, and the
document. Setting the HTTP response headers often goes hand in hand with setting the status codes in the status line.
For example, several of the "document moved" status codes have an accompanying Location header, and a 401
(Unauthorized) code must include an accompanying WWW-Authenticate header.
However, specifying headers can play a useful role even when no unusual status code is set. Response headers can be
used to specify cookies, to supply the modification date (for caching), to instruct the browser to reload the page after a
designated interval, to say how long the file is so that persistent HTTP connections can be used, and many other tasks.
The most general way to specify headers is by the setHeader method of HttpServletResponse, which takes
two strings: the header name and the header value. Like setting the status codes, this must be done before any
document content is sent.
There are also two specialized methods to set headers that contain dates (setDateHeader) and integers
(setIntHeader). The first saves you the trouble of translating a Java date in milliseconds since the epoch (as
returned by System.currentTimeMillis or the getTime method applied to a Date object) into a GMT
time string. The second spares you the minor inconvenience of converting an int to a String.
Rather than setting a header outright, you can add a new header, in case a header with that name already exists. Use
addHeader, addDateHeader, and addIntHeader for this. If it really matters to you whether a specific
header has already been set, use containsHeader to check.
Finally, HttpServletResponse also supplies a number of convenience methods for specifying common headers.
●
●
●
●
The setContentType method sets the Content-Type header, and is used by the majority of servlets.
The setContentLength method sets the Content-Length header, useful if the browser supports
persistent (keep-alive) HTTP connections.
The addCookie method sets a cookie (there is no corresponding setCookie, since it is normal to have
multiple Set-Cookie lines).
And, as discussed in the previous section, the sendRedirect method sets the Location header as well as
setting the status code to 302.
2. Common Response Headers and their Meaning
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (1 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
Header
Interpretation/Purpose
Allow
What request methods (GET, POST, etc.) does the server support?
ContentEncoding
What method was used to encode the document? You need to decode it to get the type specified by
the Content-Type header. Using gzip to compress the document can dramatically reduce
download times for HTML files, but it is only supported by Netscape on Unix and IE 4 and 5 on
Windows. On the other hand, gzipping HTML files can dramatically reduce download times, and
Java's GZIPOutputStream makes it easy. So you should explicitly check if the browser
supports this by looking at the Accept-Encoding header (i.e. via
request.getHeader("Accept-Encoding")). That way, you can return gzipped pages
to browser that know how to unzip them, but still return regular pages to other browsers.
How many bytes are being sent? This information is only needed if the browser is using a persistent
(keep-alive) HTTP connection. If you want your servlet to take advantage of this when the browser
Content-Length supports it, your servlet should write the document into a ByteArrayOutputStream, look up
its size when done, put that into the Content-Length field, then send the content via
byteArrayStream.writeTo(response.getOutputStream()).
Content-Type
What is the MIME type of the following document? Default for servlets is text/plain, but they
usually explicitly specify text/html. Setting this header is so common that there is a special
method in HttpServletResponse for it: setContentType.
Date
What is current time (in GMT)? Use the setDateHeader method to specify this header. That
saves you the trouble of formatting the date string properly.
Expires
At what time should content be considered out of date and thus no longer cached?
Last-Modified
When was document last changed? Client can supply a date via an If-Modified-Since
request header. This is treated as a conditional GET, with document only being returned if the LastModified date is later than the specified date. Otherwise a 304 (Not Modified) status line is
returned. Again, use the setDateHeader method to specify this header.
Location
Where should client go to get document? This is usually set indirectly, along with a 302 status code,
via the sendRedirect method of HttpServletResponse.
Refresh
How soon should browser ask for an updated page (in seconds)? Instead of just reloading current
page, you can specify a specific page to load via setHeader("Refresh", "5;
URL=http://host/path"). Note that this is commonly set via <META HTTPEQUIV="Refresh" CONTENT="5; URL=http://host/path"> in the HEAD section
of the HTML page, rather than as an explicit header from the server. This is because automatic
reloading or forwarding is something often desired by HTML authors who do not have CGI or
servlet access. But for servlets, setting the header directly is easier and clearer. Note that this header
means "reload this page or go to the specified URL in N seconds." It does not mean "reload this
page or go to the specified URL every N seconds." So you have to send a Refresh header each
time, and sending a 204 (No Content) status code stops the browser from reloading further,
regardless of whether you explicitly send the Refresh header or use <META HTTPEQUIV="Refresh" ...>. Note that this header is not officially part of HTTP 1.1, but is an
extension supported by both Netscape and Internet Explorer.
Server
What server am I? Servlets don't usually set this; the Web server itself does.
Set-Cookie
Specifies cookie associated with page. Servlets should not use response.setHeader("SetCookie", ...), but instead use the special-purpose addCookie method of
HttpServletResponse. See separate section on handling cookies.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (2 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
WWWAuthenticate
What authorization type and realm should client supply in their Authorization header? This
header is required in responses that have a 401 (Unauthorized) status line. E.g.
response.setHeader("WWW-Authenticate", "BASIC
realm=\"executives\""). Note that servlets do not usually handle this themselves, but
instead let password-protected Web pages be handled by the Web server's specialized mechanisms
(e.g. .htaccess).
For full details on HTTP headers, see the specifications at http://www.w3.org/Protocols/.
3. Example: Automatically Reloading Pages as Content
Changes
Here is an example that lets you ask for a list of some large prime numbers. Since this may take some time for very
large numbers (e.g. 150 digits), the servlet immediately returns the results found so far, but then keeps calculating,
using a low-priority thread so that it won't degrade Web server performance. If the calculations are not complete, it
instructs the browser to ask for a new page in a few seconds by sending it a Refresh header. If you want to try it out
yourself, start with the HTML front-end.
Note that, in addition to illustrating the value of HTTP response headers, this example shows two other valuable servlet
capabilities. First, it shows that servlets can handle multiple simultaneous connections, each in their own thread. In this
case it maintains a Vector of previous requests for prime calculations, matching the current request to previous ones
by looking at the number of primes (length of list) and number of digits (length of each prime), and synchronizing all
access to this list. Secondly, it shows how easy it is for servlets to maintain state between requests, something that is
cumbersome to implement in traditional CGI and many CGI alternatives. This lets the browser access the ongoing
calculations when reloading the page, plus permits the servlet to keep a list of the N most recently requested results,
returning them immediately if a new request specifies the same parameters as a recent request. We'll discuss persistent
state even further in a later section.
3.1 PrimeNumbers.java (Download source code)
Note: also uses ServletUtilities.java, shown earlier, PrimeList.java for creating a Vector of prime numbers in a
background thread, and Primes.java for generating large random numbers of type BigInteger and checking if they
are prime.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
public class PrimeNumbers extends HttpServlet {
private static Vector primeListVector = new Vector();
private static int maxPrimeLists = 30;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int numPrimes =
ServletUtilities.getIntParameter(request, "numPrimes", 50);
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (3 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
int numDigits =
ServletUtilities.getIntParameter(request, "numDigits", 120);
PrimeList primeList =
findPrimeList(primeListVector, numPrimes, numDigits);
if (primeList == null) {
primeList = new PrimeList(numPrimes, numDigits, true);
synchronized(primeListVector) {
if (primeListVector.size() >= maxPrimeLists)
primeListVector.removeElementAt(0);
primeListVector.addElement(primeList);
}
}
Vector currentPrimes = primeList.getPrimes();
int numCurrentPrimes = currentPrimes.size();
int numPrimesRemaining = (numPrimes - numCurrentPrimes);
boolean isLastResult = (numPrimesRemaining == 0);
if (!isLastResult) {
response.setHeader("Refresh", "5");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Some " + numDigits + "-Digit Prime Numbers";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H2 ALIGN=CENTER>" + title + "</H2>\n" +
"<H3>Primes found with " + numDigits +
" or more digits: " + numCurrentPrimes + ".</H3>");
if (isLastResult)
out.println("<B>Done searching.</B>");
else
out.println("<B>Still looking for " + numPrimesRemaining +
" more<BLINK>...</BLINK></B>");
out.println("<OL>");
for(int i=0; i<numCurrentPrimes; i++) {
out.println(" <LI>" + currentPrimes.elementAt(i));
}
out.println("</OL>");
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//
//
//
//
//
//
See if there is an existing ongoing or completed calculation with
the same number of primes and length of prime. If so, return
those results instead of starting a new background thread. Keep
this list small so that the Web server doesn't use too much memory.
Synchronize access to the list since there may be multiple simultaneous
requests.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (4 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
private PrimeList findPrimeList(Vector primeListVector,
int numPrimes,
int numDigits) {
synchronized(primeListVector) {
for(int i=0; i<primeListVector.size(); i++) {
PrimeList primes = (PrimeList)primeListVector.elementAt(i);
if ((numPrimes == primes.numPrimes()) &&
(numDigits == primes.numDigits()))
return(primes);
}
return(null);
}
}
}
3.3 PrimeNumbers.html
Note: click left on the source code link to try this out yourself. Click right to download a copy of the HTML file.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Finding Large Prime Numbers</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2 ALIGN="CENTER">Finding Large Prime Numbers</H2>
<BR><BR>
<CENTER>
<FORM ACTION="/servlet/hall.PrimeNumbers">
<B>Number of primes to calculate:</B>
<INPUT TYPE="TEXT" NAME="numPrimes" VALUE=25 SIZE=4><BR>
<B>Number of digits:</B>
<INPUT TYPE="TEXT" NAME="numDigits" VALUE=150 SIZE=3><BR>
<INPUT TYPE="SUBMIT" VALUE="Start Calculating">
</FORM>
</CENTER>
</BODY>
</HTML>
3.4 Front End
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (5 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
3.5 Intermediate Result
3.6 Final Result
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (6 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code
freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and
JavaServer Pages (Sun Microsystems Press and Prentice Hall, May 2000). See
http://www.coreservlets.com/ for the topics covered, table of contents, index, reader reviews,
and complete source code archive.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (7 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Specifying HTTP Response Headers
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...t-Tutorial/Servlet-Tutorial-Response-Headers.html (8 of 8) [29/07/2002 15:01:57]
Servlet Tutorial: Handling Cookies
Handling Cookies
1. Overview of Cookies
2. The Servlet Cookie API
3. Some Minor Cookie Utilities
4. Example: A Customized Search Engine Interface
5. Servlet and JSP Tutorial: Top
1. Overview of Cookies
Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when
visiting the same Web site or domain later. By having the server read information it sent the client previously, the site can
provide visitors with a number of conveniences:
● Identifying a user during an e-commerce session. Many on-line stores use a "shopping cart" metaphor in which the
user selects an item, adds it to his shopping cart, then continues shopping. Since the HTTP connection is closed after each
page is sent, when the user selects a new item for his cart, how does the store know that he is the same user that put the
previous item in his cart? Cookies are a good way of accomplishing this. In fact, this is so useful that servlets have an API
specifically for this, and servlet authors don't need to manipulate cookies directly to make use of it. This is discussed in
the tutorial section on Session Tracking.
● Avoiding username and password. Many large sites require you to register in order to use their services, but it is
inconvenient to remember the username and password. Cookies are a good alternative for low-security sites. When a user
registers, a cookie is sent with a unique user ID. When the client reconnects at a later date, the user ID is returned, the
server looks it up, determines it belongs to a registered user, and doesn't require an explicit username and password.
● Customizing a site. Many "portal" sites let you customize the look of the main page. They use cookies to remember what
you wanted, so that you get that result initially next time. I'll give an example like this later in this section of the tutorial.
● Focusing advertising. The search engines charge their customers much more for displaying "directed" ads than "random"
ads. That is, if you do a search on "Java Servlets", a search site can charge much more for an ad for a servlet development
environment than an ad for an on-line travel agent. On the other hand, if the search had been "Bali Hotels", the situation
would be reversed. The problem is that they have to show a random ad when you first arrive and haven't yet performed a
search, as well as when you search on something that doesn't match any ad categories. Cookies let them remember "Oh,
that's the person who was searching for such and such previously" and display an appropriate (read "high priced") ad
instead of a random (read "cheap") one.
Now, providing convenience to the user and added value to the site owner is the purpose behind cookies. And despite much
misinformation, cookies are not a serious security threat. Cookies are never interpreted or executed in any way, and thus can't be
used to insert viruses or attack your system in any way. Furthermore, since browsers generally only accept 20 cookies per site
and 300 cookies total, and each cookie is limited to 4KB, cookies cannot be used to fill up someone's disk or launch other denial
of service attacks. However, even though they don't present a serious security threat, they can preseent a significant threat to
privacy. First, some people don't like the fact that search engines can remember that they're the person that usually does searches
on such and such a topic. For example, they might search for job openings or health data, and don't want some banner ad tipping
off their coworkers next time they do a search. Even worse, two search engines could share data on a user by both loading small
images off a third party site, where that third party uses cookies and shares the data with both search engines. (Netscape,
however, provides a nice feature that lets you refuse cookies from sites other than that to which you connected, but without
disabling cookies altogether.) This trick can even be exploited via email if you use an HTML-enabled email reader that
"supports" cookies, as Outlook Express does. Thus, people could send you email that loads images, attach cookies to those
images, then later identify you (email address and all) when you went to their site.
Or, a site that ought to have much higher security standards might let users skip user name and passwords via cookies. For
example, some of the big on-line bookstores use cookies to remember users, and let you order without reentering much of your
personal information. However, they don't actually display the full credit card number, and only let you send books to an address
that was entered when you did enter the credit card in full or use the username and password. As a result, someone using the
person's computer (or stealing their cookie file) could do no more harm than sending a big book order to the credit card owner's
address, where it could be refused. However, smaller companies might not be so careful, and access to someone's computer or
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (1 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
cookie file could result in loss of valuable personal information. Even worse, incompetent sites might embed credit card or other
sensitive information directly in the cookies themselves, rather than using innocuous identifiers which are only linked to real
users on the server.
The point of all this is twofold. First, due to real and perceived privacy problems, some users turn off cookies. So, even when
you use cookies to give added value to a site, your site shouldn't depend on them. Second, as the author of servlets that use
cookies, you should be careful not to trust cookies for particularly sensitive information, since this would open the user up to
risks if somebody accessed their computer or cookie files.
2. The Servlet Cookie API
To send cookies to the client, a servlet would create one or more cookies with the appropriate names and values via new
Cookie(name, value) (section 2.1), set any desired optional attributes via cookie.setXxx (section 2.2), and add the
cookies to the response headers via response.addCookie(cookie) (section 2.3). To read incoming cookies, call
request.getCookies(), which returns an array of Cookie objects. In most cases, you loop down this array until you
find the one whose name (getName) matches the name you have in mind, then call getValue on that Cookie to see the
value associated with that name. This is discussed in section 2.4.
2.1 Creating Cookies
A Cookie is created by calling the Cookie constructor, which takes two strings: the cookie name and the cookie value. Neither
the name nor the value should contain whitespace or any of:
[ ] ( ) = , " / ? @ : ;
2.2 Reading and Specifying Cookie Attributes
Before adding the cookie to the outgoing headers, you can look up or set attributes of the cookie. Here's a summary:
getComment/setComment
Gets/sets a comment associated with this cookie.
getDomain/setDomain
Gets/sets the domain to which cookie applies. Normally, cookies are returned only to the exact hostname that sent them.
You can use this method to instruct the browser to return them to other hosts within the same domain. Note that the
domain should start with a dot (e.g. .prenhall.com), and must contain two dots for non-country domains like .com,
.edu, and .gov, and three dots for country domains like .co.uk and .edu.es.
getMaxAge/setMaxAge
Gets/sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last
only for the current session (i.e. until the user quits the browser), and will not be stored on disk. See the
LongLivedCookie class below, which defines a subclass of Cookie with a maximum age automatically set one
year in the future.
getName/setName
Gets/sets the name of the cookie. The name and the value are the two pieces you virtually always care about. Since the
getCookies method of HttpServletRequest returns an array of Cookie objects, it is common to loop down
this array until you have a particular name, then check the value with getValue. See the getCookieValue method
shown below.
getPath/setPath
Gets/sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same
directory as the current page as well as all subdirectories. This method can be used to specify something more general.
For example, someCookie.setPath("/") specifies that all pages on the server should receive the cookie. Note
that the path specified must include the current directory.
getSecure/setSecure
Gets/sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
getValue/setValue
Gets/sets the value associated with the cookie. Again, the name and the value are the two parts of a cookie that you
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (2 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
almost always care about, although in a few cases a name is used as a boolean flag, and its value is ignored (i.e the
existence of the name means true).
getVersion/setVersion
Gets/sets the cookie protocol version this cookie complies with. Version 0, the default, adheres to the original Netscape
specification. Version 1, not yet widely supported, adheres to RFC 2109.
2.3 Placing Cookies in the Response Headers
The cookie is added to the Set-Cookie response header by means of the addCookie method of
HttpServletResponse. Here's an example:
Cookie userCookie = new Cookie("user", "uid1234");
response.addCookie(userCookie);
2.4 Reading Cookies from the Client
To send cookies to the client, you created a Cookie then used addCookie to send a Set-Cookie HTTP response header.
This was discussed above in section 2.1. To read the cookies that come back from the client, you call getCookies on the
HttpServletRequest. This returns an array of Cookie objects corresponding to the values that came in on the Cookie
HTTP request header. Once you have this array, you typically loop down it, calling getName on each Cookie until you find
one matching the name you have in mind. You then call getValue on the matching Cookie, doing some processing specific to
the resultant value. This is such a common process that the following section presents a simple getCookieValue method
that, given the array of cookies, a name, and a default value, returns the value of the cookie matching the name, or, if there is no
such cookie, the designated default value.
3. Some Minor Cookie Utilities
Here are some simple but useful utilities for dealing with cookies.
3.1 Getting the Value of a Cookie with a Specified Name
Here's a section of ServletUtilities.java that slightly simplifies the retrieval of a cookie value given a cookie name by looping
through the array of available Cookie objects, returning the value of any Cookie whose name matches the input. If there is
no match, the designated default value is returned.
public static String getCookieValue(Cookie[] cookies,
String cookieName,
String defaultValue) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
3.2 LongLivedCookie.java (Download source code)
Here's a small class that you can use instead of Cookie if you want your cookie to automatically persist when the client quits
the browser.
package hall;
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (3 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
import javax.servlet.http.*;
public class LongLivedCookie extends Cookie {
public static final int SECONDS_PER_YEAR = 60*60*24*365;
public LongLivedCookie(String name, String value) {
super(name, value);
setMaxAge(SECONDS_PER_YEAR);
}
}
4. Example: A Customized Search Engine Interface
Here's a variation of the SearchEngines example shown in the section on the HTTP status line. In this version, the front end is
dynamically generated instead of coming from a static HTML file. Then, the servlet that reads the parameters and forwards them
to the appropriate search engine also returns cookies to the client that list these values. Next time the client visits the front end,
the cookie values are used to preload the form fields with the most recently used entries.
4.1 SearchEnginesFrontEnd.java
This servlet builds the form-based front end to the search engine servlet. At first blush, the output looks just like the page given
by the static HTML page presented in the section on HTTP status codes. Here, however, selected values are remembered in
cookies (set by the CustomizedSearchEngines servlet that this page sends data to), so if the user comes back to the
same page at a later time (even after quitting the browser and restarting), the page is initialized with the values from the previous
search.
You can also download the source or try it on-line. Note that code uses ServletUtilities.java, for the getCookieValue
method (shown above) and for headWithTitle for generating part of the HTML. It also uses the LongLivedCookie
class, shown above, for creating a Cookie that automatically has a long-term expiration date.
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
public class SearchEnginesFrontEnd extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
String searchString =
ServletUtilities.getCookieValue(cookies,
"searchString",
"Java Programming");
String numResults =
ServletUtilities.getCookieValue(cookies,
"numResults",
"10");
String searchEngine =
ServletUtilities.getCookieValue(cookies,
"searchEngine",
"google");
response.setContentType("text/html");
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (4 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
PrintWriter out = response.getWriter();
String title = "Searching the Web";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">Searching the Web</H1>\n" +
"\n" +
"<FORM ACTION=\"/servlet/hall.CustomizedSearchEngines\">\n" +
"<CENTER>\n" +
"Search String:\n" +
"<INPUT TYPE=\"TEXT\" NAME=\"searchString\"\n" +
"
VALUE=\"" + searchString + "\"><BR>\n" +
"Results to Show Per Page:\n" +
"<INPUT TYPE=\"TEXT\" NAME=\"numResults\"\n" +
"
VALUE=" + numResults + " SIZE=3><BR>\n" +
"<INPUT TYPE=\"RADIO\" NAME=\"searchEngine\"\n" +
"
VALUE=\"google\"" +
checked("google", searchEngine) + ">\n" +
"Google |\n" +
"<INPUT TYPE=\"RADIO\" NAME=\"searchEngine\"\n" +
"
VALUE=\"infoseek\"" +
checked("infoseek", searchEngine) + ">\n" +
"Infoseek |\n" +
"<INPUT TYPE=\"RADIO\" NAME=\"searchEngine\"\n" +
"
VALUE=\"lycos\"" +
checked("lycos", searchEngine) + ">\n" +
"Lycos |\n" +
"<INPUT TYPE=\"RADIO\" NAME=\"searchEngine\"\n" +
"
VALUE=\"hotbot\"" +
checked("hotbot", searchEngine) + ">\n" +
"HotBot\n" +
"<BR>\n" +
"<INPUT TYPE=\"SUBMIT\" VALUE=\"Search\">\n" +
"</CENTER>\n" +
"</FORM>\n" +
"\n" +
"</BODY>\n" +
"</HTML>\n");
}
private String checked(String name1, String name2) {
if (name1.equals(name2))
return(" CHECKED");
else
return("");
}
}
4.2 CustomizedSearchEngines.java
The SearchEnginesFrontEnd servlet shown above sends its data to the CustomizedSearchEngines servlet. In
many respects, it is just like the SearchEngines servlet shown in the section on HTTP status codes. However, in addition to
constructing a URL for a search engine and sending a redirection response to the client, the servlet also sends cookies recording
the user data. These cookies will, in turn, be used by the servlet building the front end to initialize the entries in the HTML
forms.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (5 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
package hall;
import
import
import
import
/**
*
*
*
*
*
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
A varition of the SearchEngine servlet that uses
cookies to remember users choices. These values
are then used by the SearchEngineFrontEnd servlet
to create the form-based front end with these
choices preset.
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class CustomizedSearchEngines extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String searchString = request.getParameter("searchString");
Cookie searchStringCookie =
new LongLivedCookie("searchString", searchString);
response.addCookie(searchStringCookie);
searchString = URLEncoder.encode(searchString);
String numResults = request.getParameter("numResults");
Cookie numResultsCookie =
new LongLivedCookie("numResults", numResults);
response.addCookie(numResultsCookie);
String searchEngine = request.getParameter("searchEngine");
Cookie searchEngineCookie =
new LongLivedCookie("searchEngine", searchEngine);
response.addCookie(searchEngineCookie);
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length; i++) {
SearchSpec searchSpec = commonSpecs[i];
if (searchSpec.getName().equals(searchEngine)) {
String url =
searchSpec.makeURL(searchString, numResults);
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,
"No recognized search engine specified.");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (6 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
}
}
4.3 SearchEnginesFrontEnd Output
Here's the front end as it looks after the user types in some values or if the user comes back to the page in the same or a later
session, after having typed in the values in the previous visit.
4.4 CustomizedSearchEngines Output
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (7 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Handling Cookies
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely
available for unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and
JavaServer Pages (Sun Microsystems Press and Prentice Hall, May 2000). See
http://www.coreservlets.com/ for the topics covered, table of contents, index, reader reviews, and
complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ava/Servlet-Tutorial/Servlet-Tutorial-Cookies.html (8 of 8) [29/07/2002 15:02:04]
Servlet Tutorial: Session Tracking
Session Tracking
1. What is Session Tracking?
2. The Session Tracking API
3. Example: Showing Session Information
4. Example: A Simple Shopping Cart
5. Servlet and JSP Tutorial: Top
1. What is Session Tracking?
There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular, when you
are doing on-line shopping, it is a real annoyance that the Web server can't easily remember previous transactions.
This makes applications like shopping carts very problematic: when you add an entry to your cart, how does the
server know what's already in your cart? Even if servers did retain contextual information, you'd still have problems
with e-commerce. When you move from the page where you specify what you want to buy (hosted on the regular
Web server) to the page that takes your credit card number and shipping address (hosted on the secure server that
uses SSL), how does the server remember what you were buying?
There are three typical solutions to this problem.
1. Cookies. You can use HTTP cookies to store information about a shopping session, and each subsequent
connection can look up the current session and then extract information about that session from some location
on the server machine. This is an excellent alternative, and is the most widely used approach. However, even
though servlets have a high-level and easy-to-use interface to cookies, there are still a number of relatively
tedious details that need to be handled:
❍ Extracting the cookie that stores the session identifier from the other cookies (there may be many, after
all),
❍ Setting an appropriate expiration time for the cookie (sessions interrupted by 24 hours probably should
be reset), and
❍ Associating information on the server with the session identifier (there may be far too much
information to actually store it in the cookie, plus sensitive data like credit card numbers should never
go in cookies).
2. URL Rewriting. You can append some extra data on the end of each URL that identifies the session, and the
server can associate that session identifier with data it has stored about that session. This is also an excellent
solution, and even has the advantage that it works with browsers that don't support cookies or where the user
has disabled cookies. However, it has most of the same problems as cookies, namely that the server-side
program has a lot of straightforward but tedious processing to do. In addition, you have to be very careful that
every URL returned to the user (even via indirect means like Location fields in server redirects) has the
extra information appended. And, if the user leaves the session and comes back via a bookmark or link, the
session information can be lost.
3. Hidden form fields. HTML forms have an entry that looks like the following: <INPUT
TYPE="HIDDEN" NAME="session" VALUE="...">. This means that, when the form is submitted,
the specified name and value are included in the GET or POST data. This can be used to store information
about the session. However, it has the major disadvantage that it only works if every page is dynamically
generated, since the whole point is that each session has a unique identifier.
Servlets provide an outstanding technical solution: the HttpSession API. This is a high-level interface built on
top of cookies or URL-rewriting. In fact, on many servers, they use cookies if the browser supports them, but
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (1 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: Session Tracking
automatically revert to URL-rewriting when cookies are unsupported or explicitly disabled. But the servlet author
doesn't need to bother with many of the details, doesn't have to explicitly manipulate cookies or information
appended to the URL, and is automatically given a convenient place to store data that is associated with each session.
2. The Session Tracking API
Using sessions in servlets is quite straightforward, and involves looking up the session object associated with the
current request, creating a new session object when necessary, looking up information associated with a session,
storing information in a session, and discarding completed or abandoned sessions.
2.1 Looking up the HttpSession object associated with the current request.
This is done by calling the getSession method of HttpServletRequest. If this returns null, you can create
a new session, but this is so commonly done that there is an option to automatically create a new session if there isn't
one already. Just pass true to getSession. Thus, your first step usually looks like this:
HttpSession session = request.getSession(true);
2.2 Looking up Information Associated with a Session.
HttpSession objects live on the server; they're just automatically associated with the requester by a behind-thescenes mechanism like cookies or URL-rewriting. These session objects have a builtin data structure that let you store
any number of keys and associated values. In version 2.1 and earlier of the servlet API, you use
getValue("key") to look up a previously stored value. The return type is Object, so you have to do a
typecast to whatever more specific type of data was associated with that key in the session. The return value is null
if there is no such attribute. In version 2.2, getValue is deprecated in favor of getAttribute, both because of
the better naming match with setAttribute (the match for getValue is putValue, not setValue), and
because setAttribute lets you use an attached HttpSessionBindingListener to monitor values, while
putValue doesn't. Nevertheless, since few commercial servlet engines yet support version 2.2, I'll use getValue
in my examples. Here's one representative example, assuming ShoppingCart is some class you've defined
yourself that stores information on items being purchased.
HttpSession session = request.getSession(true);
ShoppingCart previousItems =
(ShoppingCart)session.getValue("previousItems");
if (previousItems != null) {
doSomethingWith(previousItems);
} else {
previousItems = new ShoppingCart(...);
doSomethingElseWith(previousItems);
}
In most cases, you have a specific attribute name in mind, and want to find the value (if any) already associated with
it. However, you can also discover all the attribute names in a given session by calling getValueNames, which
returns a String array. In version 2.2, use getAttributeNames, which has a better name and which is more
consistent in that it returns an Enumeration, just like the getHeaders and getParameterNames methods
of HttpServletRequest.
Although the data that was explicitly associated with a session is the part you care most about, there are some other
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (2 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: Session Tracking
pieces of information that are sometimes useful as well.
●
●
●
●
●
getId. This method returns the unique identifier generated for each session. It is sometimes used as the key
name when there is only a single value associated with a session, or when logging information about previous
sessions.
isNew. This returns true if the client (browser) has never seen the session, usually because it was just
created rather than being referenced by an incoming client request. It returns false for preexisting sessions.
getCreationTime. This returns the time, in milliseconds since the epoch, at which the session was made. To
get a value useful for printing out, pass the value to the Date constructor or the setTimeInMillis
method of GregorianCalendar.
getLastAccessedTime. This returns the time, in milliseconds since the epoch, at which the session was last
sent from the client.
getMaxInactiveInterval. This returns the amount of time, in seconds, that a session should go without access
before being automatically invalidated. A negative value indicates that the session should never timeout.
2.3 Associating Information with a Session
As discussed in the previous section, you read information associated with a session by using getValue (or
getAttribute in version 2.2 of the servlet spec). To specify information, you use putValue (or
setAttribute in version 2.2), supplying a key and a value. Note that putValue replaces any previous values.
Sometimes that's what you want (as with the referringPage entry in the example below), but other times you
want to retrieve a previous value and augment it (as with the previousItems entry below). Here's an example:
HttpSession session = request.getSession(true);
session.putValue("referringPage", request.getHeader("Referer"));
ShoppingCart previousItems =
(ShoppingCart)session.getValue("previousItems");
if (previousItems == null) {
previousItems = new ShoppingCart(...);
}
String itemID = request.getParameter("itemID");
previousItems.addEntry(Catalog.getEntry(itemID));
// You still have to do putValue, not just modify the cart, since
// the cart may be new and thus not already stored in the session.
session.putValue("previousItems", previousItems);
3. Example: Showing Session Information
Here is a simple example that generates a Web page showing some information about the current session. You can
also download the source or try it on-line.
package hall;
import
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
java.util.*;
/** Simple example of session tracking. See the shopping
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (3 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: Session Tracking
*
*
*
*
*
*/
cart example for a more detailed one.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Searching the Web";
String heading;
Integer accessCount = new Integer(0);;
if (session.isNew()) {
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
Integer oldAccessCount =
// Use getAttribute, not getValue, in version
// 2.2 of servlet API.
(Integer)session.getValue("accessCount");
if (oldAccessCount != null) {
accessCount =
new Integer(oldAccessCount.intValue() + 1);
}
}
// Use putAttribute in version 2.2 of servlet API.
session.putValue("accessCount", accessCount);
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" +
"<H2>Information on Your Session:</H2>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
" <TH>Info Type<TH>Value\n" +
"<TR>\n" +
" <TD>ID\n" +
" <TD>" + session.getId() + "\n" +
"<TR>\n" +
" <TD>Creation Time\n" +
" <TD>" + new Date(session.getCreationTime()) + "\n" +
"<TR>\n" +
" <TD>Time of Last Access\n" +
" <TD>" + new Date(session.getLastAccessedTime()) + "\n" +
"<TR>\n" +
" <TD>Number of Previous Accesses\n" +
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (4 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: Session Tracking
" <TD>" + accessCount + "\n" +
"</TABLE>\n" +
"</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Here's a typical result, shown after visiting the page several without quitting the browser in between:
4. Example: A Simple Shopping Cart
Coming very soon...
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for
unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer Pages
(Sun Microsystems Press and Prentice Hall, May 2000). See http://www.coreservlets.com/ for the topics covered,
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (5 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: Session Tracking
table of contents, index, reader reviews, and complete source code archive.
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...et-Tutorial/Servlet-Tutorial-Session-Tracking.html (6 of 6) [29/07/2002 15:02:08]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
JavaServer Pages (JSP) 1.0
1. Overview
2. Syntax Summary
3. Template Text
(Static HTML)
4. JSP Scripting Elements:
Expressions, Scriptlets, and Declarations
5. JSP Directives
6. Example using Scripting Elements
and Directives
7. Predefined Variables
8. JSP Actions
❍ 8.1. jsp:include
❍ 8.2. jsp:useBean overview
❍ 8.3. jsp:useBean details
❍ 8.4. jsp:setProperty
❍ 8.5. jsp:getProperty
❍ 8.6. jsp:forward
❍ 8.7. jsp:plugin
9. JSP Comments and Character Escaping Conventions
10. Servlet and JSP Tutorial: Top
11. Writing Custom JSP Tag Libraries in JSP 1.1. Online chapter at Sun Java
Developer's Connection.
12. Core Servlets and JavaServer Pages. Servlet and JSP book from Sun
Microsystems Press.
1. Overview
JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. You simply write the regular HTML in
the normal manner, using whatever Web-page-building tools you normally use. You then enclose the code for the dynamic parts in
special tags, most of which start with "<%" and end with "%>". For example, here is a section of a JSP page that results in something like
"Thanks for ordering Core Web Programming" for a URL of
http://host/OrderConfirmation.jsp?title=Core+Web+Programming:
Thanks for ordering
<I><%= request.getParameter("title") %></I>
You normally give your file a .jsp extension, and typically install it in any place you could place a normal Web page. Although what you
write often looks more like a regular HTML file than a servlet, behind the scenes, the JSP page just gets converted to a normal servlet,
with the static HTML simply being printed to the output stream associated with the servlet's service method. This is normally done
the first time the page is requested, and developers can simply request the page themselves when first installing it if they want to be sure
that the first real user doesn't get a momentary delay when the JSP page is translated to a servlet and the servlet is compiled and loaded.
Note also that many Web servers let you define aliases that so that a URL that appears to reference an HTML file really points to a
servlet or JSP page.
Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and
actions. Scripting elements let you specify Java code that will become part of the resultant servlet, directives let you control the overall
structure of the servlet, and actions let you specify existing components that should be used, and otherwise control the behavior of the
JSP engine. To simplify the scripting elements, you have access to a number of predefined variables such as request in the snippet
above.
Note that this tutorial covers version 1.0 of the JSP specification. JSP has changed dramatically since version 0.92, and although these
changes were almost entirely for the better, you should note that version 1.0 JSP pages are almost totally incompatible with the earlier
JSP engines. Note that this JSP tutorial is part of a larger tutorial on servlets and JSP at http://www.apl.jhu.edu/~hall/java/ServletTutorial/.
2. Syntax Summary
JSP Element
Syntax
Interpretation Notes
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (1 of 17) [29/07/2002 15:02:20]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
JSP Expression
JSP Scriptlet
JSP Declaration
JSP page Directive
JSP include
Directive
JSP Comment
The jsp:include
Action
The jsp:useBean
Action
<%= expression %>
<% code %>
<%! code %>
<%@ page att="val" %>
<%@ include file="url" %>
<%-- comment --%>
<jsp:include
page="relative URL"
flush="true"/>
<jsp:useBean att=val*/> or
<jsp:useBean att=val*>
...
</jsp:useBean>
XML equivalent is
<jsp:expression>
Expression is
expression
evaluated and
</jsp:expression>. Predefined variables
placed in
are request, response, out, session,
output.
application, config, and pageContext
(available in scriptlets also).
Code is
inserted in
service
method.
XML equivalent is
<jsp:scriptlet>
code
</jsp:scriptlet>.
Code is
inserted in
body of servlet
class, outside
of service
method.
XML equivalent is
<jsp:declaration>
code
</jsp:declaration>.
XML equivalent is
<jsp:directive.page att="val"\>.
Legal attributes, with default values in bold, are:
● import="package.class"
● contentType="MIME-Type"
● isThreadSafe="true|false"
Directions to
● session="true|false"
the servlet
● buffer="sizekb|none"
engine about
● autoflush="true|false"
general setup.
● extends="package.class"
● info="message"
● errorPage="url"
● isErrorPage="true|false"
● language="java"
A file on the
local system to
be included
when the JSP
page is
translated into
a servlet.
XML equivalent is
<jsp:directive.include
file="url"\>.
The URL must be a relative one. Use the
jsp:include action to include a file at request
time instead of translation time.
Comment;
ignored when If you want a comment in the resultant HTML,
use regular HTML comment syntax of <-JSP page is
translated into comment -->.
servlet.
Includes a file
at the time the
page is
requested.
If you want to include the file at the time the page
is translated, use the page directive with the
include attribute instead. Warning: on some
servers, the included file must be an HTML file or
JSP file, as determined by the server (usually
based on the file extension).
Possible attributes are:
● id="name"
● scope="page|request|session|application"
Find or build a
● class="package.class"
Java Bean.
● type="package.class"
● beanName="package.class"
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (2 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
Set bean
properties,
Legal attributes are
either
● name="beanName"
The
explicitly or
● property="propertyName|*"
<jsp:setProperty att=val*/>
by designating
jsp:setProperty
● param="parameterName"
that value
Action
● value="val"
comes from a
request
parameter.
The
jsp:getProperty
Action
The jsp:forward
Action
The jsp:plugin
Action
<jsp:getProperty
name="propertyName"
value="val"/>
<jsp:forward
page="relative URL"/>
<jsp:plugin
attribute="value"*>
...
</jsp:plugin>
Retrieve and
output bean
properties.
Forwards
request to
another page.
Generates
OBJECT or
EMBED tags,
as appropriate
to the browser
type, asking
that an applet
be run using
the Java
Plugin.
3. Template Text: Static HTML
In many cases, a large percent of your JSP page just consists of static HTML, known as template text. In all respects except one, this
HTML looks just like normal HTML, follows all the same syntax rules, and is simply "passed through" to the client by the servlet
created to handle the page. Not only does the HTML look normal, it can be created by whatever tools you already are using for building
Web pages. For example, I used Allaire's HomeSite for most of the JSP pages in this tutorial.
The one minor exception to the "template text is passed straight through" rule is that, if you want to have "<%" in the output, you need to
put "<\%" in the template text.
4. JSP Scripting Elements
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms:
1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
2. Scriptlets of the form <% code %> that are inserted into the servlet's service method, and
3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
Each of these is described in more detail below.
4.1 JSP Expressions
A JSP expression is used to insert Java values directly into the output. It has the following form:
<%= Java Expression %>
The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page
is requested), and thus has full access to information about the request. For example, the following shows the date/time that the page was
requested:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (3 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined variables that you can use. These implicit objects are discussed in more
detail later, but for the purpose of expressions, the most important ones are:
● request, the HttpServletRequest;
● response, the HttpServletResponse;
● session, the HttpSession associated with the request (if any); and
● out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client.
Here's an example:
Your hostname: <%= request.getRemoteHost() %>
Finally, note that XML authors can use an alternative syntax for JSP expressions:
<jsp:expression>
Java Expression
</jsp:expression>
Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
4.2 JSP Scriptlets
If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the servlet
method that will be built to generate the page. Scriptlets have the following form:
<% Java Code %>
Scriptlets have access to the same automatically defined variables as expressions. So, for example, if you want output to appear in the
resultant page, you would use the out variable.
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets
converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect
the static HTML outside of the scriptlets. For example, the following JSP fragment, containing mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
will get converted to something like:
if (Math.random() < 0.5) {
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
If you want to use the characters "%>" inside a scriptlet, enter "%\>" instead. Finally, note that the XML equivalent of <% Code %> is
<jsp:scriptlet>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (4 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
Code
</jsp:scriptlet>
4.3 JSP Declarations
A JSP declaration lets you define methods or fields that get inserted into the main body of the servlet class (outside of the service
method processing the request). It has the following form:
<%! Java Code %>
Since declarations do not generate any output, they are normally used in conjunction with JSP expressions or scriptlets. For example,
here is a JSP fragment that prints out the number of times the current page has been requested since the server booted (or the servlet
class was changed and reloaded):
<%! private int accessCount = 0; %>
Accesses to page since server reboot:
<%= ++accessCount %>
As with scriptlets, if you want to use the characters "%>", enter "%\>" instead. Finally, note that the XML equivalent of
<%! Code %> is
<jsp:declaration>
Code
</jsp:declaration>
5. JSP Directives
A JSP directive affects the overall structure of the servlet class. It usually has the following form:
<%@ directive attribute="value" %>
However, you can also combine multiple attribute settings for a single directive, as follows:
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
There are two main types of directive: page, which lets you do things like import classes, customize the servlet superclass, and the like;
and include, which lets you insert a file into the servlet class at the time the JSP file is translated into a servlet. The specification also
mentions the taglib directive, which is not supported in JSP version 1.0, but is intended to let JSP authors define their own tags. It is
expected that this will be the main new contribution of JSP 1.1.
5.1 The JSP page Directive
The page directive lets you define one or more of the following case-sensitive attributes:
● import="package.class" or import="package.class1,...,package.classN". This lets you specify
what packages should be imported. For example:
<%@ page import="java.util.*" %>
The import attribute is the only one that is allowed to appear multiple times.
● contentType="MIME-Type" or
contentType="MIME-Type; charset=Character-Set"
This specifies the MIME type of the output. The default is text/html. For example, the directive
<%@ page contentType="text/plain" %>
has the same effect as the scriptlet
<% response.setContentType("text/plain"); %>
● isThreadSafe="true|false". A value of true (the default) indicates normal servlet processing, where multiple
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (5 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
●
●
●
●
●
●
●
●
requests can be processed simultaneously with a single servlet instance, under the assumption that the author synchronized access
to instance variables. A value of false indicates that the servlet should implement SingleThreadModel, with requests
either delivered serially or with simultaneous requests being given separate servlet instances.
session="true|false". A value of true (the default) indicates that the predefined variable session (of type
HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to
it. A value of false indicates that no sessions will be used, and attempts to access the variable session will result in errors at
the time the JSP page is translated into a servlet.
buffer="sizekb|none". This specifies the buffer size for the JspWriter out. The default is server-specific, but must
be at least 8kb.
autoflush="true|false". A value of true, the default, indicates that the buffer should be flushed when it is full. A
value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is
illegal when also using buffer="none".
extends="package.class". This indicates the superclass of servlet that will be generated. Use this with extreme caution,
since the server may be using a custom superclass already.
info="message". This defines a string that can be retrieved via the getServletInfo method.
errorPage="url". This specifies a JSP page that should process any Throwables thrown but not caught in the current
page.
isErrorPage="true|false". This indicates whether or not the current page can act as the error page for another JSP
page. The default is false.
language="java". At some point, this is intended to specify the underlying language being used. For now, don't bother with
this since java is both the default and the only legal choice.
The XML syntax for defining directives is
<jsp:directive.directiveType attribute=value />
For example, the XML equivalent of
<%@ page import="java.util.*" %>
is
<jsp:directive.page import="java.util.*" />
5.2 The JSP include Directive
This directive lets you include files at the time the JSP page is translated into a servlet. The directive looks like this:
<%@ include file="relative url" %>
The URL specified is normally interpreted relative to the JSP page that refers to it, but, as with relative URLs in general, you can tell the
system to interpret the URL relative to the home directory of the Web server by starting the URL with a forward slash. The contents of
the included file are parsed as regular JSP text, and thus can include static HTML, scripting elements, directives, and actions.
For example, many sites include a small navigation bar on each page. Due to problems with HTML frames, this is usually implemented
by way of a small table across the top of the page or down the left-hand side, with the HTML repeated for each page in the site. The
include directive is a natural way of doing this, saving the developers from the maintenance nightmare of actually copying the
HTML into each separate file. Here's some representative code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Servlet Tutorial: JavaServer Pages (JSP) 1.0</TITLE>
<META NAME="author" CONTENT="webmaster@somesite.com">
<META NAME="keywords" CONTENT="...">
<META NAME="description" CONTENT="...">
<LINK REL=STYLESHEET
HREF="Site-Styles.css"
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (6 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
TYPE="text/css">
</HEAD>
<BODY>
<%@ include file="/navbar.html" %>
<!-- Part specific to this page ... -->
</BODY>
</HTML>
Note that since the include directive inserts the files at the time the page is translated, if the navigation bar changes, you need to retranslate all the JSP pages that refer to it. This is a good compromise in a situation like this, since the navigation bar probably changes
infrequently, and you want the inclusion process to be as efficient as possible. If, however, the included files changed more often, you
could use the jsp:include action instead. This includes the file at the time the JSP page is requested, and is discussed in the tutorial
section on JSP actions.
6. Example Using Scripting Elements and Directives
Here is a simple example showing the use of JSP expressions, scriptlets, declarations, and directives. You can also download the source
or try it on-line.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Using JavaServer Pages</TITLE>
<META NAME="author" CONTENT="Marty Hall -- hall@apl.jhu.edu">
<META NAME="keywords"
CONTENT="JSP,JavaServer Pages,servlets">
<META NAME="description"
CONTENT="A quick example of the four main JSP tags.">
<LINK REL=STYLESHEET
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>
<BODY BGCOLOR="#FDF5E6" TEXT="#000000" LINK="#0000EE"
VLINK="#551A8B" ALINK="#FF0000">
<CENTER>
<TABLE BORDER=5 BGCOLOR="#EF8429">
<TR><TH CLASS="TITLE">
Using JavaServer Pages</TABLE>
</CENTER>
<P>
Some dynamic content created using various JSP mechanisms:
<UL>
<LI><B>Expression.</B><BR>
Your hostname: <%= request.getRemoteHost() %>.
<LI><B>Scriptlet.</B><BR>
<% out.println("Attached GET data: " +
request.getQueryString()); %>
<LI><B>Declaration (plus expression).</B><BR>
<%! private int accessCount = 0; %>
Accesses to page since server reboot: <%= ++accessCount %>
<LI><B>Directive (plus expression).</B><BR>
<%@ page import = "java.util.*" %>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (7 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
Current date: <%= new Date() %>
</UL>
</BODY>
</HTML>
Here's a typical result:
7. Predefined Variables
To simplify code in JSP expressions and scriptlets, you are supplied with eight automatically defined variables, sometimes called
implicit objects. The available variables are request, response, out, session, application, config, pageContext,
and page. Details for each are given below.
7.1 request
This is the HttpServletRequest associated with the request, and lets you look at the request parameters (via getParameter),
the request type (GET, POST, HEAD, etc.), and the incoming HTTP headers (cookies, Referer, etc.). Strictly speaking, request is
allowed to be a subclass of ServletRequest other than HttpServletRequest, if the protocol in the request is something
other than HTTP. This is almost never done in practice.
7.2 response
This is the HttpServletResponse associated with the response to the client. Note that, since the output stream (see out below) is
buffered, it is legal to set HTTP status codes and response headers, even though this is not permitted in regular servlets once any output
has been sent to the client.
7.3 out
This is the PrintWriter used to send output to the client. However, in order to make the response object (see the previous
section) useful, this is a buffered version of PrintWriter called JspWriter. Note that you can adjust the buffer size, or even turn
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (8 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
buffering off, through use of the buffer attribute of the page directive. This was discussed in Section 5. Also note that out is used
almost exclusively in scriptlets, since JSP expressions automatically get placed in the output stream, and thus rarely need to refer to out
explicitly.
7.4 session
This is the HttpSession object associated with the request. Recall that sessions are created automatically, so this variable is bound
even if there was no incoming session reference. The one exception is if you use the session attribute of the page directive (see
Section 5) to turn sessions off, in which case attempts to reference the session variable cause errors at the time the JSP page is translated
into a servlet.
7.5 application
This is the ServletContext as obtained via getServletConfig().getContext().
7.6 config
This is the ServletConfig object for this page.
7.7 pageContext
JSP introduced a new class called PageContext to encapsulate use of server-specific features like higher performance
JspWriters. The idea is that, if you access them through this class rather than directly, your code will still run on "regular"
servlet/JSP engines.
7.8 page
This is simply a synonym for this, and is not very useful in Java. It was created as a placeholder for the time when the scripting
language could be something other than Java.
8. Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse
JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. Available actions include:
● jsp:include - Include a file at the time the page is requested. See Section 8.1.
● jsp:useBean - Find or instantiate a JavaBean. See Section 8.2 for an overview, and Section 8.3 for details.
● jsp:setProperty - Set the property of a JavaBean. See Section 8.4.
● jsp:getProperty - Insert the property of a JavaBean into the output. See Section 8.5.
● jsp:forward - Forward the requester to a new page. See Section 8.6.
● jsp:plugin - Generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin. See Section 8.7.
These actions are described in more detail below. Remember that, as with XML in general, the element and attribute names are case
sensitive.
8.1 The jsp:include Action
This action lets you insert files into the page being generated. The syntax looks like this:
<jsp:include page="relative URL" flush="true" />
Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the
time the page is requested. This pays a small penalty in efficiency, and precludes the included page from containing general JSP code (it
cannot set HTTP headers, for example), but it gains significantly in flexibility. For example, here is a JSP page that inserts four different
snippets into a "What's New?" Web page. Each time the headlines change, authors only need to update the four files, but can leave the
main JSP page unchanged.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (9 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
WhatsNew.jsp
You can also download the source or try it on-line.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>What's New</TITLE>
<LINK REL=STYLESHEET
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>
<BODY BGCOLOR="#FDF5E6" TEXT="#000000" LINK="#0000EE"
VLINK="#551A8B" ALINK="#FF0000">
<CENTER>
<TABLE BORDER=5 BGCOLOR="#EF8429">
<TR><TH CLASS="TITLE">
What's New at JspNews.com</TABLE>
</CENTER>
<P>
Here is a summary of our four most recent
<OL>
<LI><jsp:include page="news/Item1.html"
<LI><jsp:include page="news/Item2.html"
<LI><jsp:include page="news/Item3.html"
<LI><jsp:include page="news/Item4.html"
</OL>
</BODY>
</HTML>
news stories:
flush="true"/>
flush="true"/>
flush="true"/>
flush="true"/>
Here's a typical result:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (10 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
8.2 The jsp:useBean Action
This action lets you load in a JavaBean to be used in the JSP page. This is a a very useful capability because it lets you exploit the
reusability of Java classes without sacrificing the convenience that JSP adds over servlets alone. The simplest syntax for specifying that
a bean should be used is:
<jsp:useBean id="name" class="package.class" />
This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id."
However, as we'll see shortly, you can specify a scope attribute that makes the bean associated with more than just the current page. In
that case, it is useful to obtain references to existing beans, and the jsp:useBean action specifies that a new object is instantiated
only if there is no existing one with the same id and scope. Now, once you have a bean, you can modify its properties via
jsp:setProperty, or by using a scriptlet and calling a method explicitly on the object with the variable name specified earlier via
the id attribute. Recall that with beans, when you say "this bean has a property of typeX called foo", you really mean "this class has a
method called getFoo that returns something of type X, and another method called setFoo that takes an X as an argument." The
jsp:setProperty action is discussed in more detail in the next section, but for now note that you can either supply an explicit
value, give a param attribute to say that the value is derived from the named request parameter, or just list the property to indicate
that the value should be derived from the request parameter with the same name as the property. You read existing properties in a JSP
expression or scriptlet by calling the appropriate getXxx method, or more commonly, by using the jsp:getProperty action.
Note that the class specified for the bean must be in the server's regular class path, not the part reserved for classes that get automatically
reloaded when they change. For example, in the Java Web Server, it and all the classes it uses should go in the classes directory or
be in a jar file in the lib directory, not be in the servlets directory.
Here is a very simple example that loads a bean and sets/gets a simple String parameter.
BeanTest.jsp
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (11 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
You can also download the source or try it on-line.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
<LINK REL=STYLESHEET
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean id="test" class="hall.SimpleBean" />
<jsp:setProperty name="test"
property="message"
value="Hello WWW" />
<H1>Message: <I>
<jsp:getProperty name="test" property="message" />
</I></H1>
</BODY>
</HTML>
SimpleBean.java
Here's the source code for the bean used in the BeanTest JSP page. You can also download the source.
package hall;
public class SimpleBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
Here's a typical result:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (12 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
8.3 More jsp:useBean Details
The simplest way to use a bean is to use
<jsp:useBean id="name" class="package.class" />
to load the bean, then use jsp:setProperty and jsp:getProperty to modify and retrieve bean properties. However, there
are two other options. First, you can use the container format, namely
<jsp:useBean ...>
Body
</jsp:useBean>
to indicate that the Body portion should be executed only when the bean is first instantiated, not when an existing bean is found and
used. As discussed below, beans can be shared, so not all jsp:useBean statements result in a new bean being instantiated. Second, in
addition to id and class, there are three other attributes that you can use: scope, type, and beanName. These attributes are
summarized in the following table.
Atribute
Usage
id
Gives a name to the variable that will reference the bean. A previous bean object is used instead of instantiating a new
one if one can be found with the same id and scope.
class
Designates the full package name of the bean.
scope
Indicates the context in which the bean should be made available. There are four possible values: page, request,
session, and application. The default, page, indicates that the bean is only available on the current page
(stored in the PageContext of the current page). A value of request indicates that the bean is only available for
the current client request (stored in the ServletRequest object). A value of session indicates that the object is
available to all pages during the life of the current HttpSession. Finally, a value of application indicates that it
is available to all pages that share the same ServletContext. The reason that the scope matters is that a
jsp:useBean entry will only result in a new object being instantiated if there is no previous object with the same id
and scope. Otherwise the previously existing object is used, and any jsp:setParameter elements or other entries
between the jsp:useBean start and end tags will be ignored.
type
Specifies the type of the variable that will refer to the object. This must match the classname or be a superclass or an
interface that the class implements. Remember that the name of the variable is designated via the id attribute.
beanName
Gives the name of the bean, as you would supply it to the instantiate method of Beans. It is permissible to supply
a type and a beanName, and omit the class attribute.
8.4 The jsp:setProperty Action
You use jsp:setProperty to give values to properties of beans that have been referenced earlier. You can do this in two contexts.
First, you can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:
<jsp:useBean id="myName" ... />
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (13 of 17) [29/07/2002 15:02:21]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
...
<jsp:setProperty name="myName"
property="someProperty" ... />
In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found. A
second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:
<jsp:useBean id="myName" ... >
...
<jsp:setProperty name="myName"
property="someProperty" ... />
</jsp:useBean>
Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.
There are four possible attributes of jsp:setProperty:
Attribute
Usage
name
This required attribute designates the bean whose property will be set. The jsp:useBean element must appear before
the jsp:setProperty element.
property
This required attribute indicates the property you want to set. However, there is one special case: a value of "*" means
that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
value
This optional attribute specifies the value for the property. String values are automatically converted to numbers,
boolean, Boolean, byte, Byte, char, and Character via the standard valueOf method in the target or
wrapper class. For example, a value of "true" for a boolean or Boolean property will be converted via
Boolean.valueOf, and a value of "42" for an int or Integer property will be converted via
Integer.valueOf. You can't use both value and param, but it is permissible to use neither. See the discussion
of param below.
This optional attribute designates the request parameter from which the property should be derived. If the current request
has no such parameter, nothing is done: the system does not pass null to the setter method of the property. Thus, you
can let the bean itself supply default values, overriding them only when the request parameters say to do so. For example,
the following snippet says "set the numberOfItems property to whatever the value of the numItems request
parameter is, if there is such a request parameter. Otherwise don't do anything."
param
<jsp:setProperty name="orderBean"
property="numberOfItems"
param="numItems" />
If you omit both value and param, it is the same as if you supplied a param name that matches the property
name. You can take this idea of automatically using the request property whose name matches the property one step
further by supplying a property name of "*" and omitting both value and param. In this case, the server iterates
through available properties and request parameters, matching up ones with identical names.
Here's an example that uses a bean to create a table of prime numbers. If there is a parameter named numDigits in the request data, it
is passed into the bean's numDigits property. Likewise for numPrimes.
JspPrimes.jsp
To download the JSP source, right click on the source code link. You can also download the source code for the NumberedPrimes
bean referenced by the jsp:useBean element. Browse the source code directory for other Java classes used by
NumberedPrimes. The best way to try it out on-line is to start with the HTML page that acts as a front end to it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
<LINK REL=STYLESHEET
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (14 of 17) [29/07/2002 15:02:22]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean id="primeTable" class="hall.NumberedPrimes" />
<jsp:setProperty name="primeTable" property="numDigits" />
<jsp:setProperty name="primeTable" property="numPrimes" />
Some <jsp:getProperty name="primeTable" property="numDigits" />
digit primes:
<jsp:getProperty name="primeTable" property="numberedList" />
</BODY>
</HTML>
Here's a typical result:
8.5 The jsp:getProperty Action
This element retrieves the value of a bean property, converts it to a string, and inserts it into the output. The two required attributes are
name, the name of a bean previously referenced via jsp:useBean, and property, the property whose value should be inserted. Here's an
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (15 of 17) [29/07/2002 15:02:22]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
example; for more examples, see Sections 8.2 and 8.4.
<jsp:useBean id="itemBean" ... />
...
<UL>
<LI>Number of items:
<jsp:getProperty name="itemBean" property="numItems" />
<LI>Cost of each:
<jsp:getProperty name="itemBean" property="unitCost" />
</UL>
8.6 The jsp:forward Action
This action lets you forward the request to another page. It has a single attribute, page, which should consist of a relative URL. This
could be a static value, or could be computed at request time, as in the two examples below.
<jsp:forward page="/utils/errorReporter.jsp" />
<jsp:forward page="<%= someJavaExpression %>" />
8.7 The jsp:plugin Action
This action lets you insert the browser-specific OBJECT or EMBED element needed to specify that the browser run an applet using the
Java plugin.
9. Comments and Character Quoting Conventions
There are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be
treated specially. Here's a summary:
Syntax
Purpose
<%-- comment --%> A JSP comment. Ignored by JSP-to-scriptlet translator. Any embedded JSP scripting elements, directives,
or actions are ignored.
<!-- comment -->
<\%
%\>
An HTML comment. Passed through to resultant HTML. Any embedded JSP scripting elements,
directives, or actions are executed normally.
Used in template text (static HTML) where you really want "<%".
Used in scripting elements where you really want "%>".
\'
A single quote in an attribute that uses single quotes. Remember, however, that you can use either single or
double quotes, and the other type of quote will then be a regular character.
\"
A double quote in an attribute that uses double quotes. Remember, however, that you can use either single
or double quotes, and the other type of quote will then be a regular character.
%\>
<\%
%> in an attribute.
<% in an attribute.
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for unrestricted use.
This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer Pages (Sun Microsystems
Press 2000). See http://www.coreservlets.com/ for the topics covered, table of contents, index, reader reviews, and complete source code
archive.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (16 of 17) [29/07/2002 15:02:22]
Servlet Tutorial: JavaServer Pages (JSP) 1.0
Order now
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...ll/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html (17 of 17) [29/07/2002 15:02:22]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...w.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowCGIVariables.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
/** Creates a table showing the values of all the CGI variables. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class ShowCGIVariables
extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); String[][] variables = { { "AUTH_TYPE", request.getAuthType() }, {
"CONTENT_LENGTH", String.valueOf(request.getContentLength()) }, { "CONTENT_TYPE",
request.getContentType() }, { "DOCUMENT_ROOT", getServletContext().getRealPath("/") }, {
"PATH_INFO", request.getPathInfo() }, { "PATH_TRANSLATED", request.getPathTranslated() }, {
"QUERY_STRING", request.getQueryString() }, { "REMOTE_ADDR", request.getRemoteAddr() },
{ "REMOTE_HOST", request.getRemoteHost() }, { "REMOTE_USER", request.getRemoteUser() },
{ "REQUEST_METHOD", request.getMethod() }, { "SCRIPT_NAME", request.getServletPath() }, {
"SERVER_NAME", request.getServerName() }, { "SERVER_PORT",
String.valueOf(request.getServerPort()) }, { "SERVER_PROTOCOL", request.getProtocol() }, {
"SERVER_SOFTWARE", getServletContext().getServerInfo() } }; String title = "Servlet Example:
Showing CGI Variables"; out.println(ServletUtilities.headWithTitle(title) + "\n" + "
" + title + "
\n" + "
\n" + "
\n" + "
CGI Variable Name Value"); for(int i=0; i " + varName + " " + varValue); } out.println("
"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...~hall/java/Servlet-Tutorial/hall/ShowCGIVariables.java [29/07/2002 15:03:41]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/SomeServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Servlet template from tutorial on servlets and JSP that
* appears at http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
// Use "out" to send content to browser
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial....edu/~hall/java/Servlet-Tutorial/hall/SomeServlet.java [29/07/2002 15:04:32]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloWorld.java
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
*
*
*/
Very simplistic servlet.
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/HelloWorld.java [29/07/2002 15:04:40]
Hello WWW
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Simple servlet
that generates HTML. * Part of tutorial on servlets and JSP that appears at *
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ * 1999 Marty Hall; may be freely used or adapted.
*/ public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("\n" + "\n"
+ "\n" + "\n" + "
Hello WWW
\n" + ""); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloWWW.java [29/07/2002 15:04:57]
" + title + "
package hall; import javax.servlet.*; import javax.servlet.http.*; /** Some simple time savers. * Part
of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ *
1999 Marty Hall; may be freely used or adapted. */ public class ServletUtilities { public static final
String DOCTYPE = ""; public static String headWithTitle(String title) { return(DOCTYPE + "\n" +
"\n" + "\n"); } /** Read a parameter with the specified name, convert it to an int, and return it. Return
the designated default value if the parameter doesn't exist or if it is an illegal integer format. */ public
static int getIntParameter(HttpServletRequest request, String paramName, int defaultValue) { String
paramString = request.getParameter(paramName); int paramValue; try { paramValue =
Integer.parseInt(paramString); } catch(NumberFormatException nfe) { // Handles null and bad format
paramValue = defaultValue; } return(paramValue); } public static String getCookieValue(Cookie[]
cookies, String cookieName, String defaultValue) { for(int i=0; i<cookies.length; i++) { Cookie
cookie = cookies[i]; if (cookieName.equals(cookie.getName())) return(cookie.getValue()); }
return(defaultValue); } // Approximate values are fine. public static final int
SECONDS_PER_MONTH = 60*60*24*30; public static final int SECONDS_PER_YEAR =
60*60*24*365; }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales...u/~hall/java/Servlet-Tutorial/hall/ServletUtilities.java [29/07/2002 15:05:03]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Complet...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloWWW2.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Simple servlet
that generates HTML. This variation of * HelloWWW uses the ServletUtilities utility class. * Part of
tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ *
1999 Marty Hall; may be freely used or adapted. */ public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); out.println(ServletUtilities.headWithTitle("Hello WWW") + "\n" + "
Hello WWW
\n" + ""); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...u.edu/~hall/java/Servlet-Tutorial/hall/HelloWWW2.java [29/07/2002 15:05:08]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ThreeParams.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
/** Simple servlet that reads three parameters from the form data. * Part of tutorial on servlets and
JSP that appears at * http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ * 1999 Marty Hall; may be
freely used or adapted. */ public class ThreeParams extends HttpServlet { public void
doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String
title = "Reading Three Request Parameters"; out.println(ServletUtilities.headWithTitle(title) + "\n" + "
" + title + "
\n" + "
●
●
●
\n" + "
param1: " + request.getParameter("param1") + "\n" + "
param2: " + request.getParameter("param2") + "\n" + "
param3: " + request.getParameter("param3") + "\n" + "
\n" + ""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial....edu/~hall/java/Servlet-Tutorial/hall/ThreeParams.java [29/07/2002 15:05:15]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowParameters.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
/** Shows all the parameters sent to the servlet via either * GET or POST. Specially marks
parameters that have no values or * multiple values. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class ShowParameters extends
HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); String title = "Reading All Request Parameters";
out.println(ServletUtilities.headWithTitle(title) + "\n" + "
" + title + "
\n" + "
\n" + "
\n" + "
Parameter Name
" + paramName + "\n
Parameter Value(s)"); Enumeration paramNames =
request.getParameterNames(); while(paramNames.hasMoreElements())
{ String paramName = (String)paramNames.nextElement();
out.println("
"); String[] paramValues = request.getParameterValues(paramName); if
(paramValues.length == 1) { String paramValue = paramValues[0]; if
(paramValue.length() == 0) out.print("No Value"); else
out.print(paramValue); } else { out.println("
"); for(int i=0; i" + paramValues[i]); } out.println("
"); } } out.println("
\n"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u/~hall/java/Servlet-Tutorial/hall/ShowParameters.java [29/07/2002 15:05:27]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowRequestHeaders.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
/** Shows all the request headers sent on this * particular request. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class ShowRequestHeaders
extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); String title = "Servlet Example: Showing Request Headers";
out.println(ServletUtilities.headWithTitle(title) + "\n" + "
" + title + "
\n" + "Request Method: " + request.getMethod() + "
\n" + "Request URI: " + request.getRequestURI() + "
\n" + "Request Protocol: " + request.getProtocol() + "
\n" + "
\n" + "
\n" + "
Header Name
Header Value"); Enumeration headerNames =
request.getHeaderNames(); while(headerNames.hasMoreElements())
{ String headerName = (String)headerNames.nextElement();
out.println("
" + headerName);
out.println("
" + request.getHeader(headerName)); } out.println("
\n"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...ll/java/Servlet-Tutorial/hall/ShowRequestHeaders.java [29/07/2002 15:05:51]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos.../www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/SearchEngines.java
package hall;
import
import
import
import
/**
*
*
*
*
*
*
*
*
*
*/
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
Servlet that takes a search string, number of results per
page, and a search engine name, sending the query to
that search engine. Illustrates manipulating
the response status line. It sends a 302 response
(via sendRedirect) if it gets a known search engine,
and sends a 404 response (via sendError) otherwise.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class SearchEngines extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// The URLEncoder changes spaces to "+" signs and other
// non-alphanumeric characters to "%XY", where XY is the
// hex value of the ASCII (or ISO Latin-1) character.
// The getParameter method decodes automatically, but since
// we're just passing this on to another server, we need to
// re-encode it.
String searchString =
URLEncoder.encode(request.getParameter("searchString"));
String numResults =
request.getParameter("numResults");
String searchEngine =
request.getParameter("searchEngine");
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length; i++) {
SearchSpec searchSpec = commonSpecs[i];
if (searchSpec.getName().equals(searchEngine)) {
// encodeURL is just planning ahead in case this servlet
// is ever used in an application that does session tracking.
// If cookies are turned off, session tracking is usually
// accomplished by URL rewriting, so all URLs returned
// by servlets should be sent through encodeURL.
String url =
response.encodeURL(searchSpec.makeURL(searchString,
numResults));
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,
"No recognized search engine specified.");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...du/~hall/java/Servlet-Tutorial/hall/SearchEngines.java [29/07/2002 15:06:56]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo.../www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers.java
package hall;
import
import
import
import
/**
*
*
*
*
*
*
*
*
*
*
*
*
*/
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
Servlet that processes a request to generate n
prime numbers, each with at least m digits.
It performs the calculations in a low-priority background thread,
returning only the results it has found so far. If these results
are not complete, it sends a Refresh header instructing browser
to ask for new results a little while later. It also maintains
a list of a small number of previously calculated prime lists,
to return immediately to anyone who supplies the same n and m
as a recent completed computation.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class PrimeNumbers extends HttpServlet {
private static Vector primeListVector = new Vector();
private static int maxPrimeLists = 30;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int numPrimes =
ServletUtilities.getIntParameter(request, "numPrimes", 50);
int numDigits =
ServletUtilities.getIntParameter(request, "numDigits", 120);
PrimeList primeList =
findPrimeList(primeListVector, numPrimes, numDigits);
if (primeList == null) {
primeList = new PrimeList(numPrimes, numDigits, true);
synchronized(primeListVector) {
if (primeListVector.size() >= maxPrimeLists)
primeListVector.removeElementAt(0);
primeListVector.addElement(primeList);
}
}
Vector currentPrimes = primeList.getPrimes();
int numCurrentPrimes = currentPrimes.size();
int numPrimesRemaining = (numPrimes - numCurrentPrimes);
boolean isLastResult = (numPrimesRemaining == 0);
if (!isLastResult) {
response.setHeader("Refresh", "5");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Some " + numDigits + "-Digit Prime Numbers";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H2 ALIGN=CENTER>" + title + "</H2>\n" +
"<H3>Primes found with " + numDigits +
" or more digits: " + numCurrentPrimes + ".</H3>");
if (isLastResult)
out.println("<B>Done searching.</B>");
else
out.println("<B>Still looking for " + numPrimesRemaining +
" more<BLINK>...</BLINK></B>");
out.println("<OL>");
for(int i=0; i<numCurrentPrimes; i++) {
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...hall/java/Servlet-Tutorial/hall/PrimeNumbers.java (1 of 2) [29/07/2002 15:07:31]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo.../www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers.java
out.println(" <LI>" + currentPrimes.elementAt(i));
}
out.println("</OL>");
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//
//
//
//
//
//
See if there is an existing ongoing or completed calculation with
the same number of primes and length of prime. If so, return
those results instead of starting a new background thread. Keep
this list small so that the Web server doesn't use too much memory.
Synchronize access to the list since there may be multiple simultaneous
requests.
private PrimeList findPrimeList(Vector primeListVector,
int numPrimes,
int numDigits) {
synchronized(primeListVector) {
for(int i=0; i<primeListVector.size(); i++) {
PrimeList primes = (PrimeList)primeListVector.elementAt(i);
if ((numPrimes == primes.numPrimes()) &&
(numDigits == primes.numDigits()))
return(primes);
}
return(null);
}
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...hall/java/Servlet-Tutorial/hall/PrimeNumbers.java (2 of 2) [29/07/2002 15:07:31]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...0JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeList.java
package hall;
import java.util.*;
import java.math.BigInteger;
/**
*
*
*
*
*
*
*/
Creates a Vector of large prime numbers, usually in
a low-priority background thread. Provides a few small
thread-safe access methods.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class PrimeList implements Runnable {
private Vector primes;
private int numPrimes, numDigits;
// Finds numPrimes prime numbers, each of which are
// numDigits long or longer.
public PrimeList(int numPrimes, int numDigits,
boolean runInBackground) {
// Using Vector instead of ArrayList
// to support JDK 1.1 servlet engines
primes = new Vector(numPrimes);
this.numPrimes = numPrimes;
this.numDigits = numDigits;
if (runInBackground) {
Thread t = new Thread(this);
// Use low priority so you don't slow down server.
t.setPriority(Thread.MIN_PRIORITY);
t.start();
} else {
run();
}
}
public void run() {
BigInteger start = Primes.random(numDigits);
for(int i=0; i<numPrimes; i++) {
start = Primes.nextPrime(start);
synchronized(this) {
primes.addElement(start);
}
}
}
public synchronized boolean isDone() {
return(primes.size() == numPrimes);
}
public synchronized Vector getPrimes() {
if (isDone())
return(primes);
else
return((Vector)primes.clone());
}
public int numDigits() {
return(numDigits);
}
public int numPrimes() {
return(numPrimes);
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...edu/~hall/java/Servlet-Tutorial/hall/PrimeList.java (1 of 2) [29/07/2002 15:07:33]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...0JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeList.java
public synchronized int numCalculatedPrimes() {
return(primes.size());
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...edu/~hall/java/Servlet-Tutorial/hall/PrimeList.java (2 of 2) [29/07/2002 15:07:33]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...s%20JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/Primes.java
package hall;
import java.math.BigInteger;
/**
*
*
*
*
*
*/
A few utilities to generate a large random BigInteger,
and find the next prime number above a given BigInteger.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class Primes {
// Note that BigInteger.ZERO was new in JDK 1.2, and 1.1
// code is being used to support the most servlet engines.
private static final BigInteger ZERO = new BigInteger("0");
private static final BigInteger ONE = new BigInteger("1");
private static final BigInteger TWO = new BigInteger("2");
// Likelihood of false prime is less than 1/2^ERR_VAL
// Assumedly BigInteger uses the Miller-Rabin test or
// equivalent, and thus is NOT fooled by Carmichael numbers.
// See section 33.8 of Cormen et al's Introduction to Algorithms
// for details.
private static final int ERR_VAL = 100;
public static BigInteger nextPrime(BigInteger start) {
if (isEven(start))
start = start.add(ONE);
else
start = start.add(TWO);
if (start.isProbablePrime(ERR_VAL))
return(start);
else
return(nextPrime(start));
}
private static boolean isEven(BigInteger n) {
return(n.mod(TWO).equals(ZERO));
}
private
{ new
new
new
new
new
static StringBuffer[] digits =
StringBuffer("0"), new StringBuffer("1"),
StringBuffer("2"), new StringBuffer("3"),
StringBuffer("4"), new StringBuffer("5"),
StringBuffer("6"), new StringBuffer("7"),
StringBuffer("8"), new StringBuffer("9") };
private static StringBuffer randomDigit() {
int index = (int)Math.floor(Math.random() * 10);
return(digits[index]);
}
public static BigInteger random(int numDigits) {
StringBuffer s = new StringBuffer("");
for(int i=0; i<numDigits; i++) {
s.append(randomDigit());
}
return(new BigInteger(s.toString()));
}
public static void main(String[] args) {
int numDigits;
if (args.length > 0)
numDigits = Integer.parseInt(args[0]);
else
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...u.edu/~hall/java/Servlet-Tutorial/hall/Primes.java (1 of 2) [29/07/2002 15:07:34]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...s%20JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/Primes.java
numDigits = 150;
BigInteger start = random(150);
for(int i=0; i<50; i++) {
start = nextPrime(start);
System.out.println("Prime " + i + " = " + start);
}
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto...u.edu/~hall/java/Servlet-Tutorial/hall/Primes.java (2 of 2) [29/07/2002 15:07:34]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...ww.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/LongLivedCookie.java
package hall;
import javax.servlet.http.*;
/**
*
*
*
*
*/
Cookie that persists 1 year. Default Cookie doesn't
persist past current session.
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class LongLivedCookie extends Cookie {
public static final int SECONDS_PER_YEAR = 60*60*24*365;
public LongLivedCookie(String name, String value) {
super(name, value);
setMaxAge(SECONDS_PER_YEAR);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial.../~hall/java/Servlet-Tutorial/hall/LongLivedCookie.java [29/07/2002 15:08:17]
Searching the Web
Searching the Web
Search String:
Results to Show Per Page: 10
Google |
Infoseek |
Lycos |
HotBot
Search
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...du/~hall/java/Servlet-Tutorial/hall/SearchEngines.html [29/07/2002 15:08:29]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/SearchEnginesFrontEnd.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*;
/** Dynamically-generated variation of the * SearchEngines.html front end that uses cookies * to
remember a user's preferences. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class SearchEnginesFrontEnd
extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { Cookie[] cookies = request.getCookies(); String searchString
= ServletUtilities.getCookieValue(cookies, "searchString", "Java Programming"); String numResults
= ServletUtilities.getCookieValue(cookies, "numResults", "10"); String searchEngine =
ServletUtilities.getCookieValue(cookies, "searchEngine", "google");
response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title =
"Searching the Web"; out.println(ServletUtilities.headWithTitle(title) + "\n" + "
Searching the Web
\n" + "\n" + "
\n" + "
\n" + "Search String:\n" + " \""
\n" + "Results to Show Per Page:\n" + " + numResults +
\n" + " \"google\""
" \"lycos\""
\n" + "Google |\n" + " \"infoseek\""
\n" + "Lycos |\n" + " \"hotbot\""
\n" + " \"Search\"
\n" + "Infoseek |\n" +
\n" + "HotBot\n" + "
\n" + "
\n" + "
\n" + "\n" + "\n" + "\n"); } private String checked(String name1, String name2) { if
(name1.equals(name2)) return(" CHECKED"); else return(""); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...java/Servlet-Tutorial/hall/SearchEnginesFrontEnd.java [29/07/2002 15:08:30]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowSession.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*;
import java.util.*; /** Simple example of session tracking. See the shopping * cart example for a
more detailed one. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class ShowSession extends
HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { HttpSession session = request.getSession(true);
response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title =
"Searching the Web"; String heading; Integer accessCount = new Integer(0);; if (session.isNew()) {
heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; Integer oldAccessCount = //
Use getAttribute, not getValue, in version // 2.2 of servlet API.
(Integer)session.getValue("accessCount"); if (oldAccessCount != null) { accessCount = new
Integer(oldAccessCount.intValue() + 1); } } // Use putAttribute in version 2.2 of servlet API.
session.putValue("accessCount", accessCount); out.println(ServletUtilities.headWithTitle(title) + "\n"
+"
" + heading + "
\n" + "
Information on Your Session:
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
Info Type
Value\n" + "
ID\n" + "
" + session.getId() + "\n" + "
Creation Time\n" + "
" + new Date(session.getCreationTime()) + "\n" + "
Time of Last Access\n" + "
" + new Date(session.getLastAccessedTime()) + "\n" + "
Number of Previous Accesses\n" + " " + accessCount + "\n" + "
\n" + ""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial....edu/~hall/java/Servlet-Tutorial/hall/ShowSession.java [29/07/2002 15:09:16]
Using JavaServer Pages
Using JavaServer Pages
Some dynamic content created using various JSP mechanisms:
●
●
●
●
Expression.
Your hostname: <%= request.getRemoteHost() %>.
Scriptlet.
<% out.println("Attached GET data: " + request.getQueryString()); %>
Declaration (plus expression).
<%! private int accessCount = 0; %> Accesses to page since server reboot: <%=
++accessCount %>
Directive (plus expression).
<%@ page import = "java.util.*" %> Current date: <%= new Date() %>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/JspTest.jsp.htm [29/07/2002 15:09:57]
What's New
What's New at JspNews.com
Here is a summary of our four most recent news stories:
1.
2.
3.
4.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial....edu/~hall/java/Servlet-Tutorial/hall/WhatsNew.jsp.htm [29/07/2002 15:10:23]
Reusing JavaBeans in JSP
Reusing JavaBeans in JSP
Message:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial....edu/~hall/java/Servlet-Tutorial/hall/BeanTest.jsp.htm [29/07/2002 15:10:35]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/SimpleBean.java
package hall;
public class SimpleBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/SimpleBean.java [29/07/2002 15:10:35]
Reusing JavaBeans in JSP
Reusing JavaBeans in JSP
Some digit primes:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...edu/~hall/java/Servlet-Tutorial/hall/JspPrimes.jsp.htm [29/07/2002 15:10:46]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo...ww.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/NumberedPrimes.java
package hall;
import java.util.*;
public class NumberedPrimes {
private Vector primes;
private int numPrimes = 15;
private int numDigits = 50;
public String getNumberedList() {
if (primes == null) {
PrimeList newPrimes =
new PrimeList(numPrimes, numDigits, false);
primes = newPrimes.getPrimes();
}
StringBuffer buff = new StringBuffer("<OL>\n");
for(int i=0; i<numPrimes; i++) {
buff.append(" <LI>");
buff.append(primes.elementAt(i));
buff.append("\n");
}
buff.append("</OL>");
return(buff.toString());
}
public int getNumPrimes() {
return(numPrimes);
}
public void setNumPrimes(int numPrimes) {
if (numPrimes != this.numPrimes)
primes = null;
this.numPrimes = numPrimes;
}
public int getNumDigits() {
return(numDigits);
}
public void setNumDigits(int numDigits) {
if (numDigits != this.numDigits)
primes = null;
this.numDigits = numDigits;
}
public Vector getPrimes() {
return(primes);
}
public void setPrimes(Vector primes) {
this.primes = primes;
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u/~hall/java/Servlet-Tutorial/hall/NumberedPrimes.java [29/07/2002 15:10:47]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Size
Parent Directory
10-Jul-2000 14:12
-
BeanTest-NS.gif
19-Nov-1999 16:38
11k
BeanTest.jsp
19-Nov-1999 16:37
1k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
FileRetriever.java
19-Nov-1999 16:37
2k
GetForm.html
19-Nov-1999 16:37
1k
HelloUser.java
19-Nov-1999 16:36
1k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
HelloWWW.java
19-Nov-1999 16:36
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
HelloWorld.java
19-Nov-1999 16:36
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
JspPrimes.html
19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
JspTest-IE.gif
19-Nov-1999 16:38
14k
JspTest.jsp
19-Nov-1999 16:37
1k
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor....jhu.edu/~hall/java/Servlet-Tutorial/hall/index.htm (1 of 3) [29/07/2002 15:10:50]
Index of /~hall/java/Servlet-Tutorial/hall
LongLivedCookie.java
19-Nov-1999 16:36
1k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
PostForm.html
19-Nov-1999 16:37
1k
PrimeList.java
19-Nov-1999 16:37
2k
PrimeListTest.java
19-Nov-1999 16:36
1k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
PrimeNumbers.html
19-Nov-1999 16:37
2k
PrimeNumbers.java
19-Nov-1999 16:37
4k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
Primes.java
19-Nov-1999 16:36
2k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
SearchEngines.html
19-Nov-1999 16:37
1k
SearchEngines.java
19-Nov-1999 16:36
2k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
SearchSpec.java
19-Nov-1999 16:36
1k
ServletUtilities.java
19-Nov-1999 16:36
2k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor....jhu.edu/~hall/java/Servlet-Tutorial/hall/index.htm (2 of 3) [29/07/2002 15:10:50]
Index of /~hall/java/Servlet-Tutorial/hall
ShowParameters-Resul..> 19-Nov-1999 16:38
ShowParameters.java
18k
19-Nov-1999 16:36
2k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
ShowSession.java
19-Nov-1999 16:37
2k
SimpleBean.java
19-Nov-1999 16:37
1k
SomeServlet.java
29-Nov-1999 18:28
1k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
ThreeParams.java
19-Nov-1999 16:36
1k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
WhatsNew.jsp
19-Nov-1999 16:37
1k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor....jhu.edu/~hall/java/Servlet-Tutorial/hall/index.htm (3 of 3) [29/07/2002 15:10:50]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...0JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/JspPrimes.html
Finding Prime Numbers
Number of primes to calculate: 15
Number of digits: 50
Start Calculating
This simplified version of PrimeNumbers.html illustrates the way in which JSP pages can easily reuse
components that are packaged as JavaBeans.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...hu.edu/~hall/java/Servlet-Tutorial/hall/JspPrimes.html [29/07/2002 15:10:51]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Parent Directory
10-Jul-2000 14:12
-
WhatsNew.jsp
19-Nov-1999 16:37
1k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
ThreeParams.java
19-Nov-1999 16:36
1k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
SomeServlet.java
29-Nov-1999 18:28
1k
SimpleBean.java
19-Nov-1999 16:37
1k
ShowSession.java
19-Nov-1999 16:37
2k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
ShowParameters.java
19-Nov-1999 16:36
2k
ShowParameters-Resul..> 19-Nov-1999 16:38
18k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
ShowCGIVariables.java
Size
19-Nov-1999 16:37
2k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
ServletUtilities.java
19-Nov-1999 16:36
2k
SearchSpec.java
19-Nov-1999 16:36
1k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
SearchEngines.java
19-Nov-1999 16:36
Description
2k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-N=D.htm (1 of 3) [29/07/2002 15:11:43]
Index of /~hall/java/Servlet-Tutorial/hall
SearchEngines.html
19-Nov-1999 16:37
1k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
Primes.java
19-Nov-1999 16:36
2k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
PrimeNumbers.java
19-Nov-1999 16:37
4k
PrimeNumbers.html
19-Nov-1999 16:37
2k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeListTest.java
19-Nov-1999 16:36
1k
PrimeList.java
19-Nov-1999 16:37
2k
PostForm.html
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
LongLivedCookie.java
19-Nov-1999 16:36
1k
JspTest.jsp
19-Nov-1999 16:37
1k
JspTest-IE.gif
19-Nov-1999 16:38
14k
JspPrimes.jsp
19-Nov-1999 16:37
1k
JspPrimes.html
19-Nov-1999 16:37
1k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
IncludeTest.jsp
19-Nov-1999 16:37
1k
HelloWorld.java
19-Nov-1999 16:36
1k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-N=D.htm (2 of 3) [29/07/2002 15:11:43]
Index of /~hall/java/Servlet-Tutorial/hall
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloWWW.java
19-Nov-1999 16:36
1k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
HelloUser.java
19-Nov-1999 16:36
1k
GetForm.html
19-Nov-1999 16:37
1k
FileRetriever.java
19-Nov-1999 16:37
2k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
BeanTest.jsp
19-Nov-1999 16:37
1k
BeanTest-NS.gif
19-Nov-1999 16:38
11k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-N=D.htm (3 of 3) [29/07/2002 15:11:43]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Size
Parent Directory
10-Jul-2000 14:12
-
DatabaseUpdate.java
19-Nov-1999 16:36
1k
HelloUser.java
19-Nov-1999 16:36
1k
HelloWWW.java
19-Nov-1999 16:36
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloWorld.java
19-Nov-1999 16:36
1k
Primes.java
19-Nov-1999 16:36
2k
ShowParameters.java
19-Nov-1999 16:36
2k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ThreeParams.java
19-Nov-1999 16:36
1k
PrimeListTest.java
19-Nov-1999 16:36
1k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
SearchEngines.java
19-Nov-1999 16:36
2k
ServletUtilities.java
19-Nov-1999 16:36
2k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
LongLivedCookie.java
19-Nov-1999 16:36
1k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
SearchSpec.java
19-Nov-1999 16:36
1k
FileRetriever.java
19-Nov-1999 16:37
2k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=A.htm (1 of 3) [29/07/2002 15:11:46]
Index of /~hall/java/Servlet-Tutorial/hall
PrimeList.java
19-Nov-1999 16:37
2k
PrimeNumbers.java
19-Nov-1999 16:37
4k
ShowSession.java
19-Nov-1999 16:37
2k
NumberedPrimes.java
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
SimpleBean.java
19-Nov-1999 16:37
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
JspTest.jsp
19-Nov-1999 16:37
1k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
WhatsNew.jsp
19-Nov-1999 16:37
1k
BeanTest.jsp
19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
PostForm.html
19-Nov-1999 16:37
1k
GetForm.html
19-Nov-1999 16:37
1k
JspPrimes.html
19-Nov-1999 16:37
1k
PrimeNumbers.html
19-Nov-1999 16:37
2k
SearchEngines.html
19-Nov-1999 16:37
1k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
ShowParameters-Resul..> 19-Nov-1999 16:38
18k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=A.htm (2 of 3) [29/07/2002 15:11:46]
Index of /~hall/java/Servlet-Tutorial/hall
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
JspTest-IE.gif
19-Nov-1999 16:38
14k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
BeanTest-NS.gif
19-Nov-1999 16:38
11k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
SomeServlet.java
29-Nov-1999 18:28
1k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=A.htm (3 of 3) [29/07/2002 15:11:46]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Size
Parent Directory
10-Jul-2000 14:12
-
SimpleBean.java
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
LongLivedCookie.java
19-Nov-1999 16:36
1k
HelloWorld.java
19-Nov-1999 16:36
1k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
PrimeListTest.java
19-Nov-1999 16:36
1k
WhatsNew.jsp
19-Nov-1999 16:37
1k
BeanTest.jsp
19-Nov-1999 16:37
1k
HelloUser.java
19-Nov-1999 16:36
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
SomeServlet.java
29-Nov-1999 18:28
1k
JspPrimes.html
19-Nov-1999 16:37
1k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
HelloWWW.java
19-Nov-1999 16:36
1k
SearchEngines.html
19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
JspTest.jsp
19-Nov-1999 16:37
1k
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=A.htm (1 of 3) [29/07/2002 15:11:48]
Index of /~hall/java/Servlet-Tutorial/hall
ThreeParams.java
19-Nov-1999 16:36
1k
SearchSpec.java
19-Nov-1999 16:36
1k
GetForm.html
19-Nov-1999 16:37
1k
PostForm.html
19-Nov-1999 16:37
1k
PrimeList.java
19-Nov-1999 16:37
2k
ServletUtilities.java
19-Nov-1999 16:36
2k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
FileRetriever.java
19-Nov-1999 16:37
2k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
ShowParameters.java
19-Nov-1999 16:36
2k
PrimeNumbers.html
19-Nov-1999 16:37
2k
Primes.java
19-Nov-1999 16:36
2k
SearchEngines.java
19-Nov-1999 16:36
2k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
ShowSession.java
19-Nov-1999 16:37
2k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeNumbers.java
19-Nov-1999 16:37
4k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
BeanTest-NS.gif
19-Nov-1999 16:38
11k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=A.htm (2 of 3) [29/07/2002 15:11:48]
Index of /~hall/java/Servlet-Tutorial/hall
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
JspTest-IE.gif
19-Nov-1999 16:38
14k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
ShowParameters-Resul..> 19-Nov-1999 16:38
18k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=A.htm (3 of 3) [29/07/2002 15:11:48]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Size
Parent Directory
10-Jul-2000 14:12
-
BeanTest-NS.gif
19-Nov-1999 16:38
11k
BeanTest.jsp
19-Nov-1999 16:37
1k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
FileRetriever.java
19-Nov-1999 16:37
2k
GetForm.html
19-Nov-1999 16:37
1k
HelloUser.java
19-Nov-1999 16:36
1k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
HelloWWW.java
19-Nov-1999 16:36
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
HelloWorld.java
19-Nov-1999 16:36
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
JspPrimes.html
19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
JspTest-IE.gif
19-Nov-1999 16:38
14k
JspTest.jsp
19-Nov-1999 16:37
1k
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-D=A.htm (1 of 3) [29/07/2002 15:11:50]
Index of /~hall/java/Servlet-Tutorial/hall
LongLivedCookie.java
19-Nov-1999 16:36
1k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
PostForm.html
19-Nov-1999 16:37
1k
PrimeList.java
19-Nov-1999 16:37
2k
PrimeListTest.java
19-Nov-1999 16:36
1k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
PrimeNumbers.html
19-Nov-1999 16:37
2k
PrimeNumbers.java
19-Nov-1999 16:37
4k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
Primes.java
19-Nov-1999 16:36
2k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
SearchEngines.html
19-Nov-1999 16:37
1k
SearchEngines.java
19-Nov-1999 16:36
2k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
SearchSpec.java
19-Nov-1999 16:36
1k
ServletUtilities.java
19-Nov-1999 16:36
2k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-D=A.htm (2 of 3) [29/07/2002 15:11:50]
Index of /~hall/java/Servlet-Tutorial/hall
ShowParameters-Resul..> 19-Nov-1999 16:38
ShowParameters.java
18k
19-Nov-1999 16:36
2k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
ShowSession.java
19-Nov-1999 16:37
2k
SimpleBean.java
19-Nov-1999 16:37
1k
SomeServlet.java
29-Nov-1999 18:28
1k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
ThreeParams.java
19-Nov-1999 16:36
1k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
WhatsNew.jsp
19-Nov-1999 16:37
1k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-D=A.htm (3 of 3) [29/07/2002 15:11:50]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/BeanTest-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/BeanTest-NS.gif [29/07/2002 15:11:51]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/BigIntegerFormatter.java
package hall;
import java.math.*;
public class BigIntegerFormatter {
public static String format(String bigIntString) {
int origLength = bigIntString.length();
int numCommas = (origLength - 1) / 3;
int formattedLength = origLength + numCommas;
StringBuffer buff = new StringBuffer(formattedLength);
buff.setLength(formattedLength);
int index1 = origLength-1;
int index2 = formattedLength-1;
while(index1 >= 0) {
buff.setCharAt(index2--, bigIntString.charAt(index1--));
if (index1 < 0)
break;
buff.setCharAt(index2--, bigIntString.charAt(index1--));
if (index1 < 0)
break;
buff.setCharAt(index2--, bigIntString.charAt(index1--));
if (index1 < 0)
break;
if (index1 >= 0)
buff.setCharAt(index2--, ',');
}
return(buff.toString());
}
public static String format(Object bigInt) {
return(format(bigInt.toString()));
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...all/java/Servlet-Tutorial/hall/BigIntegerFormatter.java [29/07/2002 15:11:52]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos....edu/~hall/java/Servlet-Tutorial/hall/CustomizedSearchEngines-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria.../Servlet-Tutorial/hall/CustomizedSearchEngines-IE.gif [29/07/2002 15:11:53]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...all/java/Servlet-Tutorial/hall/CustomizedSearchEngines-Initial-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...t-Tutorial/hall/CustomizedSearchEngines-Initial-NS.gif [29/07/2002 15:11:54]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...all/java/Servlet-Tutorial/hall/CustomizedSearchEngines-Result-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...t-Tutorial/hall/CustomizedSearchEngines-Result-NS.gif [29/07/2002 15:11:56]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...hu.edu/~hall/java/Servlet-Tutorial/hall/CustomizedSearchEngines.java
package hall;
import
import
import
import
/**
*
*
*
*
*
*
*
*
*/
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
A varition of the SearchEngine servlet that uses
cookies to remember users choices. These values
are then used by the SearchEngineFrontEnd servlet
to create the form-based front end with these
choices preset.
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class CustomizedSearchEngines extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String searchString = request.getParameter("searchString");
Cookie searchStringCookie =
new LongLivedCookie("searchString", searchString);
response.addCookie(searchStringCookie);
searchString = URLEncoder.encode(searchString);
String numResults = request.getParameter("numResults");
Cookie numResultsCookie =
new LongLivedCookie("numResults", numResults);
response.addCookie(numResultsCookie);
String searchEngine = request.getParameter("searchEngine");
Cookie searchEngineCookie =
new LongLivedCookie("searchEngine", searchEngine);
response.addCookie(searchEngineCookie);
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length; i++) {
SearchSpec searchSpec = commonSpecs[i];
if (searchSpec.getName().equals(searchEngine)) {
String url =
searchSpec.makeURL(searchString, numResults);
response.sendRedirect(url);
return;
}
}
response.sendError(response.SC_NOT_FOUND,
"No recognized search engine specified.");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...va/Servlet-Tutorial/hall/CustomizedSearchEngines.java [29/07/2002 15:11:57]
Servlet Example: Reading Form Values\n" + "
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DatabaseUpdate
extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); out.println("\n" + "\n" + "\n" + "
Servlet Example: Reading Form Values
\n" + "Username: " + request.getParameter("User") + "
\n" + "Passwrd: " + request.getParameter("Password") + "
\n" + "\n" + ""); } public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u/~hall/java/Servlet-Tutorial/hall/DatabaseUpdate.java [29/07/2002 15:11:58]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...P/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/FileRetriever.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Demonstrates a
servlet translating paths and reading files. *
* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/ServletTutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class FileRetriever extends
HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { String path = getServletContext().getRealPath("/"); String file =
request.getParameter("file"); if (file == null) sendError(response, "You must supply a 'file'
parameter."); else file = path + file; try { response.setContentType("text/html"); PrintWriter out =
response.getWriter(); String title = "Reading Files from Servlets";
out.println(ServletUtilities.headWithTitle(title) + "\n" + "
" + title + "
\n" + "
" + file + "
\n" + "");
BufferedInputStream in =
new BufferedInputStream(new FileInputStream(file));
int nextByte;
while((nextByte = in.read()) != -1)
out.write(nextByte);
in.close();
out.println(""); } catch(FileNotFoundException fnfe) { sendError(response, "No such
file in: " + path); } catch(IOException ioe) { sendError(response, "IOException: " + ioe); } } public
void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException { doGet(request, response); } private void sendError(HttpServletResponse response,
String message) throws IOException { response.sendError(response.SC_NOT_FOUND, message); }
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...edu/~hall/java/Servlet-Tutorial/hall/FileRetriever.java [29/07/2002 15:11:59]
A Sample FORM using GET
A Sample FORM using GET
Item Number:
Quantity:
Price Each: $
First Name:
Last Name:
Middle Initial:
Shipping Address:
Credit Card:
Visa
Master Card
American Express
Discover
Java SmartCard
Credit Card Number:
Repeat Credit Card Number:
Submit Order
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/GetForm.html [29/07/2002 15:12:01]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...0JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloUser.java
package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class
HelloUser extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter
out = response.getWriter(); String user = request.getParameter("User"); String greeting = "Hello "; if
(user != null) greeting = greeting + user; out.println(ServletUtilities.headWithTitle(greeting) + "
" + greeting + "
"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doGet(request, response); } }
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloUser.java [29/07/2002 15:12:01]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Complet...P/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloWWW-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria....edu/~hall/java/Servlet-Tutorial/hall/HelloWWW-NS.gif [29/07/2002 15:12:02]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...P/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/HelloWorld-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale....edu/~hall/java/Servlet-Tutorial/hall/HelloWorld-IE.gif [29/07/2002 15:12:03]
Testing the include directive
Testing the include directive
Here are the contents of Item1.html:
<%@ include file="/news/Item1.txt" %>
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...du/~hall/java/Servlet-Tutorial/hall/IncludeTest.jsp.htm [29/07/2002 15:12:03]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/JspPrimes-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...u.edu/~hall/java/Servlet-Tutorial/hall/JspPrimes-IE.gif [29/07/2002 15:12:04]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...20JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/JspTest-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...jhu.edu/~hall/java/Servlet-Tutorial/hall/JspTest-IE.gif [29/07/2002 15:12:05]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/NumberedPrimesTest.java
package hall;
public class NumberedPrimesTest {
public static void main(String[] args) {
NumberedPrimes primes = new NumberedPrimes();
primes.setNumPrimes(15);
primes.setNumDigits(50);
System.out.println("15 primes, 50 digits:\n" + primes.getNumberedList());
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...all/java/Servlet-Tutorial/hall/NumberedPrimesTest.java [29/07/2002 15:12:14]
A Sample FORM using POST
A Sample FORM using POST
Item Number:
Quantity:
Price Each: $
First Name:
Last Name:
Middle Initial:
Shipping Address:
Credit Card:
Visa
Master Card
American Express
Discover
Java SmartCard
Credit Card Number:
Repeat Credit Card Number:
Submit Order
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...jhu.edu/~hall/java/Servlet-Tutorial/hall/PostForm.html [29/07/2002 15:12:16]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...P/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeListTest.java
public class PrimeListTest {
public static void main(String[] args) {
int numDigits;
if (args.length > 0)
numDigits = Integer.parseInt(args[0]);
else
numDigits = 150;
PrimeList primes = new PrimeList(50, numDigits, true);
do {
try { Thread.sleep(1000); }
catch(InterruptedException ie) {}
System.out.println("Number of " + numDigits +
" length primes found: " +
primes.numCalculatedPrimes());
} while(!primes.isDone());
System.out.println("Number of " + numDigits +
" length primes found: " +
primes.numCalculatedPrimes());
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...edu/~hall/java/Servlet-Tutorial/hall/PrimeListTest.java [29/07/2002 15:12:17]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo...hall/java/Servlet-Tutorial/hall/PrimeNumbers-Backup-No-Content.java
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
/**
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class PrimeNumbers extends HttpServlet {
private static PrimeList primeList;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
log("Entered PrimeNumbers!");
int numPrimes =
ServletUtilities.getIntParameter(request, "numPrimes", 50);
int numDigits =
ServletUtilities.getIntParameter(request, "numDigits", 120);
int numPrimesPrinted =
ServletUtilities.getIntParameter(request, "numPrimesPrinted", -1);
if (numPrimesPrinted > numPrimes)
numPrimesPrinted = 0;
if ((numPrimesPrinted == -1) || (primeList == null)) {
primeList = new PrimeList(numPrimes, numDigits, true);
}
Vector currentPrimes = primeList.getPrimes();
int numCurrentPrimes = currentPrimes.size();
if (true || (numPrimesPrinted < numCurrentPrimes))
buildPrimeTable(response, numPrimes, numDigits,
currentPrimes, numCurrentPrimes);
else {
String url = "http://hallmr1/servlet/hall.PrimeNumbers?" +
"numPrimes=" + numPrimes +
"&numDigits=" + numDigits +
"&numPrimesPrinted=" + currentPrimes.size();
response.setHeader("Refresh", "5; URL=" + url);
response.setHeader("Retry-After", "5");
response.setHeader("Location", url);
response.setStatus(response.SC_NO_CONTENT);
//response.setStatus(response.SC_RESET_CONTENT);
}
}
public void buildPrimeTable(HttpServletResponse response,
int numPrimes,
int numDigits,
Vector currentPrimes,
int numCurrentPrimes)
throws IOException {
int numPrimesRemaining = (numPrimes - numCurrentPrimes);
boolean isLastResult = (numPrimesRemaining == 0);
if (!isLastResult) {
// TO DO: How to build URL to self?
String url = "http://hallmr1/servlet/hall.PrimeNumbers?" +
"numPrimes=" + numPrimes +
"&numDigits=" + numDigits +
"&numPrimesPrinted=" + currentPrimes.size();
response.setHeader("Refresh", "5; URL=" + url);
}
response.setContentType("text/html");
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tu...utorial/hall/PrimeNumbers-Backup-No-Content.java (1 of 2) [29/07/2002 15:12:18]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo...hall/java/Servlet-Tutorial/hall/PrimeNumbers-Backup-No-Content.java
PrintWriter out = response.getWriter();
String title = "Some " + numDigits + "-Digit Prime Numbers (back to 204)";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H2 ALIGN=CENTER>" + title + "</H1>\n" +
"<H3>Primes found with " + numDigits +
" or more digits: " + numCurrentPrimes + ".</H3>");
if (isLastResult)
out.println("<B>Done searching.</B>");
else
out.println("<B>Still looking for " + numPrimesRemaining +
" more<BLINK>...</BLINK></B>");
out.println("<OL>");
for(int i=0; i<numCurrentPrimes; i++) {
out.println(" <LI>" + currentPrimes.elementAt(i));
}
out.println("</OL>");
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tu...utorial/hall/PrimeNumbers-Backup-No-Content.java (2 of 2) [29/07/2002 15:12:18]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...pl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers-Backup.java
package hall;
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.util.*;
/**
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 1999 Marty Hall; may be freely used or adapted.
*/
public class PrimeNumbers extends HttpServlet {
private static PrimeList primeList;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
log("Entered PrimeNumbers!");
int numPrimes =
ServletUtilities.getIntParameter(request, "numPrimes", 50);
int numDigits =
ServletUtilities.getIntParameter(request, "numDigits", 120);
int numPrimesPrinted =
ServletUtilities.getIntParameter(request, "numPrimesPrinted", -1);
if (numPrimesPrinted > numPrimes)
numPrimesPrinted = 0;
if ((numPrimesPrinted == -1) || (primeList == null)) {
primeList = new PrimeList(numPrimes, numDigits, true);
}
Vector currentPrimes = primeList.getPrimes();
int numCurrentPrimes = currentPrimes.size();
// TO DO: How to build URL to self?
if (numPrimes > numPrimesPrinted) {
response.setHeader("Refresh",
"5; URL=http://hallmr1/servlet/hall.PrimeNumbers?" +
"numPrimes=" + numPrimes +
"&numDigits=" + numDigits +
"&numPrimesPrinted=" + currentPrimes.size());
}
if (numPrimesPrinted < numCurrentPrimes)
buildPrimeTable(response, numPrimes, numDigits,
currentPrimes, numCurrentPrimes);
else
response.setStatus(response.SC_NO_CONTENT);
}
public void buildPrimeTable(HttpServletResponse response,
int numPrimes,
int numDigits,
Vector currentPrimes,
int numCurrentPrimes)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Some " + numDigits + "-Digit Prime Numbers";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H2 ALIGN=CENTER>" + title + "</H1>\n" +
"<H3>Found " + numCurrentPrimes +
" prime numbers with " + numDigits +
" or more digits.</H3>");
int numPrimesRemaining = numPrimes - numCurrentPrimes;
if (numPrimesRemaining == 0)
out.println("<B>Done searching.</B>");
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...va/Servlet-Tutorial/hall/PrimeNumbers-Backup.java (1 of 2) [29/07/2002 15:12:19]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...pl.jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers-Backup.java
else
out.println("<B>Still looking for " + numPrimesRemaining +
" more<BLINK>...</BLINK></B>");
out.println("<OL>");
for(int i=0; i<numCurrentPrimes; i++) {
out.println(" <LI>" + currentPrimes.elementAt(i));
}
out.println("</OL>");
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tut...va/Servlet-Tutorial/hall/PrimeNumbers-Backup.java (2 of 2) [29/07/2002 15:12:19]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/....jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers-Initial-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale.../java/Servlet-Tutorial/hall/PrimeNumbers-Initial-IE.gif [29/07/2002 15:12:20]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers-Result1-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...java/Servlet-Tutorial/hall/PrimeNumbers-Result1-IE.gif [29/07/2002 15:12:20]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...jhu.edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers-Result2-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...java/Servlet-Tutorial/hall/PrimeNumbers-Result2-IE.gif [29/07/2002 15:12:21]
Finding Large Prime Numbers
Finding Large Prime Numbers
Number of primes to calculate: 25
Number of digits: 150
Start Calculating
This example illustrates two capabilities of Java servlets:
●
●
The ability of servlets to maintain persistent state. In this case, the servlet starts a lowpriority thread to calculate prime numbers, and returns a page showing initial results.
Whenever the client asks for an update, it returns the new results. It also maintains a set of the
30 most recently calculated results, to return immediately if a client asks for something already
computed.
The ability of servlets to manipulate HTTP response headers. In this case, it sends
Refresh headers if the prime calculations are not yet complete, along with a page showing
the intermediate results.
This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely
available for unrestricted use. Created for for work in the Research and Technology Development
Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins PartTime MS Program in Computer Science, and for various industry seminars and on-site Java short
courses.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...edu/~hall/java/Servlet-Tutorial/hall/PrimeNumbers.html [29/07/2002 15:12:22]
Reusing JavaBeans in JSP
Reusing JavaBeans in JSP
Primes:
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial.../~hall/java/Servlet-Tutorial/hall/PrimeNumbers.jsp.htm [29/07/2002 15:12:23]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...jhu.edu/~hall/java/Servlet-Tutorial/hall/SearchEngines-Initial-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriale...java/Servlet-Tutorial/hall/SearchEngines-Initial-NS.gif [29/07/2002 15:12:23]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/....jhu.edu/~hall/java/Servlet-Tutorial/hall/SearchEngines-Result-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...java/Servlet-Tutorial/hall/SearchEngines-Result-NS.gif [29/07/2002 15:12:24]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...hu.edu/~hall/java/Servlet-Tutorial/hall/SearchEnginesFrontEnd-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...va/Servlet-Tutorial/hall/SearchEnginesFrontEnd-IE.gif [29/07/2002 15:12:25]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...JSP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/SearchSpec.java
package hall;
/**
*
*
*
*
*/
Small class that encapsulates how to construct a search string
for a particular search engine.
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
class SearchSpec {
private String name, baseURL, numResultsSuffix;
private static SearchSpec[] commonSpecs =
{ new SearchSpec("google",
"http://www.google.com/search?q=",
"&num="),
new SearchSpec("infoseek",
"http://infoseek.go.com/Titles?qt=",
"&nh="),
new SearchSpec("lycos",
"http://lycospro.lycos.com/cgi-bin/pursuit?query=",
"&maxhits="),
new SearchSpec("hotbot",
"http://www.hotbot.com/?MT=",
"&DC=")
};
public SearchSpec(String name,
String baseURL,
String numResultsSuffix) {
this.name = name;
this.baseURL = baseURL;
this.numResultsSuffix = numResultsSuffix;
}
public String makeURL(String searchString, String numResults) {
return(baseURL + searchString + numResultsSuffix + numResults);
}
public String getName() {
return(name);
}
public static SearchSpec[] getCommonSpecs() {
return(commonSpecs);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/SearchSpec.java [29/07/2002 15:12:26]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowCGIVariables-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...all/java/Servlet-Tutorial/hall/ShowCGIVariables-NS.gif [29/07/2002 15:12:26]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...hu.edu/~hall/java/Servlet-Tutorial/hall/ShowParameters-Initial-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...va/Servlet-Tutorial/hall/ShowParameters-Initial-NS.gif [29/07/2002 15:12:27]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...hu.edu/~hall/java/Servlet-Tutorial/hall/ShowParameters-Result-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria...va/Servlet-Tutorial/hall/ShowParameters-Result-NS.gif [29/07/2002 15:12:34]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos/...apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowPreviousQueries.java
package hall;
import
import
import
import
/**
*
*
*
*
*/
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
java.net.*;
<P>
Part of tutorial on servlets and JSP that appears at
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
1999 Marty Hall; may be freely used or adapted.
public class SearchEnginesFrontEnd extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...ll/java/Servlet-Tutorial/hall/ShowPreviousQueries.java [29/07/2002 15:12:35]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowRequestHeaders-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...l/java/Servlet-Tutorial/hall/ShowRequestHeaders-IE.gif [29/07/2002 15:12:36]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowRequestHeaders-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoria.../java/Servlet-Tutorial/hall/ShowRequestHeaders-NS.gif [29/07/2002 15:12:36]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos.../www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ShowSession-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...du/~hall/java/Servlet-Tutorial/hall/ShowSession-NS.gif [29/07/2002 15:12:37]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completos.../www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/ThreeParams-IE.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...du/~hall/java/Servlet-Tutorial/hall/ThreeParams-IE.gif [29/07/2002 15:12:38]
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutoriales%20Completo...SP/www.apl.jhu.edu/~hall/java/Servlet-Tutorial/hall/WhatsNew-NS.gif
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial...u.edu/~hall/java/Servlet-Tutorial/hall/WhatsNew-NS.gif [29/07/2002 15:12:38]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Parent Directory
10-Jul-2000 14:12
-
SomeServlet.java
29-Nov-1999 18:28
1k
My-Style-Sheet.css
19-Nov-1999 16:38
1k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
BeanTest-NS.gif
19-Nov-1999 16:38
11k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
JspTest-IE.gif
19-Nov-1999 16:38
14k
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowParameters-Resul..> 19-Nov-1999 16:38
18k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
ThreeParams-IE.gif
10k
19-Nov-1999 16:38
Size
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=D.htm (1 of 3) [29/07/2002 15:13:14]
Index of /~hall/java/Servlet-Tutorial/hall
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
SearchEngines.html
19-Nov-1999 16:37
1k
PrimeNumbers.html
19-Nov-1999 16:37
2k
JspPrimes.html
19-Nov-1999 16:37
1k
GetForm.html
19-Nov-1999 16:37
1k
PostForm.html
19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
BeanTest.jsp
19-Nov-1999 16:37
1k
WhatsNew.jsp
19-Nov-1999 16:37
1k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
JspTest.jsp
19-Nov-1999 16:37
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
SimpleBean.java
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
ShowSession.java
19-Nov-1999 16:37
2k
PrimeNumbers.java
19-Nov-1999 16:37
4k
PrimeList.java
19-Nov-1999 16:37
2k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
FileRetriever.java
19-Nov-1999 16:37
2k
SearchSpec.java
19-Nov-1999 16:36
1k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
LongLivedCookie.java
19-Nov-1999 16:36
1k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=D.htm (2 of 3) [29/07/2002 15:13:14]
Index of /~hall/java/Servlet-Tutorial/hall
ServletUtilities.java
19-Nov-1999 16:36
2k
SearchEngines.java
19-Nov-1999 16:36
2k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
PrimeListTest.java
19-Nov-1999 16:36
1k
ThreeParams.java
19-Nov-1999 16:36
1k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ShowParameters.java
19-Nov-1999 16:36
2k
Primes.java
19-Nov-1999 16:36
2k
HelloWorld.java
19-Nov-1999 16:36
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloWWW.java
19-Nov-1999 16:36
1k
HelloUser.java
19-Nov-1999 16:36
1k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tuto....jhu.edu/~hall/java/Servlet-Tutorial/hall/-M=D.htm (3 of 3) [29/07/2002 15:13:14]
Servlet and JSP Tutorial: Table of Contents
CONTENTS
Intro & TOC
Servlet Overview
Setup
First Servlets
Handling FORM Data
Request Headers
CGI Variables
Response Status Codes
Response Headers
Cookies
Session Tracking
JavaServer Pages (JSP)
Servlet/JSP Book. Sun Press book that grew from this tutorial.
Servlet/JSP short courses Public or on-site courses.
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutorial.../~hall/java/Servlet-Tutorial/Servlet-Tutorial-TOC.html [30/07/2002 17:43:05]
Index of /~hall/java/Servlet-Tutorial/hall
Index of /~hall/java/Servlet-Tutorial/hall
Name
Last modified
Size
Parent Directory
10-Jul-2000 14:12
-
CustomizedSearchEngi..> 19-Nov-1999 16:38
34k
CustomizedSearchEngi..> 19-Nov-1999 16:38
33k
ShowCGIVariables-NS.gif 19-Nov-1999 16:38
25k
PrimeNumbers-Result2..> 19-Nov-1999 16:38
24k
SearchEngines-Result..> 19-Nov-1999 16:38
21k
WhatsNew-NS.gif
19-Nov-1999 16:38
21k
JspPrimes-IE.gif
19-Nov-1999 16:38
21k
ShowRequestHeaders-I..> 19-Nov-1999 16:38
20k
ShowParameters-Initi..> 19-Nov-1999 16:38
20k
ShowRequestHeaders-N..> 19-Nov-1999 16:38
20k
ShowParameters-Resul..> 19-Nov-1999 16:38
18k
ShowSession-NS.gif
19-Nov-1999 16:38
14k
JspTest-IE.gif
19-Nov-1999 16:38
14k
SearchEnginesFrontEn..> 19-Nov-1999 16:38
13k
PrimeNumbers-Result1..> 19-Nov-1999 16:38
13k
CustomizedSearchEngi..> 19-Nov-1999 16:38
13k
SearchEngines-Initia..> 19-Nov-1999 16:38
12k
BeanTest-NS.gif
19-Nov-1999 16:38
11k
PrimeNumbers-Initial..> 19-Nov-1999 16:38
10k
ThreeParams-IE.gif
19-Nov-1999 16:38
10k
HelloWWW-NS.gif
19-Nov-1999 16:38
8k
HelloWorld-IE.gif
19-Nov-1999 16:38
6k
Description
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=D.htm (1 of 3) [30/07/2002 17:44:20]
Index of /~hall/java/Servlet-Tutorial/hall
PrimeNumbers.java
19-Nov-1999 16:37
4k
PrimeNumbers-Backup-..> 19-Nov-1999 16:36
3k
SearchEnginesFrontEn..> 19-Nov-1999 16:36
3k
PrimeNumbers-Backup...> 19-Nov-1999 16:36
3k
ShowSession.java
19-Nov-1999 16:37
2k
ShowCGIVariables.java
19-Nov-1999 16:37
2k
SearchEngines.java
19-Nov-1999 16:36
2k
Primes.java
19-Nov-1999 16:36
2k
PrimeNumbers.html
19-Nov-1999 16:37
2k
ShowParameters.java
19-Nov-1999 16:36
2k
CustomizedSearchEngi..> 19-Nov-1999 16:36
2k
FileRetriever.java
19-Nov-1999 16:37
2k
ShowRequestHeaders.java 19-Nov-1999 16:36
2k
ServletUtilities.java
19-Nov-1999 16:36
2k
PrimeList.java
19-Nov-1999 16:37
2k
PostForm.html
19-Nov-1999 16:37
1k
GetForm.html
19-Nov-1999 16:37
1k
SearchSpec.java
19-Nov-1999 16:36
1k
ThreeParams.java
19-Nov-1999 16:36
1k
JspTest.jsp
19-Nov-1999 16:37
1k
NumberedPrimes.java
19-Nov-1999 16:37
1k
DatabaseUpdate.java
19-Nov-1999 16:36
1k
PrimeNumbers.jsp
19-Nov-1999 16:37
1k
BigIntegerFormatter...> 19-Nov-1999 16:37
1k
JspPrimes.jsp
19-Nov-1999 16:37
1k
SearchEngines.html
19-Nov-1999 16:37
1k
HelloWWW.java
19-Nov-1999 16:36
1k
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=D.htm (2 of 3) [30/07/2002 17:44:20]
Index of /~hall/java/Servlet-Tutorial/hall
My-Style-Sheet.css
19-Nov-1999 16:38
1k
JspPrimes.html
19-Nov-1999 16:37
1k
SomeServlet.java
29-Nov-1999 18:28
1k
HelloWWW2.java
19-Nov-1999 16:36
1k
HelloUser.java
19-Nov-1999 16:36
1k
BeanTest.jsp
19-Nov-1999 16:37
1k
WhatsNew.jsp
19-Nov-1999 16:37
1k
PrimeListTest.java
19-Nov-1999 16:36
1k
ShowPreviousQueries...> 19-Nov-1999 16:37
1k
HelloWorld.java
19-Nov-1999 16:36
1k
LongLivedCookie.java
19-Nov-1999 16:36
1k
IncludeTest.jsp
19-Nov-1999 16:37
1k
NumberedPrimesTest.java 19-Nov-1999 16:37
1k
SimpleBean.java
1k
19-Nov-1999 16:37
Apache/1.3.9 Server at www.apl.jhu.edu Port 80
file:///D|/Julian/Datos%20JULIAN/Libros%20y%20Tutor...l.jhu.edu/~hall/java/Servlet-Tutorial/hall/-S=D.htm (3 of 3) [30/07/2002 17:44:20]
Download