Java Web Server

Internal Servlets


Contents / New Features / Administrator Docs / Developer Docs / Index

The JavaTM Web ServerTM servlet architecture is very flexible, and the server takes advantage of this by dividing its work among several internal servlets. These are:

NOTE: The Java Web Server also provides a number of useful sample servlets such as a Link Checker, Finger, and Certificate Generator. For more information see Sample Servlets.

File Servlet

The File servlet provides the standard document serving capabilites of Java Web Server. This servlet includes a caching mechanism to speed up response times for frequently accessed files. In addition, it recognizes files that are to be parsed for server side includes and passes them to the SSInclude Servlet.

Invoker Servlet

The purpose of this servlet is to invoke other servlets which are explicitly requested by name, that is, http://<server-host-name>/servlet/<servlet-name>.

SSInclude Servlet (Server Side Includes)

Servlets can be embedded within html documents using the servlet tag. When the server serves any file with a .shtml extension it parses the file for the servlet tag as it writes it out to the client. All text not included in the servlet tag is written to the client directly. When the server detects the servlet tag, it loads the servlet if necessary, invokes that servlet, and sends the output of the servlet to the client at the point where the servlet tag was embedded.

The syntax for the servlet tag is:

<servlet name=ServletName code=ServletCode.class codebase=ServletCodeBase
initParam1=initArg1 initParam2=initArg2 ...>
<param name=param1 value=val1>
<param name=param2 value=val2>
.
.
.
</servlet>

The server first tries to invoke the servlet with the name referenced by the name field. If this fails or if no name is provided it attempts to load the servlet based on the code and codebase fields, and passes any remaining fields within the <servlet> tag to the servlet as initialization arguments. If the server loads the servlet and a name was specified, the server keeps the servlet loaded so that the next time the servlet is accessed it is not reloaded. If no name is specified, the server reloads the servlet each time it is accessed.

Once the server has a handle to the servlet, either by referencing it from the name or loading it, it calls service on the servlet. The name value pairs specified in the html file with the <param> tag are accessible to the servlet using the standard getParameter and getParameters methods on the ServletRequest object passed to the servlet in service.

Everything the servlet writes to ServletResponse.getOutputStream() gets written to the client as part of the document the client requested. The servlet's output appears in the document at the location where the servlet tag was embedded.

Below is a sample servlet, the .shtml file which embeds it, and the file the browser sees when it requests this .shtml file.

/*
 * @(#)TestServlet.java	1.6 96/10/14 
 *
 * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import javax.servlet.*;
import java.io.*;
import java.util.Date;
import java.util.Hashtable;

import sun.server.http.HttpServlet;
import sun.server.http.HttpRequest;
import sun.server.http.HttpResponse;

/**
 * TestServlet Servlet
 *
 * This is a simple servlet to demonstrate server-side includes
 * @author Scott Atwood
 * @version 1.6, 10/14/96
 */
public class TestServlet extends GenericServlet {

    public void init(ServletStub stub) {
	try {
	    super.init(stub);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	System.out.println("TestServlet.init()");
    }

    public void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException
    {
	System.out.println("Test Servlet.service()");
	ServletOutputStream out = res.getOutputStream();
	out.println("init1: "+getInitParameter("init1"));
	out.println("init2: "+getInitParameter("init2"));
	out.println("request params: "+req.getParameters());
    }

    public String getServletInfo() {
        return "Sample server-side includes";
    }

}

The contents of test.shtml:

<h3>Test Servlet</h3>
<servlet code=TestServlet init1=one init2=two>
<param name=foo value=bar>
<param name=foo2 value=bar2>
</servlet>

The file the browser sees when it requests test.shtml:

Test Servlet

init1: one
init2: two
request params: {foo=bar, foo2=bar2}

Admin Servlet

The Admin servlet facilitates administration of the Java Web Server through a GUI front end (the Administration Tool).

CgiServlet

This servlet acts as a gateway for the CGI 1.1 interface. This servlet allows any program that utilizes the CGI 1.1 standard to operate under Java Web Server. For more information on CGI see the NCSA documentation.

ImagemapServlet

This servlet implements server-side imagemaps, utilizing an extension of standard NCSA mapfiles. A server-side imagemap is specfied as follows in an HTML file:
<A HREF="http://<server-host-name>/imagemap/<map-file-name>">
<IMG SRC="<image-file>" ISMAP>
</A>
Imagemap files may reside anywhere an HTML document can reside. Imagemap files are in standard NCSA format (see the NCSA Imagemapping Tutorial for a description). In addition to standard NCSA features, Java Web Server supports the following additions:
Top
java-server-feedback@java.sun.com
Copyright © 1997 Sun Microsystems, Inc.
All Rights Reserved.