2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package javax.servlet;
19 import java.io.IOException;
20 import java.util.Enumeration;
23 * Defines a generic, protocol-independent servlet. To write an HTTP servlet for
24 * use on the Web, extend {@link javax.servlet.http.HttpServlet} instead.
26 * <code>GenericServlet</code> implements the <code>Servlet</code> and
27 * <code>ServletConfig</code> interfaces. <code>GenericServlet</code> may be
28 * directly extended by a servlet, although it's more common to extend a
29 * protocol-specific subclass such as <code>HttpServlet</code>.
31 * <code>GenericServlet</code> makes writing servlets easier. It provides simple
32 * versions of the lifecycle methods <code>init</code> and <code>destroy</code>
33 * and of the methods in the <code>ServletConfig</code> interface.
34 * <code>GenericServlet</code> also implements the <code>log</code> method,
35 * declared in the <code>ServletContext</code> interface.
37 * To write a generic servlet, you need only override the abstract
38 * <code>service</code> method.
40 public abstract class GenericServlet implements Servlet, ServletConfig,
41 java.io.Serializable {
43 private static final long serialVersionUID = 1L;
45 private transient ServletConfig config;
48 * Does nothing. All of the servlet initialization is done by one of the
49 * <code>init</code> methods.
51 public GenericServlet() {
56 * Called by the servlet container to indicate to a servlet that the servlet
57 * is being taken out of service. See {@link Servlet#destroy}.
60 public void destroy() {
65 * Returns a <code>String</code> containing the value of the named
66 * initialization parameter, or <code>null</code> if the parameter does not
67 * exist. See {@link ServletConfig#getInitParameter}.
69 * This method is supplied for convenience. It gets the value of the named
70 * parameter from the servlet's <code>ServletConfig</code> object.
73 * a <code>String</code> specifying the name of the
74 * initialization parameter
75 * @return String a <code>String</code> containing the value of the
76 * initialization parameter
79 public String getInitParameter(String name) {
80 return getServletConfig().getInitParameter(name);
84 * Returns the names of the servlet's initialization parameters as an
85 * <code>Enumeration</code> of <code>String</code> objects, or an empty
86 * <code>Enumeration</code> if the servlet has no initialization parameters.
87 * See {@link ServletConfig#getInitParameterNames}.
89 * This method is supplied for convenience. It gets the parameter names from
90 * the servlet's <code>ServletConfig</code> object.
92 * @return Enumeration an enumeration of <code>String</code> objects
93 * containing the names of the servlet's initialization parameters
96 public Enumeration<String> getInitParameterNames() {
97 return getServletConfig().getInitParameterNames();
101 * Returns this servlet's {@link ServletConfig} object.
103 * @return ServletConfig the <code>ServletConfig</code> object that
104 * initialized this servlet
107 public ServletConfig getServletConfig() {
112 * Returns a reference to the {@link ServletContext} in which this servlet
113 * is running. See {@link ServletConfig#getServletContext}.
115 * This method is supplied for convenience. It gets the context from the
116 * servlet's <code>ServletConfig</code> object.
118 * @return ServletContext the <code>ServletContext</code> object passed to
119 * this servlet by the <code>init</code> method
122 public ServletContext getServletContext() {
123 return getServletConfig().getServletContext();
127 * Returns information about the servlet, such as author, version, and
128 * copyright. By default, this method returns an empty string. Override this
129 * method to have it return a meaningful value. See
130 * {@link Servlet#getServletInfo}.
132 * @return String information about this servlet, by default an empty string
135 public String getServletInfo() {
140 * Called by the servlet container to indicate to a servlet that the servlet
141 * is being placed into service. See {@link Servlet#init}.
143 * This implementation stores the {@link ServletConfig} object it receives
144 * from the servlet container for later use. When overriding this form of
145 * the method, call <code>super.init(config)</code>.
148 * the <code>ServletConfig</code> object that contains
149 * configuration information for this servlet
150 * @exception ServletException
151 * if an exception occurs that interrupts the servlet's
153 * @see UnavailableException
156 public void init(ServletConfig config) throws ServletException {
157 this.config = config;
162 * A convenience method which can be overridden so that there's no need to
163 * call <code>super.init(config)</code>.
165 * Instead of overriding {@link #init(ServletConfig)}, simply override this
166 * method and it will be called by
167 * <code>GenericServlet.init(ServletConfig config)</code>. The
168 * <code>ServletConfig</code> object can still be retrieved via
169 * {@link #getServletConfig}.
171 * @exception ServletException
172 * if an exception occurs that interrupts the servlet's
175 public void init() throws ServletException {
180 * Writes the specified message to a servlet log file, prepended by the
181 * servlet's name. See {@link ServletContext#log(String)}.
184 * a <code>String</code> specifying the message to be written to
187 public void log(String msg) {
188 getServletContext().log(getServletName() + ": " + msg);
192 * Writes an explanatory message and a stack trace for a given
193 * <code>Throwable</code> exception to the servlet log file, prepended by
194 * the servlet's name. See {@link ServletContext#log(String, Throwable)}.
197 * a <code>String</code> that describes the error or exception
199 * the <code>java.lang.Throwable</code> error or exception
201 public void log(String message, Throwable t) {
202 getServletContext().log(getServletName() + ": " + message, t);
206 * Called by the servlet container to allow the servlet to respond to a
207 * request. See {@link Servlet#service}.
209 * This method is declared abstract so subclasses, such as
210 * <code>HttpServlet</code>, must override it.
213 * the <code>ServletRequest</code> object that contains the
216 * the <code>ServletResponse</code> object that will contain the
218 * @exception ServletException
219 * if an exception occurs that interferes with the servlet's
220 * normal operation occurred
221 * @exception IOException
222 * if an input or output exception occurs
225 public abstract void service(ServletRequest req, ServletResponse res)
226 throws ServletException, IOException;
229 * Returns the name of this servlet instance. See
230 * {@link ServletConfig#getServletName}.
232 * @return the name of this servlet instance
235 public String getServletName() {
236 return config.getServletName();