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