home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 September / PCWorld_2000-09_cd.bin / Komunik / sambar / _setup.1 / javaeng.jar / javax / servlet / GenericServlet.java < prev    next >
Text File  |  2000-04-03  |  6KB  |  227 lines

  1. /*
  2.  * GenericServlet.java -- Abstract base class for all servlets
  3.  *
  4.  * Copyright (c) 1998, 1999 by Free Software Foundation, Inc.
  5.  * Written by Paul Siegmann (pauls@euronet.nl)
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU Library General Public License as published
  9.  * by the Free Software Foundation, version 2. (see COPYING.LIB)
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software Foundation
  18.  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
  19.  */
  20.  
  21. package javax.servlet;
  22.  
  23. import java.io.IOException;
  24. import java.io.Serializable;
  25. import java.util.Enumeration;
  26.  
  27.  
  28. /**
  29.  * Abstract base class for all servlets.
  30.  *
  31. #ifdef SERVLET_2_0
  32.  * @version Servlet API 2.0 
  33. #endif
  34. #ifdef SERVLET_2_1
  35.  * @version Servlet API 2.1
  36. #endif
  37. #ifdef SERVLET_2_2
  38.  * @version Servlet API 2.2
  39. #endif
  40.  * @since Servlet API 1.0
  41.  * @author Paul Siegmann (pauls@euronet.nl)
  42.  */
  43. public abstract class GenericServlet
  44.     implements Servlet, ServletConfig, Serializable 
  45. {
  46.     private ServletConfig myConfig;
  47.  
  48.     /**
  49.      * Does nothing.
  50.      *
  51.      * @since Servlet API 1.0
  52.      */
  53.     public GenericServlet() {
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Initializes the servlet.
  59.      * Called by the server exactly once during the lifetime of the servlet.
  60.      * This method can be used to setup resources (connections to a
  61.      * database for example) for this servlet.
  62. #ifdef SERVLET_2_0
  63. #else
  64.      * <p>
  65.      * This version saves the ServletConfig and calls <code>init()</code>.
  66.      * This means that a servlet can just override <code>init()</code>.
  67. #endif
  68.      * Note that if a servlet overrides this method it should call
  69.      * <code>super.init(config)</code> otherwise the other methods in
  70.      * GenericServlet are not garanteed to work.
  71.      *
  72.      * @since Servlet API 1.0
  73.      *
  74.      * @param config This servlet configuration class
  75.      * @exception ServletException If an error occurs
  76.      */
  77.     public void init (ServletConfig config) throws ServletException {
  78.         myConfig = config;
  79.         log("started");
  80. #ifdef SERVLET_2_0
  81. #else
  82.         init();
  83. #endif
  84.     }
  85.  
  86. #ifdef SERVLET_2_0
  87. #else
  88.     /**
  89.      * Automatically called by <code>init(ServletConfig config)</code>.
  90.      * This version does nothing.
  91.      *
  92.      * @since Servlet API 2.1
  93.      *
  94.      * @exception ServletException If an error occurs
  95.      */
  96.     public void init () throws ServletException {
  97.     }
  98. #endif
  99.  
  100.     /**
  101.      * Returns the servlets context
  102.      *
  103.      * @since Servlet API 1.0
  104.      *
  105.      * @return The context
  106.      */
  107.     public ServletContext getServletContext() {
  108.         return myConfig.getServletContext();
  109.     }
  110.  
  111.  
  112.     /**
  113.      * Gets a servlet's initialization parameter
  114.      *
  115.      * @since Servlet API 1.0
  116.      *
  117.      * @param name the name of the wanted parameter
  118.      * @return the value of the wanted parameter.
  119.      * null if the named parameter is not present.
  120.      */
  121.     public String getInitParameter (String name) {
  122.         return myConfig.getInitParameter(name);
  123.     }
  124.  
  125.  
  126.     /**
  127.      * Gets all the initialization parameters
  128.      *
  129.      * @since Servlet API 1.0
  130.      *
  131.      * @return an Enumeration of all the parameters
  132.      */
  133.     public Enumeration getInitParameterNames () {
  134.         return myConfig.getInitParameterNames();
  135.     }
  136.  
  137.  
  138.     /**
  139.      * Writes the class name and a message to the log.
  140.      * Calls <code>getServletContext().log()</code>.
  141.      *
  142.      * @since Servlet API 1.0
  143.      *
  144.      * @param message the message to write
  145.      */
  146.     public void log(String message) {
  147.         getServletContext().log(this.getClass().getName() + ": " + message);
  148.     }
  149.  
  150.  
  151. #ifdef SERVLET_2_0
  152. #else
  153.     /**
  154.      * Writes the class name, a message and a stack trace to the log.
  155.      * Calls <code>getServletContext().log()</code>.
  156.      *
  157.      * @since Servlet API 2.1
  158.      * @param message the message to write
  159.      * @param th the object that was thrown to cause this log
  160.      */
  161.     public void log(String message, Throwable th) {
  162.         getServletContext().log(this.getClass().getName() + ": " + message, th);
  163.     }
  164.  
  165. #endif
  166.  
  167.     /**
  168.      * The servlet programmer can put other additional info (version
  169.      * number, etc) here.
  170.      * <p>
  171.      * The Servlet 2.1 Spec says that this should return an empty string.
  172.      * The Servlet 2.1 API says that this should return null unless overridden.
  173.      * This version returns the servlet's class name which seems more usefull.
  174.      *
  175.      * @since Servlet API 1.0
  176.      *
  177.      * @return The String holding the information
  178.      */
  179.     public String getServletInfo() {
  180.         return this.getClass().getName();
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Gets the servlet config class
  186.      *
  187.      * @since Servlet API 1.0
  188.      *
  189.      * @return The config class
  190.      */
  191.     public ServletConfig getServletConfig() {
  192.         return myConfig;
  193.     }
  194.  
  195.  
  196.     /**
  197.      * Called by the server every time it wants the servlet to handle
  198.      * a request.
  199.      * 
  200.      * @since Servlet API 1.0
  201.      *
  202.      * @param request all the request information
  203.      * @param response class to write all the response data to
  204.      * @exception ServletException If an error occurs
  205.      * @exception IOException If an error occurs
  206.      */
  207.     public abstract void service(ServletRequest request,
  208.                     ServletResponse response) throws ServletException, IOException;
  209.  
  210.  
  211.     /**
  212.      * Called by the server when it no longer needs the servlet.
  213.      * The servlet programmer should use this method to free all
  214.      * the resources the servlet is holding.
  215.      * <p>
  216.      * This version does nothing because it has nothing to clean up.
  217.      * <p>
  218.      * Note that the the 2.1 Spec says that this should do nothing,
  219.      * but the 2.1 API Doc says that it logs the destroy action.
  220.      *
  221.      * @since Servlet API 1.0
  222.      */
  223.     public void destroy() {
  224.         // log("destroy");
  225.     }
  226. }
  227.