home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / javax / servlet / Servlet.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  7.3 KB  |  165 lines

  1. /*
  2.  * @(#)Servlet.java    1.23 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package javax.servlet;
  23.  
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * This interface is for developing servlets.  A servlet is a body of
  28.  * Java code that is loaded into and runs inside a network service,
  29.  * such as a web server.  It receives and responds to requests from
  30.  * clients.  For example, a client may need information from a
  31.  * database; a servlet can be written that receives the request, gets
  32.  * and processes the data as needed by the client, and then returns it
  33.  * to the client.
  34.  * 
  35.  * <P>All servlets implement this interface.  Servlet writers typically
  36.  * do this by subclassing either GenericServlet, which implements the
  37.  * Servlet interface, or by subclassing GenericServlet's descendent,
  38.  * HttpServlet.  Developers need to directly implement this interface
  39.  * only if their servlets cannot (or choose not to) inherit from
  40.  * GenericServlet or HttpServlet.  For example, RMI or CORBA objects
  41.  * that act as servlets will directly implement this interface.
  42.  *
  43.  * <p>The Servlet interface defines methods to initialize a servlet, to
  44.  * receive and respond to client requests, and to destroy a servlet and
  45.  * its resources.  These are known as life-cycle methods, and are called
  46.  * by the network service in the following manner:
  47.  *
  48.  * <ol>
  49.  * <li>Servlet is created then <b>init</b>ialized.
  50.  * <li>Zero or more <b>service</b> calls from clients are handled
  51.  * <li>Servlet is <b>destroy</b>ed then garbage collected and finalized
  52.  * </ol>
  53.  *
  54.  * Initializing a servlet involves doing any expensive one-time setup,
  55.  * such as loading configuration data from files or starting helper
  56.  * threads.  Service calls from clients are handled using a request and
  57.  * response paradigm.  They rely on the underlying network transport to
  58.  * provide quality of service guarantees, such as reordering,
  59.  * duplication, message integrity, privacy, etc. Destroying a servlet
  60.  * involves undoing any initialization work and synchronizing
  61.  * persistent state with the current in-memory state.
  62.  * 
  63.  * <p>In addition to the life-cycle methods, the Servlet interface
  64.  * provides for a method for the servlet to use to get any startup
  65.  * information, and a method that allows the servlet to return basic
  66.  * information about itself, such as its author, version and copyright.
  67.  *
  68.  * @see GenericServlet
  69.  * @see javax.servlet.http.HttpServlet
  70.  * 
  71.  * @version     1.23, 05/22/97
  72.  * @author      David Connelly
  73.  * @author Pavani Diwanji */
  74.  
  75. public
  76. interface Servlet {
  77.  
  78.     /**
  79.      * Initializes the servlet. The method is called once,
  80.      * automatically, by the network service when it loads the servlet.
  81.      * It is guaranteed to finish before any service requests are
  82.      * accepted.  After initialization, the network service does not
  83.      * call the init method again unless it reloads the servlet after
  84.      * it has unloaded and destroyed it.
  85.      * 
  86.      * <p>The init method should save the ServletConfig object so that
  87.      * it can be returned by the getServletConfig method.  If a fatal
  88.      * initialization error occurs, the init method should throw an
  89.      * appropriate "UnavailableException" exception.  It should never
  90.      * call the method System.exit.
  91.      *
  92.      * @see UnavailableException
  93.      * @see javax.servlet.Servlet#getServletConfig()
  94.      * @param config object containing the servlet's startup
  95.      * configuration and initialization parameters
  96.      * @exception ServletException if a servlet exception has occurred
  97.      */
  98.     public void init(ServletConfig config) throws ServletException;
  99.  
  100.     /**
  101.      * Returns a servlet config object, which contains any
  102.      * initialization parameters and startup configuration for this
  103.      * servlet.  This is the ServletConfig object passed to the init
  104.      * method; the init method should have stored this object so that
  105.      * this method could return it.
  106.      *
  107.      * @see javax.servlet.Servlet#init
  108.      */
  109.     public ServletConfig getServletConfig();
  110.  
  111.     /**
  112.      * Carries out a single request from the client.  The method
  113.      * implements a request and response paradigm.  The request object
  114.      * contains information about the service request, including
  115.      * parameters provided by the client.  The response object is used
  116.      * to return information to the client.  The request and response
  117.      * objects rely on the underlying network transport for quality of
  118.      * service guarantees, such as reordering, duplication, privacy,
  119.      * and authentication.
  120.      *
  121.      * <p>Service requests are not handled until servlet initialization
  122.      * has completed.  Any requests for service that are received
  123.      * during initialization block until it is complete.  Note that
  124.      * servlets typically run inside multi-threaded servers; servers
  125.      * can handle multiple service requests simultaneously.  It is the
  126.      * servlet writer's responsibility to synchronize access to any
  127.      * shared resources, such as network connections or the servlet's
  128.      * class and instance variables.  Information on multi-threaded
  129.      * programming in Java can be found in <a
  130.      * href="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">the
  131.      * Java tutorial on multi-threaded programming</a>.
  132.      *
  133.      * @param req the client's request of the servlet
  134.      * @param res the servlet's response to the client
  135.      * @exception ServletException if a servlet exception has occurred
  136.      * @exception IOException if an I/O exception has occurred
  137.      */
  138.     public void service(ServletRequest req, ServletResponse res)
  139.     throws ServletException, IOException;
  140.  
  141.     /**
  142.      * Returns a string containing information about the servlet, such as
  143.      * its author, version, and copyright.
  144.      */
  145.     public String getServletInfo();
  146.  
  147.     /**
  148.      * Cleans up whatever resources are being held (e.g., memory, file
  149.      * handles, threads) and makes sure that any persistent state is
  150.      * synchronized with the servlet's current in-memory state.  The
  151.      * method is called once, automatically, by the network service
  152.      * when it unloads the servlet. After destroy is run, it cannot be
  153.      * called again until the network service reloads the servlet.
  154.      *
  155.      * <p>When the network service removes a servlet, it calls destroy
  156.      * after all service calls have been completed, or a
  157.      * service-specific number of seconds have passed, whichever comes
  158.      * first.  In the case of long-running operations, there could be
  159.      * other threads running service requests when destroy is called.
  160.      * The servlet writer is responsible for making sure that any
  161.      * threads still in the service method complete.
  162.      */
  163.     public void destroy();
  164. }
  165.