A Tutorial on Java Servlets and Java Server Pages JSP 1.0

170
A Tutorial on Java Servlets and Java Server Pages (JSP) 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. 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 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]

Transcript of A Tutorial on Java Servlets and Java Server Pages JSP 1.0

A Tutorial on Java Servlets and Java Server Pages (JSP)

CONTENTSIntro & TOC Servlet Overview Setup First Servlets Handling FORM Data Request Headers

Servlets and JavaServer Pages (JSP) 1.0: A TutorialTable of Contentss s

CGI Variables Response Status Codes Response Headers Cookies Session Trackings

JavaServer Pages (JSP) Servlet/JSP Book. Sun Press book that grew from this tutorial.

s

Servlet/JSP short courses Public or onsite courses.s

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 q What is a Java servlet? q What advantage does a Java servlet have over a traditional CGI program? q What is JSP? q What are the advantages of JSP? Getting Started: Installation and Setup q Obtaining and Installing the Servlet and JSP Classes q Getting a ServletCapable Web Server or Servlet Engine First Servlets

s

s

s

Accessing the Standard CGI Variables q The CGI Variables, Their Meaning, and the Servlet Equivalent q Example: Making a Table of All CGI Variables Generating the Response: HTTP Status Codes q Overview: Status Codes and Messages q Setting Status Codes from Servlets q HTTP 1.1 Status Codes and Their Meanings q Example: A Search Engine Front End Generating the Response: HTTP Response Headers q Overview q Common Response Headers and Their Meaning q 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)

s

s

Basic Servlet Structure q A Simple Servlet Generating Plain Text q Compiling and Invoking the Servlet q A Simple Servlet Generating HTML q Some Simple HTML Utilities for Servlets Processing the Request: Form Data q Introduction (Format, URLencoding, GET, POST) q Example: Reading Specific Parameters q Example: Making Table of All Parameters Processing the Request: HTTP Request Headers q Common Request Headers and Their Meaning q Reading Headers from Servlets q Example: Making Table of All Headersq

s

s

s

Updating Display with New Data. Handling Cookies q Intro: How Cookies Can Be Used (and Abused) q The Servlet Cookie API q Some Cookie Utilities q Example: A Customized Search Engine Interface Session Tracking q Overview of Session Tracking q The Servlet Session Tracking API q Example JavaServer Pages (JSP) q JSP Overview q JSP Syntax Summary q Template Text (Static HTML) q JSP Scripting Elements: Expressions, Scriptlets, and Declarations q JSP Directives q Example using JSP Scripting Elements and Directives q Predefined Variables q JSP Actions q JSP Comments and Character Escaping Conventions q Creating Custom JSP Tag Libraries

This page is part of my Tutorial on Servlets and JSP. 1999 Martyfile:///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 TutorialTable of Contentss s

s

s

s

s

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 q What is a Java servlet? q What advantage does a Java servlet have over a traditional CGI program? q What is JSP? q What are the advantages of JSP? Getting Started: Installation and Setup q Obtaining and Installing the Servlet and JSP Classes q Getting a Servlet-Capable Web Server or Servlet Engine First Servlets q Basic Servlet Structure q A Simple Servlet Generating Plain Text q Compiling and Invoking the Servlet q A Simple Servlet Generating HTML q Some Simple HTML Utilities for Servlets Processing the Request: Form Data q Introduction (Format, URLencoding, GET, POST) q Example: Reading Specific Parameters q Example: Making Table of All

s

s

s

s

s

Accessing the Standard CGI Variables q The CGI Variables, Their Meaning, and the Servlet Equivalent q Example: Making a Table of All CGI Variables Generating the Response: HTTP Status Codes q Overview: Status Codes and Messages q Setting Status Codes from Servlets q HTTP 1.1 Status Codes and Their Meanings q Example: A Search Engine Front End Generating the Response: HTTP Response Headers q Overview q Common Response Headers and Their Meaning q Example: Starting a Long-Running Calculation, Showing Partial Results, and Periodically Updating Display with New Data. Handling Cookies q Intro: How Cookies Can Be Used (and Abused) q The Servlet Cookie API q Some Cookie Utilities q Example: A Customized Search Engine Interface Session Tracking q Overview of Session Tracking q The Servlet Session Tracking API q 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

s

Parameters Processing the Request: HTTP Request Headers q Common Request Headers and Their Meaning q Reading Headers from Servlets q Example: Making Table of All Headers

s

JavaServer Pages (JSP) q JSP Overview q JSP Syntax Summary q Template Text (Static HTML) q JSP Scripting Elements: Expressions, Scriptlets, and Declarations q JSP Directives q Example using JSP Scripting Elements and Directives q Predefined Variables q JSP Actions q JSP Comments and Character Escaping Conventions q 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 Overview1. 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: q 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. q 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. q 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 :-). q 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 databasefile:///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

q

q

q

q

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:

Welcome to Our Store Welcome to Our Store Welcome, To access your account settings, click here. Regular HTML for all the rest of the on-line store's Web page.

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

q

q

q

q

q

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 Setup1. 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 KitsYour 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.jarAdd ":$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/NTset 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 ServerYour 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 youll 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: q 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/. q 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. q 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, OReillys 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/. q New Atlantas 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, butfile:///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

q

q

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/. Suns 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 Servlets1. 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 StructureHere'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 TextHere 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.javaYou 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 ServletNote 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 ServletWith 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 HTMLMost 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.javaYou 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("\n" + "\n" + "Hello WWW\n" + "\n" + "Hello WWW\n" + ""); } }

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 UtilitiesIt 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 = ""; public static String headWithTitle(String title) { return(DOCTYPE + "\n" + "\n" + "" + title + "\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") + "\n" + "Hello WWW\n" + ""); } }

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 Data1. Introduction 2. Example: Reading Three Parameters 3. Example: Listing All Form Data 4. Servlet and JSP Tutorial: Top

1. IntroductionIf 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 ParametersHere'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.javaYou 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) + "\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); } }

+

+ + +

2.2 ThreeParams Output

3. Example: Listing All Form DataHere'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.javaYou 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) + "\n" + "" + title + "\n" + "\n" + "\n" + "Parameter NameParameter Value(s)"); Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.println("" + paramName + "\n"); 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= 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) + "\n" + "" + title + "\n" + "Primes found with " + numDigits + " or more digits: " + numCurrentPrimes + "."); if (isLastResult) out.println("

Done searching."); else out.println("Still looking for " + numPrimesRemaining + " more..."); out.println(""); for(int i=0; i \' \" %\> in an attribute. 19-Nov-1999 16:37 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:36 DatabaseUpdate.java FileRetriever.java GetForm.html HelloUser.java HelloWWW-NS.gif HelloWWW.java HelloWWW2.java HelloWorld-IE.gif HelloWorld.java IncludeTest.jsp JspPrimes-IE.gif JspPrimes.html JspPrimes.jsp JspTest-IE.gif JspTest.jsp 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37

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 My-Style-Sheet.css NumberedPrimes.java

19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:37

1k 1k 1k 1k 1k 2k 1k 3k 3k 10k 13k 24k 2k 4k 1k 2k 12k 21k 1k 2k 13k 3k 1k 2k 25k 2k 20k

NumberedPrimesTest.java 19-Nov-1999 16:37 PostForm.html PrimeList.java PrimeListTest.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeNumbers-Backup...> 19-Nov-1999 16:36 PrimeNumbers-Initial..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 PrimeNumbers.html PrimeNumbers.java PrimeNumbers.jsp Primes.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

SearchEngines-Initia..> 19-Nov-1999 16:38 SearchEngines-Result..> 19-Nov-1999 16:38 SearchEngines.html SearchEngines.java 19-Nov-1999 16:37 19-Nov-1999 16:36

SearchEnginesFrontEn..> 19-Nov-1999 16:38 SearchEnginesFrontEn..> 19-Nov-1999 16:36 SearchSpec.java ServletUtilities.java 19-Nov-1999 16:36 19-Nov-1999 16:36

ShowCGIVariables-NS.gif 19-Nov-1999 16:38 ShowCGIVariables.java 19-Nov-1999 16:37

ShowParameters-Initi..> 19-Nov-1999 16:38

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 19-Nov-1999 16:36

18k 2k 1k 20k 20k 2k 14k 2k 1k 1k 10k 1k 21k 1k

ShowPreviousQueries...> 19-Nov-1999 16:37 ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowRequestHeaders.java 19-Nov-1999 16:36 ShowSession-NS.gif ShowSession.java SimpleBean.java SomeServlet.java ThreeParams-IE.gif ThreeParams.java WhatsNew-NS.gif WhatsNew.jsp 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 29-Nov-1999 18:28 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:38 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....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 NumbersNumber of primes to calculate: 15 Number of digits: 50Start 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/hallName Last modified Size Description

Parent Directory WhatsNew.jsp WhatsNew-NS.gif ThreeParams.java ThreeParams-IE.gif SomeServlet.java SimpleBean.java ShowSession.java ShowSession-NS.gif

10-Jul-2000 14:12 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:38 29-Nov-1999 18:28 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:38

1k 21k 1k 10k 1k 1k 2k 14k 2k 20k 20k 1k 2k 18k 20k 2k 25k 2k 1k 3k 13k 2k

ShowRequestHeaders.java 19-Nov-1999 16:36 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowPreviousQueries...> 19-Nov-1999 16:37 ShowParameters.java 19-Nov-1999 16:36

ShowParameters-Resul..> 19-Nov-1999 16:38 ShowParameters-Initi..> 19-Nov-1999 16:38 ShowCGIVariables.java 19-Nov-1999 16:37

ShowCGIVariables-NS.gif 19-Nov-1999 16:38 ServletUtilities.java SearchSpec.java 19-Nov-1999 16:36 19-Nov-1999 16:36

SearchEnginesFrontEn..> 19-Nov-1999 16:36 SearchEnginesFrontEn..> 19-Nov-1999 16:38 SearchEngines.java 19-Nov-1999 16:36

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 21k 12k 2k 1k 4k 2k 24k 13k 10k 3k 3k 1k 2k 1k 1k 1k 1k 1k 1k 14k 1k 1k 21k 1k 1k 6k

SearchEngines-Result..> 19-Nov-1999 16:38 SearchEngines-Initia..> 19-Nov-1999 16:38 Primes.java PrimeNumbers.jsp PrimeNumbers.java PrimeNumbers.html 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37

PrimeNumbers-Result2..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 PrimeNumbers-Initial..> 19-Nov-1999 16:38 PrimeNumbers-Backup...> 19-Nov-1999 16:36 PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeListTest.java PrimeList.java PostForm.html 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37

NumberedPrimesTest.java 19-Nov-1999 16:37 NumberedPrimes.java My-Style-Sheet.css LongLivedCookie.java JspTest.jsp JspTest-IE.gif JspPrimes.jsp JspPrimes.html JspPrimes-IE.gif IncludeTest.jsp HelloWorld.java HelloWorld-IE.gif 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:38

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 HelloWWW.java HelloWWW-NS.gif HelloUser.java GetForm.html FileRetriever.java DatabaseUpdate.java

19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

1k 1k 8k 1k 1k 2k 1k 2k 33k 13k 34k 1k 1k 11k

CustomizedSearchEngi..> 19-Nov-1999 16:36 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 BigIntegerFormatter...> 19-Nov-1999 16:37 BeanTest.jsp BeanTest-NS.gif 19-Nov-1999 16:37 19-Nov-1999 16:38

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/hallName Last modified Size Description

Parent Directory DatabaseUpdate.java HelloUser.java HelloWWW.java HelloWWW2.java HelloWorld.java Primes.java ShowParameters.java

10-Jul-2000 14:12 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36

1k 1k 1k 1k 1k 2k 2k 2k 1k 1k 3k 3k 2k 2k 2k 1k 3k 1k 2k 2k 1k 1k

ShowRequestHeaders.java 19-Nov-1999 16:36 ThreeParams.java PrimeListTest.java 19-Nov-1999 16:36 19-Nov-1999 16:36

PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeNumbers-Backup...> 19-Nov-1999 16:36 SearchEngines.java ServletUtilities.java 19-Nov-1999 16:36 19-Nov-1999 16:36

CustomizedSearchEngi..> 19-Nov-1999 16:36 LongLivedCookie.java 19-Nov-1999 16:36

SearchEnginesFrontEn..> 19-Nov-1999 16:36 SearchSpec.java FileRetriever.java ShowCGIVariables.java 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37

ShowPreviousQueries...> 19-Nov-1999 16:37 BigIntegerFormatter...> 19-Nov-1999 16:37

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 PrimeNumbers.java ShowSession.java NumberedPrimes.java

19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37

2k 4k 2k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 2k 1k 8k 6k 10k 20k 18k 20k 20k 10k 12k 21k

NumberedPrimesTest.java 19-Nov-1999 16:37 SimpleBean.java IncludeTest.jsp JspTest.jsp PrimeNumbers.jsp WhatsNew.jsp BeanTest.jsp JspPrimes.jsp PostForm.html GetForm.html JspPrimes.html PrimeNumbers.html SearchEngines.html HelloWWW-NS.gif HelloWorld-IE.gif ThreeParams-IE.gif 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38

ShowParameters-Initi..> 19-Nov-1999 16:38 ShowParameters-Resul..> 19-Nov-1999 16:38 ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 PrimeNumbers-Initial..> 19-Nov-1999 16:38 SearchEngines-Initia..> 19-Nov-1999 16:38 SearchEngines-Result..> 19-Nov-1999 16:38

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 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 JspTest-IE.gif 19-Nov-1999 16:38

25k 13k 33k 13k 24k 34k 14k 13k 14k 11k 21k 21k 1k 1k

SearchEnginesFrontEn..> 19-Nov-1999 16:38 ShowSession-NS.gif BeanTest-NS.gif JspPrimes-IE.gif WhatsNew-NS.gif My-Style-Sheet.css SomeServlet.java 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38 29-Nov-1999 18:28

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/hallName Last modified Size Description

Parent Directory SimpleBean.java

10-Jul-2000 14:12 19-Nov-1999 16:37

1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k

NumberedPrimesTest.java 19-Nov-1999 16:37 IncludeTest.jsp LongLivedCookie.java HelloWorld.java 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:36

ShowPreviousQueries...> 19-Nov-1999 16:37 PrimeListTest.java WhatsNew.jsp BeanTest.jsp HelloUser.java HelloWWW2.java SomeServlet.java JspPrimes.html My-Style-Sheet.css HelloWWW.java SearchEngines.html JspPrimes.jsp 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:36 29-Nov-1999 18:28 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37

BigIntegerFormatter...> 19-Nov-1999 16:37 PrimeNumbers.jsp DatabaseUpdate.java NumberedPrimes.java JspTest.jsp 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37

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 SearchSpec.java GetForm.html PostForm.html PrimeList.java ServletUtilities.java

19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

1k 1k 1k 1k 2k 2k 2k 2k 2k 2k 2k 2k 2k 2k 2k 3k 3k 3k 4k 6k 8k 10k 10k 11k 12k 13k 13k

ShowRequestHeaders.java 19-Nov-1999 16:36 FileRetriever.java 19-Nov-1999 16:37

CustomizedSearchEngi..> 19-Nov-1999 16:36 ShowParameters.java PrimeNumbers.html Primes.java SearchEngines.java ShowCGIVariables.java ShowSession.java 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37

PrimeNumbers-Backup...> 19-Nov-1999 16:36 SearchEnginesFrontEn..> 19-Nov-1999 16:36 PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeNumbers.java HelloWorld-IE.gif HelloWWW-NS.gif ThreeParams-IE.gif 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38

PrimeNumbers-Initial..> 19-Nov-1999 16:38 BeanTest-NS.gif 19-Nov-1999 16:38

SearchEngines-Initia..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38

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 JspTest-IE.gif ShowSession-NS.gif 19-Nov-1999 16:38 19-Nov-1999 16:38

13k 14k 14k 18k 20k 20k 20k 21k 21k 21k 24k 25k 33k 34k

ShowParameters-Resul..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowParameters-Initi..> 19-Nov-1999 16:38 ShowRequestHeaders-I..> 19-Nov-1999 16:38 JspPrimes-IE.gif WhatsNew-NS.gif 19-Nov-1999 16:38 19-Nov-1999 16:38

SearchEngines-Result..> 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 ShowCGIVariables-NS.gif 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38

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/hallName Last modified Size Description

Parent Directory BeanTest-NS.gif BeanTest.jsp

10-Jul-2000 14:12 19-Nov-1999 16:38 19-Nov-1999 16:37

11k 1k 1k 34k 13k 33k 2k 1k 2k 1k 1k 8k 1k 1k 6k 1k 1k 21k 1k 1k 14k 1k

BigIntegerFormatter...> 19-Nov-1999 16:37 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:36 DatabaseUpdate.java FileRetriever.java GetForm.html HelloUser.java HelloWWW-NS.gif HelloWWW.java HelloWWW2.java HelloWorld-IE.gif HelloWorld.java IncludeTest.jsp JspPrimes-IE.gif JspPrimes.html JspPrimes.jsp JspTest-IE.gif JspTest.jsp 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:38 19-Nov-1999 16:37

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 My-Style-Sheet.css NumberedPrimes.java

19-Nov-1999 16:36 19-Nov-1999 16:38 19-Nov-1999 16:37

1k 1k 1k 1k 1k 2k 1k 3k 3k 10k 13k 24k 2k 4k 1k 2k 12k 21k 1k 2k 13k 3k 1k 2k 25k 2k 20k

NumberedPrimesTest.java 19-Nov-1999 16:37 PostForm.html PrimeList.java PrimeListTest.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeNumbers-Backup...> 19-Nov-1999 16:36 PrimeNumbers-Initial..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 PrimeNumbers.html PrimeNumbers.java PrimeNumbers.jsp Primes.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

SearchEngines-Initia..> 19-Nov-1999 16:38 SearchEngines-Result..> 19-Nov-1999 16:38 SearchEngines.html SearchEngines.java 19-Nov-1999 16:37 19-Nov-1999 16:36

SearchEnginesFrontEn..> 19-Nov-1999 16:38 SearchEnginesFrontEn..> 19-Nov-1999 16:36 SearchSpec.java ServletUtilities.java 19-Nov-1999 16:36 19-Nov-1999 16:36

ShowCGIVariables-NS.gif 19-Nov-1999 16:38 ShowCGIVariables.java 19-Nov-1999 16:37

ShowParameters-Initi..> 19-Nov-1999 16:38

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 19-Nov-1999 16:36

18k 2k 1k 20k 20k 2k 14k 2k 1k 1k 10k 1k 21k 1k

ShowPreviousQueries...> 19-Nov-1999 16:37 ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowRequestHeaders.java 19-Nov-1999 16:36 ShowSession-NS.gif ShowSession.java SimpleBean.java SomeServlet.java ThreeParams-IE.gif ThreeParams.java WhatsNew-NS.gif WhatsNew.jsp 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 29-Nov-1999 18:28 19-Nov-1999 16:38 19-Nov-1999 16:36 19-Nov-1999 16:38 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/-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. 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 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) + "\n" + "" + title + "\n" + "Primes found with " + numDigits + " or more digits: " + numCurrentPrimes + "."); if (isLastResult) out.println("Done searching."); else out.println("Still looking for " + numPrimesRemaining + " more..."); out.println(""); for(int i=0; i 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) + "\n" + "" + title + "\n" + "Found " + numCurrentPrimes + " prime numbers with " + numDigits + " or more digits."); int numPrimesRemaining = numPrimes - numCurrentPrimes; if (numPrimesRemaining == 0) out.println("Done searching.");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("Still looking for " + numPrimesRemaining + " more..."); out.println(""); for(int i=0; i 19-Nov-1999 16:38 JspTest-IE.gif 19-Nov-1999 16:38

CustomizedSearchEngi..> 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 ShowCGIVariables-NS.gif 19-Nov-1999 16:38 SearchEngines-Result..> 19-Nov-1999 16:38 SearchEngines-Initia..> 19-Nov-1999 16:38 PrimeNumbers-Initial..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowParameters-Resul..> 19-Nov-1999 16:38 ShowParameters-Initi..> 19-Nov-1999 16:38 ThreeParams-IE.gif 19-Nov-1999 16:38

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 HelloWWW-NS.gif SearchEngines.html PrimeNumbers.html JspPrimes.html GetForm.html PostForm.html JspPrimes.jsp BeanTest.jsp WhatsNew.jsp PrimeNumbers.jsp JspTest.jsp IncludeTest.jsp SimpleBean.java

19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37

6k 8k 1k 2k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 2k 4k 2k 1k 1k 2k 2k 1k 3k 1k 2k

NumberedPrimesTest.java 19-Nov-1999 16:37 NumberedPrimes.java ShowSession.java PrimeNumbers.java PrimeList.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37

BigIntegerFormatter...> 19-Nov-1999 16:37 ShowPreviousQueries...> 19-Nov-1999 16:37 ShowCGIVariables.java FileRetriever.java SearchSpec.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

SearchEnginesFrontEn..> 19-Nov-1999 16:36 LongLivedCookie.java 19-Nov-1999 16:36

CustomizedSearchEngi..> 19-Nov-1999 16:36

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 SearchEngines.java

19-Nov-1999 16:36 19-Nov-1999 16:36

2k 2k 3k 3k 1k 1k 2k 2k 2k 1k 1k 1k 1k 1k

PrimeNumbers-Backup...> 19-Nov-1999 16:36 PrimeNumbers-Backup-..> 19-Nov-1999 16:36 PrimeListTest.java ThreeParams.java 19-Nov-1999 16:36 19-Nov-1999 16:36

ShowRequestHeaders.java 19-Nov-1999 16:36 ShowParameters.java Primes.java HelloWorld.java HelloWWW2.java HelloWWW.java HelloUser.java DatabaseUpdate.java 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:36

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

CONTENTSIntro & 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/hallName Last modified Size Description

Parent Directory

10-Jul-2000 14:12

34k 33k 25k 24k 21k 21k 21k 20k 20k 20k 18k 14k 14k 13k 13k 13k 12k 11k 10k 10k 8k 6k

CustomizedSearchEngi..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 ShowCGIVariables-NS.gif 19-Nov-1999 16:38 PrimeNumbers-Result2..> 19-Nov-1999 16:38 SearchEngines-Result..> 19-Nov-1999 16:38 WhatsNew-NS.gif JspPrimes-IE.gif 19-Nov-1999 16:38 19-Nov-1999 16:38

ShowRequestHeaders-I..> 19-Nov-1999 16:38 ShowParameters-Initi..> 19-Nov-1999 16:38 ShowRequestHeaders-N..> 19-Nov-1999 16:38 ShowParameters-Resul..> 19-Nov-1999 16:38 ShowSession-NS.gif JspTest-IE.gif 19-Nov-1999 16:38 19-Nov-1999 16:38

SearchEnginesFrontEn..> 19-Nov-1999 16:38 PrimeNumbers-Result1..> 19-Nov-1999 16:38 CustomizedSearchEngi..> 19-Nov-1999 16:38 SearchEngines-Initia..> 19-Nov-1999 16:38 BeanTest-NS.gif 19-Nov-1999 16:38

PrimeNumbers-Initial..> 19-Nov-1999 16:38 ThreeParams-IE.gif HelloWWW-NS.gif HelloWorld-IE.gif 19-Nov-1999 16:38 19-Nov-1999 16:38 19-Nov-1999 16:38

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 3k 3k 3k 2k 2k 2k 2k 2k 2k 2k 2k 2k 2k 2k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k

PrimeNumbers-Backup-..> 19-Nov-1999 16:36 SearchEnginesFrontEn..> 19-Nov-1999 16:36 PrimeNumbers-Backup...> 19-Nov-1999 16:36 ShowSession.java ShowCGIVariables.java SearchEngines.java Primes.java PrimeNumbers.html ShowParameters.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:36

CustomizedSearchEngi..> 19-Nov-1999 16:36 FileRetriever.java 19-Nov-1999 16:37

ShowRequestHeaders.java 19-Nov-1999 16:36 ServletUtilities.java PrimeList.java PostForm.html GetForm.html SearchSpec.java ThreeParams.java JspTest.jsp NumberedPrimes.java DatabaseUpdate.java PrimeNumbers.jsp 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36 19-Nov-1999 16:37

BigIntegerFormatter...> 19-Nov-1999 16:37 JspPrimes.jsp SearchEngines.html HelloWWW.java 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

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 JspPrimes.html SomeServlet.java HelloWWW2.java HelloUser.java BeanTest.jsp WhatsNew.jsp PrimeListTest.java

19-Nov-1999 16:38 19-Nov-1999 16:37 29-Nov-1999 18:28 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37 19-Nov-1999 16:37 19-Nov-1999 16:36

1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k 1k

ShowPreviousQueries...> 19-Nov-1999 16:37 HelloWorld.java LongLivedCookie.java IncludeTest.jsp 19-Nov-1999 16:36 19-Nov-1999 16:36 19-Nov-1999 16:37

NumberedPrimesTest.java 19-Nov-1999 16:37 SimpleBean.java 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]