home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Komunik / sambar / sambar51p.exe / lib / javaeng.jar / javax / servlet / GenericServlet.java < prev    next >
Encoding:
Java Source  |  2001-10-22  |  11.4 KB  |  376 lines

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution, if
  20.  *    any, must include the following acknowlegement:  
  21.  *       "This product includes software developed by the 
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowlegement may appear in the software itself,
  24.  *    if and wherever such third-party acknowlegements normally appear.
  25.  *
  26.  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27.  *    Foundation" must not be used to endorse or promote products derived
  28.  *    from this software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache"
  32.  *    nor may "Apache" appear in their names without prior written
  33.  *    permission of the Apache Group.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * ====================================================================
  55.  *
  56.  * This source code implements specifications defined by the Java
  57.  * Community Process. In order to remain compliant with the specification
  58.  * DO NOT add / change / or delete method signatures!
  59.  */ 
  60.  
  61. package javax.servlet;
  62.  
  63. import java.io.IOException;
  64. import java.util.Enumeration;
  65.  
  66. /**
  67.  *
  68.  * Defines a generic, protocol-independent
  69.  * servlet. To write an HTTP servlet for use on the
  70.  * Web, extend {@link javax.servlet.http.HttpServlet} instead.
  71.  *
  72.  * <p><code>GenericServlet</code> implements the <code>Servlet</code>
  73.  * and <code>ServletConfig</code> interfaces. <code>GenericServlet</code>
  74.  * may be directly extended by a servlet, although it's more common to extend
  75.  * a protocol-specific subclass such as <code>HttpServlet</code>.
  76.  *
  77.  * <p><code>GenericServlet</code> makes writing servlets
  78.  * easier. It provides simple versions of the lifecycle methods 
  79.  * <code>init</code> and <code>destroy</code> and of the methods 
  80.  * in the <code>ServletConfig</code> interface. <code>GenericServlet</code>
  81.  * also implements the <code>log</code> method, declared in the
  82.  * <code>ServletContext</code> interface. 
  83.  *
  84.  * <p>To write a generic servlet, you need only
  85.  * override the abstract <code>service</code> method. 
  86.  *
  87.  *
  88.  * @author     Various
  89.  * @version     $Version$
  90.  *
  91.  *
  92.  *
  93.  */
  94.  
  95.  
  96. public abstract class GenericServlet 
  97.     implements Servlet, ServletConfig, java.io.Serializable
  98. {
  99.  
  100.     private transient ServletConfig config;
  101.     
  102.  
  103.     /**
  104.      *
  105.      * Does nothing. All of the servlet initialization
  106.      * is done by one of the <code>init</code> methods.
  107.      *
  108.      */
  109.  
  110.     public GenericServlet() { }
  111.     
  112.     
  113.     
  114.    /**
  115.      * Called by the servlet container to indicate to a servlet that the
  116.      * servlet is being taken out of service.  See {@link Servlet#destroy}.
  117.      *
  118.      * 
  119.      */
  120.  
  121.     public void destroy() {
  122.     log("destroy");
  123.     }
  124.     
  125.     
  126.     
  127.     /**
  128.      * Returns a <code>String</code> containing the value of the named
  129.      * initialization parameter, or <code>null</code> if the parameter does
  130.      * not exist.  See {@link ServletConfig#getInitParameter}.
  131.      *
  132.      * <p>This method is supplied for convenience. It gets the 
  133.      * value of the named parameter from the servlet's 
  134.      * <code>ServletConfig</code> object.
  135.      *
  136.      * @param name         a <code>String</code> specifying the name 
  137.      *                of the initialization parameter
  138.      *
  139.      * @return String         a <code>String</code> containing the value
  140.      *                of the initalization parameter
  141.      *
  142.      */ 
  143.  
  144.     public String getInitParameter(String name) {
  145.     return getServletConfig().getInitParameter(name);
  146.     }
  147.     
  148.     
  149.  
  150.    /**
  151.     * Returns the names of the servlet's initialization parameters 
  152.     * as an <code>Enumeration</code> of <code>String</code> objects,
  153.     * or an empty <code>Enumeration</code> if the servlet has no
  154.     * initialization parameters.  See {@link
  155.     * ServletConfig#getInitParameterNames}.
  156.     *
  157.     * <p>This method is supplied for convenience. It gets the 
  158.     * parameter names from the servlet's <code>ServletConfig</code> object. 
  159.     *
  160.     *
  161.     * @return Enumeration     an enumeration of <code>String</code>
  162.     *                objects containing the names of 
  163.     *                the servlet's initialization parameters
  164.     *
  165.     */
  166.  
  167.     public Enumeration getInitParameterNames() {
  168.     return getServletConfig().getInitParameterNames();
  169.     }   
  170.     
  171.      
  172.  
  173.      
  174.  
  175.     /**
  176.      * Returns this servlet's {@link ServletConfig} object.
  177.      *
  178.      * @return ServletConfig     the <code>ServletConfig</code> object
  179.      *                that initialized this servlet
  180.      *
  181.      */
  182.     
  183.     public ServletConfig getServletConfig() {
  184.     return config;
  185.     }
  186.     
  187.     
  188.  
  189.     
  190.     /**
  191.      * Returns a reference to the {@link ServletContext} in which this servlet
  192.      * is running.  See {@link ServletConfig#getServletContext}.
  193.      *
  194.      * <p>This method is supplied for convenience. It gets the 
  195.      * context from the servlet's <code>ServletConfig</code> object.
  196.      *
  197.      *
  198.      * @return ServletContext     the <code>ServletContext</code> object
  199.      *                passed to this servlet by the <code>init</code>
  200.      *                method
  201.      *
  202.      */
  203.  
  204.     public ServletContext getServletContext() {
  205.     return getServletConfig().getServletContext();
  206.     }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.     /**
  213.      * Returns information about the servlet, such as 
  214.      * author, version, and copyright. 
  215.      * By default, this method returns an empty string.  Override this method
  216.      * to have it return a meaningful value.  See {@link
  217.      * Servlet#getServletInfo}.
  218.      *
  219.      *
  220.      * @return String         information about this servlet, by default an
  221.      *                 empty string
  222.      *
  223.      */
  224.     
  225.     public String getServletInfo() {
  226.     return "";
  227.     }
  228.  
  229.  
  230.  
  231.  
  232.     /**
  233.      *
  234.      * Called by the servlet container to indicate to a servlet that the
  235.      * servlet is being placed into service.  See {@link Servlet#init}.
  236.      *
  237.      * <p>This implementation stores the {@link ServletConfig}
  238.      * object it receives from the servlet container for alter use.
  239.      * When overriding this form of the method, call 
  240.      * <code>super.init(config)</code>.
  241.      *
  242.      * @param config             the <code>ServletConfig</code> object
  243.      *                    that contains configutation
  244.      *                    information for this servlet
  245.      *
  246.      * @exception ServletException     if an exception occurs that
  247.      *                    interrupts the servlet's normal
  248.      *                    operation
  249.      *
  250.      * 
  251.      * @see                 UnavailableException
  252.      *
  253.      */
  254.  
  255.     public void init(ServletConfig config) throws ServletException {
  256.     this.config = config;
  257.     log("init");
  258.     this.init();
  259.     }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.     /**
  266.      *
  267.      * A convenience method which can be overridden so that there's no need
  268.      * to call <code>super.init(config)</code>.
  269.      *
  270.      * <p>Instead of overriding {@link #init(ServletConfig)}, simply override
  271.      * this method and it will be called by
  272.      * <code>GenericServlet.init(ServletConfig config)</code>.
  273.      * The <code>ServletConfig</code> object can still be retrieved via {@link
  274.      * #getServletConfig}. 
  275.      *
  276.      * @exception ServletException     if an exception occurs that
  277.      *                    interrupts the servlet's
  278.      *                    normal operation
  279.      *
  280.      */
  281.     
  282.     public void init() throws ServletException {
  283.  
  284.     }
  285.     
  286.  
  287.  
  288.  
  289.     /**
  290.      * 
  291.      * Writes the specified message to a servlet log file, prepended by the
  292.      * servlet's name.  See {@link ServletContext#log(String)}.
  293.      *
  294.      * @param msg     a <code>String</code> specifying
  295.      *            the message to be written to the log file
  296.      *
  297.      */
  298.      
  299.     public void log(String msg) {
  300.         ServletContext context = getServletContext();
  301.         if (context==null)
  302.             System.err.println("GenericServlet.log(pre-init): "+
  303.                                getServletName()+": "+msg);
  304.         else
  305.             context.log(getServletName() + ": "+ msg);
  306.     }
  307.    
  308.    
  309.    
  310.    
  311.     /**
  312.      * Writes an explanatory message and a stack trace
  313.      * for a given <code>Throwable</code> exception
  314.      * to the servlet log file, prepended by the servlet's name.
  315.      * See {@link ServletContext#log(String, Throwable)}.
  316.      *
  317.      *
  318.      * @param message         a <code>String</code> that describes
  319.      *                the error or exception
  320.      *
  321.      * @param t            the <code>java.lang.Throwable</code> error
  322.      *                 or exception
  323.      *
  324.      *
  325.      */
  326.    
  327.     public void log(String message, Throwable t) {
  328.     getServletContext().log(getServletName() + ": " + message, t);
  329.     }
  330.     
  331.     
  332.     
  333.     /**
  334.      * Called by the servlet container to allow the servlet to respond to
  335.      * a request.  See {@link Servlet#service}.
  336.      * 
  337.      * <p>This method is declared abstract so subclasses, such as 
  338.      * <code>HttpServlet</code>, must override it.
  339.      *
  340.      *
  341.      *
  342.      * @param req     the <code>ServletRequest</code> object
  343.      *            that contains the client's request
  344.      *
  345.      * @param res     the <code>ServletResponse</code> object
  346.      *            that will contain the servlet's response
  347.      *
  348.      * @exception ServletException     if an exception occurs that
  349.      *                    interferes with the servlet's
  350.      *                    normal operation occurred
  351.      *
  352.      * @exception IOException         if an input or output
  353.      *                    exception occurs
  354.      *
  355.      */
  356.  
  357.     public abstract void service(ServletRequest req, ServletResponse res)
  358.     throws ServletException, IOException;
  359.     
  360.  
  361.  
  362.     /**
  363.      * Returns the name of this servlet instance.
  364.      * See {@link ServletConfig#getServletName}.
  365.      *
  366.      * @return          the name of this servlet instance
  367.      *
  368.      *
  369.      *
  370.      */
  371.  
  372.     public String getServletName() {
  373.         return config.getServletName();
  374.     }
  375. }
  376.