Sample Servlets
Contents /
New Features /
Administrator Docs /
Developer Docs /
Index
Here are some sample servlets that run with the
JavaTM Web
ServerTM:
Note: If you're reading this on the java.sun.com site, none of the links on this
page will work. To see what these servlets do, download a copy of the
Java Web Server (with documentation), run it, and enter a URL of the form:
http://server_host_name/system/doc/servlets/sample.html
Simple Servlet
This servlet outputs a simple HTML page.
It illustrates the core concepts behind HTTP servlets.
Try it,
Source
Snoop Servlet
This servlet displays the arguments it receives:
- The URL it thinks was used to invoke it.
- Any initialization parameters passed to the servlet.
- All the data made available to it through standard
servlet request methods, including any parameters.
- When used over HTTPS, it shows the SSL cipher suite
used, as well as any X509 certificate chain presented
by the client.
Try it,
Source
Cookie Counter Servlet
This servlet uses cookies to maintain a persistent counter.
It shows:
- How to use the sun.servlet.util.Cookie programming
interface to get and set cookies.
- Using servlet initialization parameters to make different
instances of the same servlet class behave differently.
- Explicit buffering of servlet response data to ensure
that HTTP connections use "keep alive".
Try it,
Source
Phone Servlet
This servlet loads a list of phone numbers from a
file specified as an initialization parameter. On
request, it will return a particular person's phone number if
their name is specified in the query string. Otherwise, it will
return the entire list.
Try it,
Source
Protected Servlet
This servlet simply outputs an html page.
To demonstrate the basic authentication scheme, this servlet is
protected by an ACL (user jeeves and password jeeves).
Try it,
Source
Date Servlet
This servlet outputs the current date and time in plain text
format, suitable for embedding in an existing HTML document as
a server-side include.
Try it,
Server Side Include Example,
Source
Hello World Servlet
This is another simple servlet which outputs a simple
html document. It is used in the
servlet tutorial.
However, it is not loaded by default when
Java Web Server starts up. To load it, do the following:
1. Save this page in your browser. |
2. Go to the URL http://<server:9090>.
|
3. Log in to the Administration Tool with admin/admin as the
username/password. |
4. Once logged on, select Web Page Service -> Manage -> Servlets ->
(Servlets) Add. |
5. Enter "hello" as the Servlet Name. |
6. Enter "HelloWorldServlet" as the Class Name. |
7. Press the Add Button. |
8. Return to this page. |
Then the servlet can be accessed by the URL
http://<server>/servlet/hello
See the tutorial,
Try it,
Source
Certificate Authority Servlet
This servlet is a relatively advanced example, showing:
- The use of HTML forms with the POST method.
- Use of the ServletUnavailable exception for reporting
fatal initialization time errors.
- Dynamic generation of non-HTML data by a servlet.
- Some relatively complex parsing for the form parameters.
- Integration with the KEYGEN extension in Netscape Navigator.
- How to use the sun.security.x509 package in
JDKTM 1.1
(being augmented by public Java APIs in a later version of the JDK)
to create and sign public key certificates.
- Some of the basic functionality of a Certificate Authority,
such as VeriSign.
The POST method is used both because a relatively large amount of
data is sent to this servlet, and because the GET method is inappropriate
for this kind of application. That is, applications that involve servers
storing data (in this case, the generated certificate) can't safely be
repeated, and GET methods are supposed to be safely repeatable.
This servlet illustrates the kind of significant web-based services
that can be hosted using servlets to update shared data. The class which
implements the servlet will be the gateway into the service, and it will
interact with other servlets and subsystems to do the bulk of the work
requested through the servlet.
Try it,
Source