JavaServer

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:

Try it, Source


Cookie Counter Servlet

This servlet uses cookies to maintain a persistent counter. It shows:

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 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


Top
java-server-feedback@java.sun.com
Copyright © 1997 Sun Microsystems, Inc.
All Rights Reserved.