Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
154
Chapter 7 Generating the Server Response: HTTP Response Headers
WWW Authenticate
This header is always included with a 401 (
Unauthorized
) status code.
It tells the browser what authorization type and realm the client should
supply in its
Authorization
header. Frequently, servlets let pass
word protected Web pages be handled by the Web server's specialized
mechanisms (e.g.,
.htaccess
) rather than handling them directly. For
an example of servlets dealing directly with this header, see Section 4.5
(Restricting Access to Web Pages).
7.3 Persistent Servlet State and
Auto Reloading Pages
Here is an example that lets you ask for a list of some large, randomly chosen
prime numbers. This computation may take some time for very large num
bers (e.g., 150 digits), so the servlet immediately returns initial results but
then keeps calculating, using a low priority thread so that it won't degrade
Web server performance. If the calculations are not complete, the servlet
instructs the browser to ask for a new page in a few seconds by sending it a
Refresh
header.
In addition to illustrating the value of HTTP response headers, this exam
ple shows two other valuable servlet capabilities. First, it shows that the same
servlet can handle multiple simultaneous connections, each with its own
thread. So, while one thread is finishing a calculation for one client, another
client can connect and still see partial results.
Second, this example shows how easy it is for servlets to maintain state
between requests, something that is cumbersome to implement in tradi
tional CGI and many CGI alternatives. Only a single instance of the servlet
is created, and each request simply results in a new thread calling the serv
let's
service
method (which calls
doGet
or
doPost
). So, shared data simply
has to be placed in a regular instance variable (field) of the servlet. Thus,
the servlet can access the appropriate ongoing calculation when the
browser reloads the page and can keep a list of the N most recently
requested results, returning them immediately if a new request specifies
the same parameters as a recent one. Of course, the normal rules that
require authors to synchronize multithreaded access to shared data still
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved