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 * Defines methods that all servlets must implement.
026 * <p/>
027 * <p>A servlet is a small Java program that runs within a Web server.
028 * Servlets receive and respond to requests from Web clients,
029 * usually across HTTP, the HyperText Transfer Protocol.
030 * <p/>
031 * <p>To implement this interface, you can write a generic servlet
032 * that extends
033 * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that
034 * extends <code>javax.servlet.http.HttpServlet</code>.
035 * <p/>
036 * <p>This interface defines methods to initialize a servlet,
037 * to service requests, and to remove a servlet from the server.
038 * These are known as life-cycle methods and are called in the
039 * following sequence:
040 * <ol>
041 * <li>The servlet is constructed, then initialized with the <code>init</code> method.
042 * <li>Any calls from clients to the <code>service</code> method are handled.
043 * <li>The servlet is taken out of service, then destroyed with the
044 * <code>destroy</code> method, then garbage collected and finalized.
045 * </ol>
046 * <p/>
047 * <p>In addition to the life-cycle methods, this interface
048 * provides the <code>getServletConfig</code> method, which the servlet
049 * can use to get any startup information, and the <code>getServletInfo</code>
050 * method, which allows the servlet to return basic information about itself,
051 * such as author, version, and copyright.
052 *
053 * @version $Rev: 788194 $ $Date: 2009-06-24 18:05:48 -0400 (Wed, 24 Jun 2009) $
054 * @see GenericServlet
055 * @see javax.servlet.http.HttpServlet
056 */
057
058 public interface Servlet {
059
060 /**
061 * Called by the servlet container to indicate to a servlet that the
062 * servlet is being placed into service.
063 * <p/>
064 * <p>The servlet container calls the <code>init</code>
065 * method exactly once after instantiating the servlet.
066 * The <code>init</code> method must complete successfully
067 * before the servlet can receive any requests.
068 * <p/>
069 * <p>The servlet container cannot place the servlet into service
070 * if the <code>init</code> method
071 * <ol>
072 * <li>Throws a <code>ServletException</code>
073 * <li>Does not return within a time period defined by the Web server
074 * </ol>
075 *
076 * @param config a <code>ServletConfig</code> object
077 * containing the servlet's
078 * configuration and initialization parameters
079 * @throws ServletException if an exception has occurred that
080 * interferes with the servlet's normal
081 * operation
082 * @see UnavailableException
083 * @see #getServletConfig
084 */
085 public void init(ServletConfig config) throws ServletException;
086
087 /**
088 * Returns a {@link ServletConfig} object, which contains
089 * initialization and startup parameters for this servlet.
090 * The <code>ServletConfig</code> object returned is the one
091 * passed to the <code>init</code> method.
092 * <p/>
093 * <p>Implementations of this interface are responsible for storing the
094 * <code>ServletConfig</code> object so that this
095 * method can return it. The {@link GenericServlet}
096 * class, which implements this interface, already does this.
097 *
098 * @return the <code>ServletConfig</code> object
099 * that initializes this servlet
100 * @see #init
101 */
102 public ServletConfig getServletConfig();
103
104 /**
105 * Called by the servlet container to allow the servlet to respond to
106 * a request.
107 * <p/>
108 * <p>This method is only called after the servlet's <code>init()</code>
109 * method has completed successfully.
110 * <p/>
111 * <p> The status code of the response always should be set for a servlet
112 * that throws or sends an error.
113 * <p/>
114 * <p/>
115 * <p>Servlets typically run inside multithreaded servlet containers
116 * that can handle multiple requests concurrently. Developers must
117 * be aware to synchronize access to any shared resources such as files,
118 * network connections, and as well as the servlet's class and instance
119 * variables.
120 * More information on multithreaded programming in Java is available in
121 * <a href="http://java.sun.com/Series/Tutorial/java/threads/multithreaded.html">
122 * the Java tutorial on multi-threaded programming</a>.
123 *
124 * @param req the <code>ServletRequest</code> object that contains
125 * the client's request
126 * @param res the <code>ServletResponse</code> object that contains
127 * the servlet's response
128 * @throws ServletException if an exception occurs that interferes
129 * with the servlet's normal operation
130 * @throws IOException if an input or output exception occurs
131 */
132 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
133
134 /**
135 * Returns information about the servlet, such
136 * as author, version, and copyright.
137 * <p/>
138 * <p>The string that this method returns should
139 * be plain text and not markup of any kind (such as HTML, XML,
140 * etc.).
141 *
142 * @return a <code>String</code> containing servlet information
143 */
144 public String getServletInfo();
145
146 /**
147 * Called by the servlet container to indicate to a servlet that the
148 * servlet is being taken out of service. This method is
149 * only called once all threads within the servlet's
150 * <code>service</code> method have exited or after a timeout
151 * period has passed. After the servlet container calls this
152 * method, it will not call the <code>service</code> method again
153 * on this servlet.
154 * <p/>
155 * <p>This method gives the servlet an opportunity
156 * to clean up any resources that are being held (for example, memory,
157 * file handles, threads) and make sure that any persistent state is
158 * synchronized with the servlet's current state in memory.
159 */
160 public void destroy();
161 }