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. package javax.servlet;
  61. import java.io.IOException;
  62. import java.util.Enumeration;
  63. /**
  64. *
  65. * Defines a generic, protocol-independent
  66. * servlet. To write an HTTP servlet for use on the
  67. * Web, extend {@link javax.servlet.http.HttpServlet} instead.
  68. *
  69. * <p><code>GenericServlet</code> implements the <code>Servlet</code>
  70. * and <code>ServletConfig</code> interfaces. <code>GenericServlet</code>
  71. * may be directly extended by a servlet, although it's more common to extend
  72. * a protocol-specific subclass such as <code>HttpServlet</code>.
  73. *
  74. * <p><code>GenericServlet</code> makes writing servlets
  75. * easier. It provides simple versions of the lifecycle methods
  76. * <code>init</code> and <code>destroy</code> and of the methods
  77. * in the <code>ServletConfig</code> interface. <code>GenericServlet</code>
  78. * also implements the <code>log</code> method, declared in the
  79. * <code>ServletContext</code> interface.
  80. *
  81. * <p>To write a generic servlet, you need only
  82. * override the abstract <code>service</code> method.
  83. *
  84. *
  85. * @author Various
  86. * @version $Version$
  87. *
  88. *
  89. *
  90. */
  91. public abstract class GenericServlet
  92. implements Servlet, ServletConfig, java.io.Serializable
  93. {
  94. private transient ServletConfig config;
  95. /**
  96. *
  97. * Does nothing. All of the servlet initialization
  98. * is done by one of the <code>init</code> methods.
  99. *
  100. */
  101. public GenericServlet() { }
  102. /**
  103. * Called by the servlet container to indicate to a servlet that the
  104. * servlet is being taken out of service. See {@link Servlet#destroy}.
  105. *
  106. *
  107. */
  108. public void destroy() {
  109. log("destroy");
  110. }
  111. /**
  112. * Returns a <code>String</code> containing the value of the named
  113. * initialization parameter, or <code>null</code> if the parameter does
  114. * not exist. See {@link ServletConfig#getInitParameter}.
  115. *
  116. * <p>This method is supplied for convenience. It gets the
  117. * value of the named parameter from the servlet's
  118. * <code>ServletConfig</code> object.
  119. *
  120. * @param name a <code>String</code> specifying the name
  121. * of the initialization parameter
  122. *
  123. * @return String a <code>String</code> containing the value
  124. * of the initalization parameter
  125. *
  126. */
  127. public String getInitParameter(String name) {
  128. return getServletConfig().getInitParameter(name);
  129. }
  130. /**
  131. * Returns the names of the servlet's initialization parameters
  132. * as an <code>Enumeration</code> of <code>String</code> objects,
  133. * or an empty <code>Enumeration</code> if the servlet has no
  134. * initialization parameters. See {@link
  135. * ServletConfig#getInitParameterNames}.
  136. *
  137. * <p>This method is supplied for convenience. It gets the
  138. * parameter names from the servlet's <code>ServletConfig</code> object.
  139. *
  140. *
  141. * @return Enumeration an enumeration of <code>String</code>
  142. * objects containing the names of
  143. * the servlet's initialization parameters
  144. *
  145. */
  146. public Enumeration getInitParameterNames() {
  147. return getServletConfig().getInitParameterNames();
  148. }
  149. /**
  150. * Returns this servlet's {@link ServletConfig} object.
  151. *
  152. * @return ServletConfig the <code>ServletConfig</code> object
  153. * that initialized this servlet
  154. *
  155. */
  156. public ServletConfig getServletConfig() {
  157. return config;
  158. }
  159. /**
  160. * Returns a reference to the {@link ServletContext} in which this servlet
  161. * is running. See {@link ServletConfig#getServletContext}.
  162. *
  163. * <p>This method is supplied for convenience. It gets the
  164. * context from the servlet's <code>ServletConfig</code> object.
  165. *
  166. *
  167. * @return ServletContext the <code>ServletContext</code> object
  168. * passed to this servlet by the <code>init</code>
  169. * method
  170. *
  171. */
  172. public ServletContext getServletContext() {
  173. return getServletConfig().getServletContext();
  174. }
  175. /**
  176. * Returns information about the servlet, such as
  177. * author, version, and copyright.
  178. * By default, this method returns an empty string. Override this method
  179. * to have it return a meaningful value. See {@link
  180. * Servlet#getServletInfo}.
  181. *
  182. *
  183. * @return String information about this servlet, by default an
  184. * empty string
  185. *
  186. */
  187. public String getServletInfo() {
  188. return "";
  189. }
  190. /**
  191. *
  192. * Called by the servlet container to indicate to a servlet that the
  193. * servlet is being placed into service. See {@link Servlet#init}.
  194. *
  195. * <p>This implementation stores the {@link ServletConfig}
  196. * object it receives from the servlet container for later use.
  197. * When overriding this form of the method, call
  198. * <code>super.init(config)</code>.
  199. *
  200. * @param config the <code>ServletConfig</code> object
  201. * that contains configutation
  202. * information for this servlet
  203. *
  204. * @exception ServletException if an exception occurs that
  205. * interrupts the servlet's normal
  206. * operation
  207. *
  208. *
  209. * @see UnavailableException
  210. *
  211. */
  212. public void init(ServletConfig config) throws ServletException {
  213. this.config = config;
  214. log("init");
  215. this.init();
  216. }
  217. /**
  218. *
  219. * A convenience method which can be overridden so that there's no need
  220. * to call <code>super.init(config)</code>.
  221. *
  222. * <p>Instead of overriding {@link #init(ServletConfig)}, simply override
  223. * this method and it will be called by
  224. * <code>GenericServlet.init(ServletConfig config)</code>.
  225. * The <code>ServletConfig</code> object can still be retrieved via {@link
  226. * #getServletConfig}.
  227. *
  228. * @exception ServletException if an exception occurs that
  229. * interrupts the servlet's
  230. * normal operation
  231. *
  232. */
  233. public void init() throws ServletException {
  234. }
  235. /**
  236. *
  237. * Writes the specified message to a servlet log file, prepended by the
  238. * servlet's name. See {@link ServletContext#log(String)}.
  239. *
  240. * @param msg a <code>String</code> specifying
  241. * the message to be written to the log file
  242. *
  243. */
  244. public void log(String msg) {
  245. getServletContext().log(getServletName() + ": "+ msg);
  246. }
  247. /**
  248. * Writes an explanatory message and a stack trace
  249. * for a given <code>Throwable</code> exception
  250. * to the servlet log file, prepended by the servlet's name.
  251. * See {@link ServletContext#log(String, Throwable)}.
  252. *
  253. *
  254. * @param message a <code>String</code> that describes
  255. * the error or exception
  256. *
  257. * @param t the <code>java.lang.Throwable</code> error
  258. * or exception
  259. *
  260. *
  261. */
  262. public void log(String message, Throwable t) {
  263. getServletContext().log(getServletName() + ": " + message, t);
  264. }
  265. /**
  266. * Called by the servlet container to allow the servlet to respond to
  267. * a request. See {@link Servlet#service}.
  268. *
  269. * <p>This method is declared abstract so subclasses, such as
  270. * <code>HttpServlet</code>, must override it.
  271. *
  272. *
  273. *
  274. * @param req the <code>ServletRequest</code> object
  275. * that contains the client's request
  276. *
  277. * @param res the <code>ServletResponse</code> object
  278. * that will contain the servlet's response
  279. *
  280. * @exception ServletException if an exception occurs that
  281. * interferes with the servlet's
  282. * normal operation occurred
  283. *
  284. * @exception IOException if an input or output
  285. * exception occurs
  286. *
  287. */
  288. public abstract void service(ServletRequest req, ServletResponse res)
  289. throws ServletException, IOException;
  290. /**
  291. * Returns the name of this servlet instance.
  292. * See {@link ServletConfig#getServletName}.
  293. *
  294. * @return the name of this servlet instance
  295. *
  296. *
  297. *
  298. */
  299. public String getServletName() {
  300. return config.getServletName();
  301. }
  302. }