View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.servlet;
21  
22  import java.io.IOException;
23  
24  
25  /**
26   * Defines methods that all servlets must implement.
27   *
28   * <p>A servlet is a small Java program that runs within a Web server.
29   * Servlets receive and respond to requests from Web clients,
30   * usually across HTTP, the HyperText Transfer Protocol. 
31   *
32   * <p>To implement this interface, you can write a generic servlet
33   * that extends
34   * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that
35   * extends <code>javax.servlet.http.HttpServlet</code>.
36   *
37   * <p>This interface defines methods to initialize a servlet,
38   * to service requests, and to remove a servlet from the server.
39   * These are known as life-cycle methods and are called in the
40   * following sequence:
41   * <ol>
42   * <li>The servlet is constructed, then initialized with the <code>init</code> method.
43   * <li>Any calls from clients to the <code>service</code> method are handled.
44   * <li>The servlet is taken out of service, then destroyed with the 
45   * <code>destroy</code> method, then garbage collected and finalized.
46   * </ol>
47   *
48   * <p>In addition to the life-cycle methods, this interface
49   * provides the <code>getServletConfig</code> method, which the servlet 
50   * can use to get any startup information, and the <code>getServletInfo</code>
51   * method, which allows the servlet to return basic information about itself,
52   * such as author, version, and copyright.
53   *
54   * @author 	Various
55   * @version 	$Version$
56   *
57   * @see 	GenericServlet
58   * @see 	javax.servlet.http.HttpServlet
59   *
60   */
61  
62  
63  public interface Servlet {
64  
65      /**
66       * Called by the servlet container to indicate to a servlet that the 
67       * servlet is being placed into service.
68       *
69       * <p>The servlet container calls the <code>init</code>
70       * method exactly once after instantiating the servlet.
71       * The <code>init</code> method must complete successfully
72       * before the servlet can receive any requests.
73       *
74       * <p>The servlet container cannot place the servlet into service
75       * if the <code>init</code> method
76       * <ol>
77       * <li>Throws a <code>ServletException</code>
78       * <li>Does not return within a time period defined by the Web server
79       * </ol>
80       *
81       *
82       * @param config			a <code>ServletConfig</code> object 
83       *					containing the servlet's
84       * 					configuration and initialization parameters
85       *
86       * @exception ServletException 	if an exception has occurred that
87       *					interferes with the servlet's normal
88       *					operation
89       *
90       * @see 				UnavailableException
91       * @see 				#getServletConfig
92       *
93       */
94  
95      public void init(ServletConfig config) throws ServletException;
96      
97      
98  
99      /**
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 }