Save This Page
Home » glassfish-v2ur2-b04-src » javax » servlet » [javadoc | source]
    1   
    2   
    3   /*
    4    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    5    * 
    6    * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
    7    * 
    8    * Portions Copyright Apache Software Foundation.
    9    * 
   10    * The contents of this file are subject to the terms of either the GNU
   11    * General Public License Version 2 only ("GPL") or the Common Development
   12    * and Distribution License("CDDL") (collectively, the "License").  You
   13    * may not use this file except in compliance with the License. You can obtain
   14    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   15    * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
   16    * language governing permissions and limitations under the License.
   17    * 
   18    * When distributing the software, include this License Header Notice in each
   19    * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
   20    * Sun designates this particular file as subject to the "Classpath" exception
   21    * as provided by Sun in the GPL Version 2 section of the License file that
   22    * accompanied this code.  If applicable, add the following below the License
   23    * Header, with the fields enclosed by brackets [] replaced by your own
   24    * identifying information: "Portions Copyrighted [year]
   25    * [name of copyright owner]"
   26    * 
   27    * Contributor(s):
   28    * 
   29    * If you wish your version of this file to be governed by only the CDDL or
   30    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   31    * elects to include this software in this distribution under the [CDDL or GPL
   32    * Version 2] license."  If you don't indicate a single choice of license, a
   33    * recipient has the option to distribute your version of this file under
   34    * either the CDDL, the GPL Version 2 or to extend the choice of license to
   35    * its licensees as provided above.  However, if you add GPL Version 2 code
   36    * and therefore, elected the GPL Version 2 license, then the option applies
   37    * only if the new code is made subject to such option by the copyright
   38    * holder.
   39    */
   40   
   41   package javax.servlet;
   42   
   43   import java.io.IOException;
   44   import java.util.Enumeration;
   45   import java.util.ResourceBundle;
   46   
   47   /**
   48    *
   49    * Defines a generic, protocol-independent
   50    * servlet. To write an HTTP servlet for use on the
   51    * Web, extend {@link javax.servlet.http.HttpServlet} instead.
   52    *
   53    * <p><code>GenericServlet</code> implements the <code>Servlet</code>
   54    * and <code>ServletConfig</code> interfaces. <code>GenericServlet</code>
   55    * may be directly extended by a servlet, although it's more common to extend
   56    * a protocol-specific subclass such as <code>HttpServlet</code>.
   57    *
   58    * <p><code>GenericServlet</code> makes writing servlets
   59    * easier. It provides simple versions of the lifecycle methods 
   60    * <code>init</code> and <code>destroy</code> and of the methods 
   61    * in the <code>ServletConfig</code> interface. <code>GenericServlet</code>
   62    * also implements the <code>log</code> method, declared in the
   63    * <code>ServletContext</code> interface. 
   64    *
   65    * <p>To write a generic servlet, you need only
   66    * override the abstract <code>service</code> method. 
   67    *
   68    *
   69    * @author 	Various
   70    */
   71   
   72    
   73   public abstract class GenericServlet 
   74       implements Servlet, ServletConfig, java.io.Serializable
   75   {
   76       private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
   77       private static ResourceBundle lStrings =
   78           ResourceBundle.getBundle(LSTRING_FILE);
   79   
   80       private transient ServletConfig config;
   81       
   82   
   83       /**
   84        *
   85        * Does nothing. All of the servlet initialization
   86        * is done by one of the <code>init</code> methods.
   87        *
   88        */
   89   
   90       public GenericServlet() { }
   91       
   92       
   93       
   94      /**
   95        * Called by the servlet container to indicate to a servlet that the
   96        * servlet is being taken out of service.  See {@link Servlet#destroy}.
   97        *
   98        * 
   99        */
  100   
  101       public void destroy() {
  102       }
  103       
  104       
  105       
  106       /**
  107        * Returns a <code>String</code> containing the value of the named
  108        * initialization parameter, or <code>null</code> if the parameter does
  109        * not exist.  See {@link ServletConfig#getInitParameter}.
  110        *
  111        * <p>This method is supplied for convenience. It gets the 
  112        * value of the named parameter from the servlet's 
  113        * <code>ServletConfig</code> object.
  114        *
  115        * @param name 		a <code>String</code> specifying the name 
  116        *				of the initialization parameter
  117        *
  118        * @return String 		a <code>String</code> containing the value
  119        *				of the initialization parameter
  120        *
  121        */ 
  122   
  123       public String getInitParameter(String name) {
  124           ServletConfig sc = getServletConfig();
  125           if (sc == null) {
  126               throw new IllegalStateException(
  127                   lStrings.getString("err.servlet_config_not_initialized"));
  128           }
  129   
  130           return sc.getInitParameter(name);
  131       }
  132       
  133       
  134   
  135      /**
  136       * Returns the names of the servlet's initialization parameters 
  137       * as an <code>Enumeration</code> of <code>String</code> objects,
  138       * or an empty <code>Enumeration</code> if the servlet has no
  139       * initialization parameters.  See {@link
  140       * ServletConfig#getInitParameterNames}.
  141       *
  142       * <p>This method is supplied for convenience. It gets the 
  143       * parameter names from the servlet's <code>ServletConfig</code> object. 
  144       *
  145       *
  146       * @return Enumeration 	an enumeration of <code>String</code>
  147       *				objects containing the names of 
  148       *				the servlet's initialization parameters
  149       *
  150       */
  151   
  152       public Enumeration getInitParameterNames() {
  153           ServletConfig sc = getServletConfig();
  154           if (sc == null) {
  155               throw new IllegalStateException(
  156                   lStrings.getString("err.servlet_config_not_initialized"));
  157           }
  158   
  159           return sc.getInitParameterNames();
  160       }   
  161       
  162        
  163    
  164        
  165   
  166       /**
  167        * Returns this servlet's {@link ServletConfig} object.
  168        *
  169        * @return ServletConfig 	the <code>ServletConfig</code> object
  170        *				that initialized this servlet
  171        *
  172        */
  173       
  174       public ServletConfig getServletConfig() {
  175   	return config;
  176       }
  177       
  178       
  179    
  180       
  181       /**
  182        * Returns a reference to the {@link ServletContext} in which this servlet
  183        * is running.  See {@link ServletConfig#getServletContext}.
  184        *
  185        * <p>This method is supplied for convenience. It gets the 
  186        * context from the servlet's <code>ServletConfig</code> object.
  187        *
  188        *
  189        * @return ServletContext 	the <code>ServletContext</code> object
  190        *				passed to this servlet by the <code>init</code>
  191        *				method
  192        *
  193        */
  194   
  195       public ServletContext getServletContext() {
  196           ServletConfig sc = getServletConfig();
  197           if (sc == null) {
  198               throw new IllegalStateException(
  199                   lStrings.getString("err.servlet_config_not_initialized"));
  200           }
  201   
  202           return sc.getServletContext();
  203       }
  204   
  205   
  206   
  207    
  208   
  209       /**
  210        * Returns information about the servlet, such as 
  211        * author, version, and copyright. 
  212        * By default, this method returns an empty string.  Override this method
  213        * to have it return a meaningful value.  See {@link
  214        * Servlet#getServletInfo}.
  215        *
  216        *
  217        * @return String 		information about this servlet, by default an
  218        * 				empty string
  219        *
  220        */
  221       
  222       public String getServletInfo() {
  223   	return "";
  224       }
  225   
  226   
  227   
  228   
  229       /**
  230        *
  231        * Called by the servlet container to indicate to a servlet that the
  232        * servlet is being placed into service.  See {@link Servlet#init}.
  233        *
  234        * <p>This implementation stores the {@link ServletConfig}
  235        * object it receives from the servlet container for later use.
  236        * When overriding this form of the method, call 
  237        * <code>super.init(config)</code>.
  238        *
  239        * @param config 			the <code>ServletConfig</code> object
  240        *					that contains configutation
  241        *					information for this servlet
  242        *
  243        * @exception ServletException 	if an exception occurs that
  244        *					interrupts the servlet's normal
  245        *					operation
  246        *
  247        * 
  248        * @see 				UnavailableException
  249        *
  250        */
  251   
  252       public void init(ServletConfig config) throws ServletException {
  253   	this.config = config;
  254   	this.init();
  255       }
  256   
  257   
  258   
  259   
  260   
  261       /**
  262        *
  263        * A convenience method which can be overridden so that there's no need
  264        * to call <code>super.init(config)</code>.
  265        *
  266        * <p>Instead of overriding {@link #init(ServletConfig)}, simply override
  267        * this method and it will be called by
  268        * <code>GenericServlet.init(ServletConfig config)</code>.
  269        * The <code>ServletConfig</code> object can still be retrieved via {@link
  270        * #getServletConfig}. 
  271        *
  272        * @exception ServletException 	if an exception occurs that
  273        *					interrupts the servlet's
  274        *					normal operation
  275        *
  276        */
  277       
  278       public void init() throws ServletException {
  279   
  280       }
  281       
  282   
  283   
  284   
  285       /**
  286        * 
  287        * Writes the specified message to a servlet log file, prepended by the
  288        * servlet's name.  See {@link ServletContext#log(String)}.
  289        *
  290        * @param msg 	a <code>String</code> specifying
  291        *			the message to be written to the log file
  292        *
  293        */
  294        
  295       public void log(String msg) {
  296   	getServletContext().log(getServletName() + ": "+ msg);
  297       }
  298      
  299      
  300      
  301      
  302       /**
  303        * Writes an explanatory message and a stack trace
  304        * for a given <code>Throwable</code> exception
  305        * to the servlet log file, prepended by the servlet's name.
  306        * See {@link ServletContext#log(String, Throwable)}.
  307        *
  308        *
  309        * @param message 		a <code>String</code> that describes
  310        *				the error or exception
  311        *
  312        * @param t			the <code>java.lang.Throwable</code> error
  313        * 				or exception
  314        *
  315        *
  316        */
  317      
  318       public void log(String message, Throwable t) {
  319   	getServletContext().log(getServletName() + ": " + message, t);
  320       }
  321       
  322       
  323       
  324       /**
  325        * Called by the servlet container to allow the servlet to respond to
  326        * a request.  See {@link Servlet#service}.
  327        * 
  328        * <p>This method is declared abstract so subclasses, such as 
  329        * <code>HttpServlet</code>, must override it.
  330        *
  331        *
  332        *
  333        * @param req 	the <code>ServletRequest</code> object
  334        *			that contains the client's request
  335        *
  336        * @param res 	the <code>ServletResponse</code> object
  337        *			that will contain the servlet's response
  338        *
  339        * @exception ServletException 	if an exception occurs that
  340        *					interferes with the servlet's
  341        *					normal operation occurred
  342        *
  343        * @exception IOException 		if an input or output
  344        *					exception occurs
  345        *
  346        */
  347   
  348       public abstract void service(ServletRequest req, ServletResponse res)
  349   	throws ServletException, IOException;
  350       
  351   
  352   
  353       /**
  354        * Returns the name of this servlet instance.
  355        * See {@link ServletConfig#getServletName}.
  356        *
  357        * @return          the name of this servlet instance
  358        *
  359        *
  360        *
  361        */
  362   
  363       public String getServletName() {
  364           ServletConfig sc = getServletConfig();
  365           if (sc == null) {
  366               throw new IllegalStateException(
  367                   lStrings.getString("err.servlet_config_not_initialized"));
  368           }
  369   
  370           return sc.getServletName();
  371       }
  372   }

Save This Page
Home » glassfish-v2ur2-b04-src » javax » servlet » [javadoc | source]