blob: 5c7b170194d7b5a77e05cab894ecbc8facf1427e [file] [log] [blame]
Michael E. Rodriguezd8af66f2005-12-08 20:55:55 +00001/*
2 * Copyright 1999,2005 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
17package javax.servlet;
18
19import java.io.InputStream;
20import java.net.URL;
21import java.net.MalformedURLException;
22import java.util.Enumeration;
23
24
25/**
26 *
27 * Defines a set of methods that a servlet uses to communicate with its
28 * servlet container, for example, to get the MIME type of a file, dispatch
29 * requests, or write to a log file.
30 *
31 * <p>There is one context per "web application" per Java Virtual Machine. (A
32 * "web application" is a collection of servlets and content installed under a
33 * specific subset of the server's URL namespace such as <code>/catalog</code>
34 * and possibly installed via a <code>.war</code> file.)
35 *
36 * <p>In the case of a web
37 * application marked "distributed" in its deployment descriptor, there will
38 * be one context instance for each virtual machine. In this situation, the
39 * context cannot be used as a location to share global information (because
40 * the information won't be truly global). Use an external resource like
41 * a database instead.
42 *
43 * <p>The <code>ServletContext</code> object is contained within
44 * the {@link ServletConfig} object, which the Web server provides the
45 * servlet when the servlet is initialized.
46 *
47 * @author Various
48 * @version $Version$
49 *
50 * @see Servlet#getServletConfig
51 * @see ServletConfig#getServletContext
52 *
53 */
54
55public interface ServletContext {
56
57
58 /**
59 * Returns a <code>ServletContext</code> object that
60 * corresponds to a specified URL on the server.
61 *
62 * <p>This method allows servlets to gain
63 * access to the context for various parts of the server, and as
64 * needed obtain {@link RequestDispatcher} objects from the context.
65 * The given path must be begin with "/", is interpreted relative
66 * to the server's document root and is matched against the context roots of
67 * other web applications hosted on this container.
68 *
69 * <p>In a security conscious environment, the servlet container may
70 * return <code>null</code> for a given URL.
71 *
72 * @param uripath a <code>String</code> specifying the context path of
73 * another web application in the container.
74 * @return the <code>ServletContext</code> object that
75 * corresponds to the named URL, or null if either
76 none exists or the container wishes to restrict
77 * this access.
78 *
79 * @see RequestDispatcher
80 *
81 */
82
83 public ServletContext getContext(String uripath);
84
85
86
87 /**
88 * Returns the major version of the Java Servlet API that this
89 * servlet container supports. All implementations that comply
90 * with Version 2.1 must have this method
91 * return the integer 2.
92 *
93 * @return 2
94 *
95 */
96
97 public int getMajorVersion();
98
99
100
101 /**
102 * Returns the minor version of the Servlet API that this
103 * servlet container supports. All implementations that comply
104 * with Version 2.1 must have this method
105 * return the integer 1.
106 *
107 * @return 1
108 *
109 */
110
111 public int getMinorVersion();
112
113
114
115 /**
116 * Returns the MIME type of the specified file, or <code>null</code> if
117 * the MIME type is not known. The MIME type is determined
118 * by the configuration of the servlet container, and may be specified
119 * in a web application deployment descriptor. Common MIME
120 * types are <code>"text/html"</code> and <code>"image/gif"</code>.
121 *
122 *
123 * @param file a <code>String</code> specifying the name
124 * of a file
125 *
126 * @return a <code>String</code> specifying the file's MIME type
127 *
128 */
129
130 public String getMimeType(String file);
131
132 /**
133 * Returns a URL to the resource that is mapped to a specified
134 * path. The path must begin with a "/" and is interpreted
135 * as relative to the current context root.
136 *
137 * <p>This method allows the servlet container to make a resource
138 * available to servlets from any source. Resources
139 * can be located on a local or remote
140 * file system, in a database, or in a <code>.war</code> file.
141 *
142 * <p>The servlet container must implement the URL handlers
143 * and <code>URLConnection</code> objects that are necessary
144 * to access the resource.
145 *
146 * <p>This method returns <code>null</code>
147 * if no resource is mapped to the pathname.
148 *
149 * <p>Some containers may allow writing to the URL returned by
150 * this method using the methods of the URL class.
151 *
152 * <p>The resource content is returned directly, so be aware that
153 * requesting a <code>.jsp</code> page returns the JSP source code.
154 * Use a <code>RequestDispatcher</code> instead to include results of
155 * an execution.
156 *
157 * <p>This method has a different purpose than
158 * <code>java.lang.Class.getResource</code>,
159 * which looks up resources based on a class loader. This
160 * method does not use class loaders.
161 *
162 * @param path a <code>String</code> specifying
163 * the path to the resource
164 *
165 * @return the resource located at the named path,
166 * or <code>null</code> if there is no resource
167 * at that path
168 *
169 * @exception MalformedURLException if the pathname is not given in
170 * the correct form
171 *
172 */
173
174 public URL getResource(String path) throws MalformedURLException;
175
176
177
178 /**
179 * Returns the resource located at the named path as
180 * an <code>InputStream</code> object.
181 *
182 * <p>The data in the <code>InputStream</code> can be
183 * of any type or length. The path must be specified according
184 * to the rules given in <code>getResource</code>.
185 * This method returns <code>null</code> if no resource exists at
186 * the specified path.
187 *
188 * <p>Meta-information such as content length and content type
189 * that is available via <code>getResource</code>
190 * method is lost when using this method.
191 *
192 * <p>The servlet container must implement the URL handlers
193 * and <code>URLConnection</code> objects necessary to access
194 * the resource.
195 *
196 * <p>This method is different from
197 * <code>java.lang.Class.getResourceAsStream</code>,
198 * which uses a class loader. This method allows servlet containers
199 * to make a resource available
200 * to a servlet from any location, without using a class loader.
201 *
202 *
203 * @param path a <code>String</code> specifying the path
204 * to the resource
205 *
206 * @return the <code>InputStream</code> returned to the
207 * servlet, or <code>null</code> if no resource
208 * exists at the specified path
209 *
210 *
211 */
212
213 public InputStream getResourceAsStream(String path);
214
215
216
217
218 /**
219 *
220 * Returns a {@link RequestDispatcher} object that acts
221 * as a wrapper for the resource located at the given path.
222 * A <code>RequestDispatcher</code> object can be used to forward
223 * a request to the resource or to include the resource in a response.
224 * The resource can be dynamic or static.
225 *
226 * <p>The pathname must begin with a "/" and is interpreted as relative
227 * to the current context root. Use <code>getContext</code> to obtain
228 * a <code>RequestDispatcher</code> for resources in foreign contexts.
229 * This method returns <code>null</code> if the <code>ServletContext</code>
230 * cannot return a <code>RequestDispatcher</code>.
231 *
232 * @param path a <code>String</code> specifying the pathname
233 * to the resource
234 *
235 * @return a <code>RequestDispatcher</code> object
236 * that acts as a wrapper for the resource
237 * at the specified path
238 *
239 * @see RequestDispatcher
240 * @see ServletContext#getContext
241 *
242 */
243
244 public RequestDispatcher getRequestDispatcher(String path);
245
246
247
248 /**
249 *
250 * @deprecated As of Java Servlet API 2.1, with no direct replacement.
251 *
252 * <p>This method was originally defined to retrieve a servlet
253 * from a <code>ServletContext</code>. In this version, this method
254 * always returns <code>null</code> and remains only to preserve
255 * binary compatibility. This method will be permanently removed
256 * in a future version of the Java Servlet API.
257 *
258 * <p>In lieu of this method, servlets can share information using the
259 * <code>ServletContext</code> class and can perform shared business logic
260 * by invoking methods on common non-servlet classes.
261 *
262 */
263
264 public Servlet getServlet(String name) throws ServletException;
265
266
267
268
269
270
271 /**
272 *
273 * @deprecated As of Java Servlet API 2.0, with no replacement.
274 *
275 * <p>This method was originally defined to return an <code>Enumeration</code>
276 * of all the servlets known to this servlet context. In this
277 * version, this method always returns an empty enumeration and
278 * remains only to preserve binary compatibility. This method
279 * will be permanently removed in a future version of the Java
280 * Servlet API.
281 *
282 */
283
284 public Enumeration getServlets();
285
286
287
288
289
290
291 /**
292 * @deprecated As of Java Servlet API 2.1, with no replacement.
293 *
294 * <p>This method was originally defined to return an
295 * <code>Enumeration</code>
296 * of all the servlet names known to this context. In this version,
297 * this method always returns an empty <code>Enumeration</code> and
298 * remains only to preserve binary compatibility. This method will
299 * be permanently removed in a future version of the Java Servlet API.
300 *
301 */
302
303 public Enumeration getServletNames();
304
305
306
307
308
309 /**
310 *
311 * Writes the specified message to a servlet log file, usually
312 * an event log. The name and type of the servlet log file is
313 * specific to the servlet container.
314 *
315 *
316 * @param msg a <code>String</code> specifying the
317 * message to be written to the log file
318 *
319 */
320
321 public void log(String msg);
322
323
324
325
326
327 /**
328 * @deprecated As of Java Servlet API 2.1, use
329 * {@link #log(String message, Throwable throwable)}
330 * instead.
331 *
332 * <p>This method was originally defined to write an
333 * exception's stack trace and an explanatory error message
334 * to the servlet log file.
335 *
336 */
337
338 public void log(Exception exception, String msg);
339
340
341
342
343
344 /**
345 * Writes an explanatory message and a stack trace
346 * for a given <code>Throwable</code> exception
347 * to the servlet log file. The name and type of the servlet log
348 * file is specific to the servlet container, usually an event log.
349 *
350 *
351 * @param message a <code>String</code> that
352 * describes the error or exception
353 *
354 * @param throwable the <code>Throwable</code> error
355 * or exception
356 *
357 */
358
359 public void log(String message, Throwable throwable);
360
361
362
363
364
365 /**
366 * Returns a <code>String</code> containing the real path
367 * for a given virtual path. For example, the path "/index.html"
368 * returns the absolute file path on the server's filesystem would be
369 * served by a request for "http://host/contextPath/index.html",
370 * where contextPath is the context path of this ServletContext..
371 *
372 * <p>The real path returned will be in a form
373 * appropriate to the computer and operating system on
374 * which the servlet container is running, including the
375 * proper path separators. This method returns <code>null</code>
376 * if the servlet container cannot translate the virtual path
377 * to a real path for any reason (such as when the content is
378 * being made available from a <code>.war</code> archive).
379 *
380 *
381 * @param path a <code>String</code> specifying a virtual path
382 *
383 *
384 * @return a <code>String</code> specifying the real path,
385 * or null if the translation cannot be performed
386 *
387 *
388 */
389
390 public String getRealPath(String path);
391
392
393
394
395 /**
396 * Returns the name and version of the servlet container on which
397 * the servlet is running.
398 *
399 * <p>The form of the returned string is
400 * <i>servername</i>/<i>versionnumber</i>.
401 * For example, the JavaServer Web Development Kit may return the string
402 * <code>JavaServer Web Dev Kit/1.0</code>.
403 *
404 * <p>The servlet container may return other optional information
405 * after the primary string in parentheses, for example,
406 * <code>JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)</code>.
407 *
408 *
409 * @return a <code>String</code> containing at least the
410 * servlet container name and version number
411 *
412 */
413
414 public String getServerInfo();
415
416
417
418
419 /**
420 * Returns the servlet container attribute with the given name,
421 * or <code>null</code> if there is no attribute by that name.
422 * An attribute allows a servlet container to give the
423 * servlet additional information not
424 * already provided by this interface. See your
425 * server documentation for information about its attributes.
426 * A list of supported attributes can be retrieved using
427 * <code>getAttributeNames</code>.
428 *
429 * <p>The attribute is returned as a <code>java.lang.Object</code>
430 * or some subclass.
431 * Attribute names should follow the same convention as package
432 * names. The Java Servlet API specification reserves names
433 * matching <code>java.*</code>, <code>javax.*</code>,
434 * and <code>sun.*</code>.
435 *
436 *
437 * @param name a <code>String</code> specifying the name
438 * of the attribute
439 *
440 * @return an <code>Object</code> containing the value
441 * of the attribute, or <code>null</code>
442 * if no attribute exists matching the given
443 * name
444 *
445 * @see ServletContext#getAttributeNames
446 *
447 */
448
449 public Object getAttribute(String name);
450
451
452
453
454 /**
455 * Returns an <code>Enumeration</code> containing the
456 * attribute names available
457 * within this servlet context. Use the
458 * {@link #getAttribute} method with an attribute name
459 * to get the value of an attribute.
460 *
461 * @return an <code>Enumeration</code> of attribute
462 * names
463 *
464 * @see #getAttribute
465 *
466 */
467
468 public Enumeration getAttributeNames();
469
470
471
472
473 /**
474 *
475 * Binds an object to a given attribute name in this servlet context. If
476 * the name specified is already used for an attribute, this
477 * method will replace the attribute with the new to the new attribute.
478 *
479 * <p>Attribute names should follow the same convention as package
480 * names. The Java Servlet API specification reserves names
481 * matching <code>java.*</code>, <code>javax.*</code>, and
482 * <code>sun.*</code>.
483 *
484 *
485 * @param name a <code>String</code> specifying the name
486 * of the attribute
487 *
488 * @param object an <code>Object</code> representing the
489 * attribute to be bound
490 *
491 *
492 *
493 */
494
495 public void setAttribute(String name, Object object);
496
497
498
499
500
501 /**
502 * Removes the attribute with the given name from
503 * the servlet context. After removal, subsequent calls to
504 * {@link #getAttribute} to retrieve the attribute's value
505 * will return <code>null</code>.
506 *
507 *
508 * @param name a <code>String</code> specifying the name
509 * of the attribute to be removed
510 *
511 */
512
513 public void removeAttribute(String name);
514}
515
516