001 /* 002 * Copyright 2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package javax.servlet; 018 019 import java.io.IOException; 020 021 022 /** 023 * Defines methods that all servlets must implement. 024 * 025 * <p>A servlet is a small Java program that runs within a Web server. 026 * Servlets receive and respond to requests from Web clients, 027 * usually across HTTP, the HyperText Transfer Protocol. 028 * 029 * <p>To implement this interface, you can write a generic servlet 030 * that extends 031 * <code>javax.servlet.GenericServlet</code> or an HTTP servlet that 032 * extends <code>javax.servlet.http.HttpServlet</code>. 033 * 034 * <p>This interface defines methods to initialize a servlet, 035 * to service requests, and to remove a servlet from the server. 036 * These are known as life-cycle methods and are called in the 037 * following sequence: 038 * <ol> 039 * <li>The servlet is constructed, then initialized with the <code>init</code> method. 040 * <li>Any calls from clients to the <code>service</code> method are handled. 041 * <li>The servlet is taken out of service, then destroyed with the 042 * <code>destroy</code> method, then garbage collected and finalized. 043 * </ol> 044 * 045 * <p>In addition to the life-cycle methods, this interface 046 * provides the <code>getServletConfig</code> method, which the servlet 047 * can use to get any startup information, and the <code>getServletInfo</code> 048 * method, which allows the servlet to return basic information about itself, 049 * such as author, version, and copyright. 050 * 051 * @author Various 052 * @version $Version$ 053 * 054 * @see GenericServlet 055 * @see javax.servlet.http.HttpServlet 056 * 057 */ 058 059 060 public interface Servlet { 061 062 /** 063 * Called by the servlet container to indicate to a servlet that the 064 * servlet is being placed into service. 065 * 066 * <p>The servlet container calls the <code>init</code> 067 * method exactly once after instantiating the servlet. 068 * The <code>init</code> method must complete successfully 069 * before the servlet can receive any requests. 070 * 071 * <p>The servlet container cannot place the servlet into service 072 * if the <code>init</code> method 073 * <ol> 074 * <li>Throws a <code>ServletException</code> 075 * <li>Does not return within a time period defined by the Web server 076 * </ol> 077 * 078 * 079 * @param config a <code>ServletConfig</code> object 080 * containing the servlet's 081 * configuration and initialization parameters 082 * 083 * @exception ServletException if an exception has occurred that 084 * interferes with the servlet's normal 085 * operation 086 * 087 * @see UnavailableException 088 * @see #getServletConfig 089 * 090 */ 091 092 public void init(ServletConfig config) throws ServletException; 093 094 095 096 /** 097 * 098 * Returns a {@link ServletConfig} object, which contains 099 * 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 }