View Javadoc

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