|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
GenericServlet.java | - | 0% | 0% | 0% |
|
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 | package javax.servlet; | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.util.Enumeration; | |
20 | ||
21 | /** | |
22 | * | |
23 | * Defines a generic, protocol-independent | |
24 | * servlet. To write an HTTP servlet for use on the | |
25 | * Web, extend {@link javax.servlet.http.HttpServlet} instead. | |
26 | * | |
27 | * <p><code>GenericServlet</code> implements the <code>Servlet</code> | |
28 | * and <code>ServletConfig</code> interfaces. <code>GenericServlet</code> | |
29 | * may be directly extended by a servlet, although it's more common to extend | |
30 | * a protocol-specific subclass such as <code>HttpServlet</code>. | |
31 | * | |
32 | * <p><code>GenericServlet</code> makes writing servlets | |
33 | * easier. It provides simple versions of the lifecycle methods | |
34 | * <code>init</code> and <code>destroy</code> and of the methods | |
35 | * in the <code>ServletConfig</code> interface. <code>GenericServlet</code> | |
36 | * also implements the <code>log</code> method, declared in the | |
37 | * <code>ServletContext</code> interface. | |
38 | * | |
39 | * <p>To write a generic servlet, you need only | |
40 | * override the abstract <code>service</code> method. | |
41 | * | |
42 | * | |
43 | * @author Various | |
44 | * @version $Version$ | |
45 | * | |
46 | * | |
47 | * | |
48 | */ | |
49 | ||
50 | ||
51 | public abstract class GenericServlet | |
52 | implements Servlet, ServletConfig, java.io.Serializable | |
53 | { | |
54 | ||
55 | private transient ServletConfig config; | |
56 | ||
57 | ||
58 | /** | |
59 | * | |
60 | * Does nothing. All of the servlet initialization | |
61 | * is done by one of the <code>init</code> methods. | |
62 | * | |
63 | */ | |
64 | ||
65 | 0 | public GenericServlet() { } |
66 | ||
67 | ||
68 | ||
69 | /** | |
70 | * Called by the servlet container to indicate to a servlet that the | |
71 | * servlet is being taken out of service. See {@link Servlet#destroy}. | |
72 | * | |
73 | * | |
74 | */ | |
75 | ||
76 | 0 | public void destroy() { |
77 | } | |
78 | ||
79 | ||
80 | ||
81 | /** | |
82 | * Returns a <code>String</code> containing the value of the named | |
83 | * initialization parameter, or <code>null</code> if the parameter does | |
84 | * not exist. See {@link ServletConfig#getInitParameter}. | |
85 | * | |
86 | * <p>This method is supplied for convenience. It gets the | |
87 | * value of the named parameter from the servlet's | |
88 | * <code>ServletConfig</code> object. | |
89 | * | |
90 | * @param name a <code>String</code> specifying the name | |
91 | * of the initialization parameter | |
92 | * | |
93 | * @return String a <code>String</code> containing the value | |
94 | * of the initialization parameter | |
95 | * | |
96 | */ | |
97 | ||
98 | 0 | public String getInitParameter(String name) { |
99 | 0 | return getServletConfig().getInitParameter(name); |
100 | } | |
101 | ||
102 | ||
103 | ||
104 | /** | |
105 | * Returns the names of the servlet's initialization parameters | |
106 | * as an <code>Enumeration</code> of <code>String</code> objects, | |
107 | * or an empty <code>Enumeration</code> if the servlet has no | |
108 | * initialization parameters. See {@link | |
109 | * ServletConfig#getInitParameterNames}. | |
110 | * | |
111 | * <p>This method is supplied for convenience. It gets the | |
112 | * parameter names from the servlet's <code>ServletConfig</code> object. | |
113 | * | |
114 | * | |
115 | * @return Enumeration an enumeration of <code>String</code> | |
116 | * objects containing the names of | |
117 | * the servlet's initialization parameters | |
118 | * | |
119 | */ | |
120 | ||
121 | 0 | public Enumeration getInitParameterNames() { |
122 | 0 | return getServletConfig().getInitParameterNames(); |
123 | } | |
124 | ||
125 | ||
126 | ||
127 | ||
128 | ||
129 | /** | |
130 | * Returns this servlet's {@link ServletConfig} object. | |
131 | * | |
132 | * @return ServletConfig the <code>ServletConfig</code> object | |
133 | * that initialized this servlet | |
134 | * | |
135 | */ | |
136 | ||
137 | 0 | public ServletConfig getServletConfig() { |
138 | 0 | return config; |
139 | } | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | /** | |
145 | * Returns a reference to the {@link ServletContext} in which this servlet | |
146 | * is running. See {@link ServletConfig#getServletContext}. | |
147 | * | |
148 | * <p>This method is supplied for convenience. It gets the | |
149 | * context from the servlet's <code>ServletConfig</code> object. | |
150 | * | |
151 | * | |
152 | * @return ServletContext the <code>ServletContext</code> object | |
153 | * passed to this servlet by the <code>init</code> | |
154 | * method | |
155 | * | |
156 | */ | |
157 | ||
158 | 0 | public ServletContext getServletContext() { |
159 | 0 | return getServletConfig().getServletContext(); |
160 | } | |
161 | ||
162 | ||
163 | ||
164 | ||
165 | ||
166 | /** | |
167 | * Returns information about the servlet, such as | |
168 | * author, version, and copyright. | |
169 | * By default, this method returns an empty string. Override this method | |
170 | * to have it return a meaningful value. See {@link | |
171 | * Servlet#getServletInfo}. | |
172 | * | |
173 | * | |
174 | * @return String information about this servlet, by default an | |
175 | * empty string | |
176 | * | |
177 | */ | |
178 | ||
179 | 0 | public String getServletInfo() { |
180 | 0 | return ""; |
181 | } | |
182 | ||
183 | ||
184 | ||
185 | ||
186 | /** | |
187 | * | |
188 | * Called by the servlet container to indicate to a servlet that the | |
189 | * servlet is being placed into service. See {@link Servlet#init}. | |
190 | * | |
191 | * <p>This implementation stores the {@link ServletConfig} | |
192 | * object it receives from the servlet container for later use. | |
193 | * When overriding this form of the method, call | |
194 | * <code>super.init(config)</code>. | |
195 | * | |
196 | * @param config the <code>ServletConfig</code> object | |
197 | * that contains configutation | |
198 | * information for this servlet | |
199 | * | |
200 | * @exception ServletException if an exception occurs that | |
201 | * interrupts the servlet's normal | |
202 | * operation | |
203 | * | |
204 | * | |
205 | * @see UnavailableException | |
206 | * | |
207 | */ | |
208 | ||
209 | 0 | public void init(ServletConfig config) throws ServletException { |
210 | 0 | this.config = config; |
211 | 0 | this.init(); |
212 | } | |
213 | ||
214 | ||
215 | ||
216 | ||
217 | ||
218 | /** | |
219 | * | |
220 | * A convenience method which can be overridden so that there's no need | |
221 | * to call <code>super.init(config)</code>. | |
222 | * | |
223 | * <p>Instead of overriding {@link #init(ServletConfig)}, simply override | |
224 | * this method and it will be called by | |
225 | * <code>GenericServlet.init(ServletConfig config)</code>. | |
226 | * The <code>ServletConfig</code> object can still be retrieved via {@link | |
227 | * #getServletConfig}. | |
228 | * | |
229 | * @exception ServletException if an exception occurs that | |
230 | * interrupts the servlet's | |
231 | * normal operation | |
232 | * | |
233 | */ | |
234 | ||
235 | 0 | public void init() throws ServletException { |
236 | ||
237 | } | |
238 | ||
239 | ||
240 | ||
241 | ||
242 | /** | |
243 | * | |
244 | * Writes the specified message to a servlet log file, prepended by the | |
245 | * servlet's name. See {@link ServletContext#log(String)}. | |
246 | * | |
247 | * @param msg a <code>String</code> specifying | |
248 | * the message to be written to the log file | |
249 | * | |
250 | */ | |
251 | ||
252 | 0 | public void log(String msg) { |
253 | 0 | getServletContext().log(getServletName() + ": "+ msg); |
254 | } | |
255 | ||
256 | ||
257 | ||
258 | ||
259 | /** | |
260 | * Writes an explanatory message and a stack trace | |
261 | * for a given <code>Throwable</code> exception | |
262 | * to the servlet log file, prepended by the servlet's name. | |
263 | * See {@link ServletContext#log(String, Throwable)}. | |
264 | * | |
265 | * | |
266 | * @param message a <code>String</code> that describes | |
267 | * the error or exception | |
268 | * | |
269 | * @param t the <code>java.lang.Throwable</code> error | |
270 | * or exception | |
271 | * | |
272 | * | |
273 | */ | |
274 | ||
275 | 0 | public void log(String message, Throwable t) { |
276 | 0 | getServletContext().log(getServletName() + ": " + message, t); |
277 | } | |
278 | ||
279 | ||
280 | ||
281 | /** | |
282 | * Called by the servlet container to allow the servlet to respond to | |
283 | * a request. See {@link Servlet#service}. | |
284 | * | |
285 | * <p>This method is declared abstract so subclasses, such as | |
286 | * <code>HttpServlet</code>, must override it. | |
287 | * | |
288 | * | |
289 | * | |
290 | * @param req the <code>ServletRequest</code> object | |
291 | * that contains the client's request | |
292 | * | |
293 | * @param res the <code>ServletResponse</code> object | |
294 | * that will contain the servlet's response | |
295 | * | |
296 | * @exception ServletException if an exception occurs that | |
297 | * interferes with the servlet's | |
298 | * normal operation occurred | |
299 | * | |
300 | * @exception IOException if an input or output | |
301 | * exception occurs | |
302 | * | |
303 | */ | |
304 | ||
305 | public abstract void service(ServletRequest req, ServletResponse res) | |
306 | throws ServletException, IOException; | |
307 | ||
308 | ||
309 | ||
310 | /** | |
311 | * Returns the name of this servlet instance. | |
312 | * See {@link ServletConfig#getServletName}. | |
313 | * | |
314 | * @return the name of this servlet instance | |
315 | * | |
316 | * | |
317 | * | |
318 | */ | |
319 | ||
320 | 0 | public String getServletName() { |
321 | 0 | return config.getServletName(); |
322 | } | |
323 | } |
|