001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.servlet;
021
022 import java.io.IOException;
023
024
025 /**
026 * Defines methods that all servlets must implement.
027 *
028 * <p>A servlet is a small Java program that runs within a Web server.
029 * Servlets receive and respond to requests from Web clients,
030 * usually across HTTP, the HyperText Transfer Protocol.
031 *
032 * <p>To implement this interface, you can write a generic servlet
033 * that extends
034 * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that
035 * extends <code>javax.servlet.http.HttpServlet</code>.
036 *
037 * <p>This interface defines methods to initialize a servlet,
038 * to service requests, and to remove a servlet from the server.
039 * These are known as life-cycle methods and are called in the
040 * following sequence:
041 * <ol>
042 * <li>The servlet is constructed, then initialized with the <code>init</code> method.
043 * <li>Any calls from clients to the <code>service</code> method are handled.
044 * <li>The servlet is taken out of service, then destroyed with the
045 * <code>destroy</code> method, then garbage collected and finalized.
046 * </ol>
047 *
048 * <p>In addition to the life-cycle methods, this interface
049 * provides the <code>getServletConfig</code> method, which the servlet
050 * can use to get any startup information, and the <code>getServletInfo</code>
051 * method, which allows the servlet to return basic information about itself,
052 * such as author, version, and copyright.
053 *
054 * @author Various
055 * @version $Version$
056 *
057 * @see GenericServlet
058 * @see javax.servlet.http.HttpServlet
059 *
060 */
061
062
063 public interface Servlet {
064
065 /**
066 * Called by the servlet container to indicate to a servlet that the
067 * servlet is being placed into service.
068 *
069 * <p>The servlet container calls the <code>init</code>
070 * method exactly once after instantiating the servlet.
071 * The <code>init</code> method must complete successfully
072 * before the servlet can receive any requests.
073 *
074 * <p>The servlet container cannot place the servlet into service
075 * if the <code>init</code> method
076 * <ol>
077 * <li>Throws a <code>ServletException</code>
078 * <li>Does not return within a time period defined by the Web server
079 * </ol>
080 *
081 *
082 * @param config a <code>ServletConfig</code> object
083 * containing the servlet's
084 * configuration and initialization parameters
085 *
086 * @exception ServletException if an exception has occurred that
087 * interferes with the servlet's normal
088 * operation
089 *
090 * @see UnavailableException
091 * @see #getServletConfig
092 *
093 */
094
095 public void init(ServletConfig config) throws ServletException;
096
097
098
099 /**
100 *
101 * Returns a {@link ServletConfig} object, which contains
102 * initialization and startup parameters for this servlet.
103 * The <code>ServletConfig</code> object returned is the one
104 * passed to the <code>init</code> method.
105 *
106 * <p>Implementations of this interface are responsible for storing the
107 * <code>ServletConfig</code> object so that this
108 * method can return it. The {@link GenericServlet}
109 * class, which implements this interface, already does this.
110 *
111 * @return the <code>ServletConfig</code> object
112 * that initializes this servlet
113 *
114 * @see #init
115 *
116 */
117
118 public ServletConfig getServletConfig();
119
120
121
122 /**
123 * Called by the servlet container to allow the servlet to respond to
124 * a request.
125 *
126 * <p>This method is only called after the servlet's <code>init()</code>
127 * method has completed successfully.
128 *
129 * <p> The status code of the response always should be set for a servlet
130 * that throws or sends an error.
131 *
132 *
133 * <p>Servlets typically run inside multithreaded servlet containers
134 * that can handle multiple requests concurrently. Developers must
135 * be aware to synchronize access to any shared resources such as files,
136 * network connections, and as well as the servlet's class and instance
137 * variables.
138 * More information on multithreaded programming in Java is available in
139 * <a href="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">
140 * the Java tutorial on multi-threaded programming</a>.
141 *
142 *
143 * @param req the <code>ServletRequest</code> object that contains
144 * the client's request
145 *
146 * @param res the <code>ServletResponse</code> object that contains
147 * the servlet's response
148 *
149 * @exception ServletException if an exception occurs that interferes
150 * with the servlet's normal operation
151 *
152 * @exception IOException if an input or output exception occurs
153 *
154 */
155
156 public void service(ServletRequest req, ServletResponse res)
157 throws ServletException, IOException;
158
159
160
161 /**
162 * Returns information about the servlet, such
163 * as author, version, and copyright.
164 *
165 * <p>The string that this method returns should
166 * be plain text and not markup of any kind (such as HTML, XML,
167 * etc.).
168 *
169 * @return a <code>String</code> containing servlet information
170 *
171 */
172
173 public String getServletInfo();
174
175
176
177 /**
178 *
179 * Called by the servlet container to indicate to a servlet that the
180 * servlet is being taken out of service. This method is
181 * only called once all threads within the servlet's
182 * <code>service</code> method have exited or after a timeout
183 * period has passed. After the servlet container calls this
184 * method, it will not call the <code>service</code> method again
185 * on this servlet.
186 *
187 * <p>This method gives the servlet an opportunity
188 * to clean up any resources that are being held (for example, memory,
189 * file handles, threads) and make sure that any persistent state is
190 * synchronized with the servlet's current state in memory.
191 *
192 */
193
194 public void destroy();
195 }