1. /*
    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
    8. *
    9. * http://www.apache.org/licenses/LICENSE-2.0
    10. *
    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.
    16. */
    17. package javax.servlet;
    18. import java.io.IOException;
    19. /**
    20. * Defines methods that all servlets must implement.
    21. *
    22. * <p>
    23. * A servlet is a small Java program that runs within a Web server. Servlets
    24. * receive and respond to requests from Web clients, usually across HTTP, the
    25. * HyperText Transfer Protocol.
    26. *
    27. * <p>
    28. * To implement this interface, you can write a generic servlet that extends
    29. * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that extends
    30. * <code>javax.servlet.http.HttpServlet</code>.
    31. *
    32. * <p>
    33. * This interface defines methods to initialize a servlet, to service requests,
    34. * and to remove a servlet from the server. These are known as life-cycle
    35. * methods and are called in the following sequence:
    36. * <ol>
    37. * <li>The servlet is constructed, then initialized with the <code>init</code>
    38. * method.
    39. * <li>Any calls from clients to the <code>service</code> method are handled.
    40. * <li>The servlet is taken out of service, then destroyed with the
    41. * <code>destroy</code> method, then garbage collected and finalized.
    42. * </ol>
    43. *
    44. * <p>
    45. * In addition to the life-cycle methods, this interface provides the
    46. * <code>getServletConfig</code> method, which the servlet can use to get any
    47. * startup information, and the <code>getServletInfo</code> method, which allows
    48. * the servlet to return basic information about itself, such as author,
    49. * version, and copyright.
    50. *
    51. * @see GenericServlet
    52. * @see javax.servlet.http.HttpServlet
    53. */
    54. public interface Servlet {
    55. /**
    56. * Called by the servlet container to indicate to a servlet that the servlet
    57. * is being placed into service.
    58. *
    59. * <p>
    60. * The servlet container calls the <code>init</code> method exactly once
    61. * after instantiating the servlet. The <code>init</code> method must
    62. * complete successfully before the servlet can receive any requests.
    63. *
    64. * <p>
    65. * The servlet container cannot place the servlet into service if the
    66. * <code>init</code> method
    67. * <ol>
    68. * <li>Throws a <code>ServletException</code>
    69. * <li>Does not return within a time period defined by the Web server
    70. * </ol>
    71. *
    72. *
    73. * @param config
    74. * a <code>ServletConfig</code> object containing the servlet's
    75. * configuration and initialization parameters
    76. *
    77. * @exception ServletException
    78. * if an exception has occurred that interferes with the
    79. * servlet's normal operation
    80. *
    81. * @see UnavailableException
    82. * @see #getServletConfig
    83. */
    84. public void init(ServletConfig config) throws ServletException;
    85. /**
    86. *
    87. * Returns a {@link ServletConfig} object, which contains initialization and
    88. * startup parameters for this servlet. The <code>ServletConfig</code>
    89. * object returned is the one passed to the <code>init</code> method.
    90. *
    91. * <p>
    92. * Implementations of this interface are responsible for storing the
    93. * <code>ServletConfig</code> object so that this method can return it. The
    94. * {@link GenericServlet} class, which implements this interface, already
    95. * does this.
    96. *
    97. * @return the <code>ServletConfig</code> object that initializes this
    98. * servlet
    99. *
    100. * @see #init
    101. */
    102. public ServletConfig getServletConfig();
    103. /**
    104. * Called by the servlet container to allow the servlet to respond to a
    105. * request.
    106. *
    107. * <p>
    108. * This method is only called after the servlet's <code>init()</code> method
    109. * has completed successfully.
    110. *
    111. * <p>
    112. * The status code of the response always should be set for a servlet that
    113. * throws or sends an error.
    114. *
    115. *
    116. * <p>
    117. * Servlets typically run inside multithreaded servlet containers that can
    118. * handle multiple requests concurrently. Developers must be aware to
    119. * synchronize access to any shared resources such as files, network
    120. * connections, and as well as the servlet's class and instance variables.
    121. * More information on multithreaded programming in Java is available in <a
    122. * href
    123. * ="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">
    124. * the Java tutorial on multi-threaded programming</a>.
    125. *
    126. *
    127. * @param req
    128. * the <code>ServletRequest</code> object that contains the
    129. * client's request
    130. *
    131. * @param res
    132. * the <code>ServletResponse</code> object that contains the
    133. * servlet's response
    134. *
    135. * @exception ServletException
    136. * if an exception occurs that interferes with the servlet's
    137. * normal operation
    138. *
    139. * @exception IOException
    140. * if an input or output exception occurs
    141. */
    142. public void service(ServletRequest req, ServletResponse res)
    143. throws ServletException, IOException;
    144. /**
    145. * Returns information about the servlet, such as author, version, and
    146. * copyright.
    147. *
    148. * <p>
    149. * The string that this method returns should be plain text and not markup
    150. * of any kind (such as HTML, XML, etc.).
    151. *
    152. * @return a <code>String</code> containing servlet information
    153. */
    154. public String getServletInfo();
    155. /**
    156. * Called by the servlet container to indicate to a servlet that the servlet
    157. * is being taken out of service. This method is only called once all
    158. * threads within the servlet's <code>service</code> method have exited or
    159. * after a timeout period has passed. After the servlet container calls this
    160. * method, it will not call the <code>service</code> method again on this
    161. * servlet.
    162. *
    163. * <p>
    164. * This method gives the servlet an opportunity to clean up any resources
    165. * that are being held (for example, memory, file handles, threads) and make
    166. * sure that any persistent state is synchronized with the servlet's current
    167. * state in memory.
    168. */
    169. public void destroy();
    170. }