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 //
021 // This source code implements specifications defined by the Java
022 // Community Process. In order to remain compliant with the specification
023 // DO NOT add / change / or delete method signatures!
024 //
025
026 package javax.servlet;
027
028 import java.io.InputStream;
029 import java.net.MalformedURLException;
030 import java.net.URL;
031 import java.util.Enumeration;
032 import java.util.Set;
033
034 /**
035 * Defines a set of methods that a servlet uses to communicate with its
036 * servlet container, for example, to get the MIME type of a file, dispatch
037 * requests, or write to a log file.
038 *
039 * <p>There is one context per "web application" per Java Virtual Machine. (A
040 * "web application" is a collection of servlets and content installed under a
041 * specific subset of the server's URL namespace such as <code>/catalog</code>
042 * and possibly installed via a <code>.war</code> file.)
043 *
044 * <p>In the case of a web
045 * application marked "distributed" in its deployment descriptor, there will
046 * be one context instance for each virtual machine. In this situation, the
047 * context cannot be used as a location to share global information (because
048 * the information won't be truly global). Use an external resource like
049 * a database instead.
050 *
051 * <p>The <code>ServletContext</code> object is contained within
052 * the {@link ServletConfig} object, which the Web server provides the
053 * servlet when the servlet is initialized.
054 *
055 * @see Servlet#getServletConfig
056 * @see ServletConfig#getServletContext
057 *
058 * @version $Rev: 467553 $ $Date: 2006-10-24 21:01:51 -0700 (Tue, 24 Oct 2006) $
059 */
060 public interface ServletContext {
061 /**
062 * Returns a <code>ServletContext</code> object that
063 * corresponds to a specified URL on the server.
064 *
065 * <p>This method allows servlets to gain
066 * access to the context for various parts of the server, and as
067 * needed obtain {@link RequestDispatcher} objects from the context.
068 * The given path must be begin with "/", is interpreted relative
069 * to the server's document root and is matched against the context roots of
070 * other web applications hosted on this container.
071 *
072 * <p>In a security conscious environment, the servlet container may
073 * return <code>null</code> for a given URL.
074 *
075 * @param uripath a <code>String</code> specifying the context path of
076 * another web application in the container.
077 * @return the <code>ServletContext</code> object that
078 * corresponds to the named URL, or null if either none exists or the
079 * container wishes to restrict this access.
080 *
081 * @see RequestDispatcher
082 */
083 public ServletContext getContext(String uripath);
084
085 /**
086 * Returns the major version of the Java Servlet API that this
087 * servlet container supports. All implementations that comply
088 * with Version 2.4 must have this method
089 * return the integer 2.
090 *
091 * @return 2
092 */
093 public int getMajorVersion();
094
095 /**
096 * Returns the minor version of the Servlet API that this
097 * servlet container supports. All implementations that comply
098 * with Version 2.4 must have this method
099 * return the integer 4.
100 *
101 * @return 4
102 */
103 public int getMinorVersion();
104
105 /**
106 * Returns the MIME type of the specified file, or <code>null</code> if
107 * the MIME type is not known. The MIME type is determined
108 * by the configuration of the servlet container, and may be specified
109 * in a web application deployment descriptor. Common MIME
110 * types are <code>"text/html"</code> and <code>"image/gif"</code>.
111 *
112 * @param file a <code>String</code> specifying the name
113 * of a file
114 *
115 * @return a <code>String</code> specifying the file's MIME type
116 */
117 public String getMimeType(String file);
118
119 /**
120 * Returns a directory-like listing of all the paths to resources within
121 * the web application whose longest sub-path matches the supplied path
122 * argument. Paths indicating subdirectory paths end with a '/'. The
123 * returned paths are all relative to the root of the web application and
124 * have a leading '/'. For example, for a web application containing<br>
125 * <br>
126 * /welcome.html<br>
127 * /catalog/index.html<br>
128 * /catalog/products.html<br>
129 * /catalog/offers/books.html<br>
130 * /catalog/offers/music.html<br>
131 * /customer/login.jsp<br>
132 * /WEB-INF/web.xml<br>
133 * /WEB-INF/classes/com.acme.OrderServlet.class,<br><br>
134 *
135 * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}<br>
136 * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.<br>
137 *
138 * @param path the partial path used to match the resources,
139 * which must start with a /
140 * @return a Set containing the directory listing, or null if there are no
141 * resources in the web application whose path begins with the supplied path.
142 *
143 * @since Servlet 2.3
144 */
145 public Set getResourcePaths(String path);
146
147 /**
148 * Returns a URL to the resource that is mapped to a specified
149 * path. The path must begin with a "/" and is interpreted
150 * as relative to the current context root.
151 *
152 * <p>This method allows the servlet container to make a resource
153 * available to servlets from any source. Resources
154 * can be located on a local or remote
155 * file system, in a database, or in a <code>.war</code> file.
156 *
157 * <p>The servlet container must implement the URL handlers
158 * and <code>URLConnection</code> objects that are necessary
159 * to access the resource.
160 *
161 * <p>This method returns <code>null</code>
162 * if no resource is mapped to the pathname.
163 *
164 * <p>Some containers may allow writing to the URL returned by
165 * this method using the methods of the URL class.
166 *
167 * <p>The resource content is returned directly, so be aware that
168 * requesting a <code>.jsp</code> page returns the JSP source code.
169 * Use a <code>RequestDispatcher</code> instead to include results of
170 * an execution.
171 *
172 * <p>This method has a different purpose than
173 * <code>java.lang.Class.getResource</code>,
174 * which looks up resources based on a class loader. This
175 * method does not use class loaders.
176 *
177 * @param path a <code>String</code> specifying the path to the resource
178 *
179 * @return the resource located at the named path, or <code>null</code>
180 * if there is no resource at that path
181 *
182 * @exception MalformedURLException if the pathname is not given in
183 * the correct form
184 */
185 public URL getResource(String path) throws MalformedURLException;
186
187 /**
188 * Returns the resource located at the named path as
189 * an <code>InputStream</code> object.
190 *
191 * <p>The data in the <code>InputStream</code> can be
192 * of any type or length. The path must be specified according
193 * to the rules given in <code>getResource</code>.
194 * This method returns <code>null</code> if no resource exists at
195 * the specified path.
196 *
197 * <p>Meta-information such as content length and content type
198 * that is available via <code>getResource</code>
199 * method is lost when using this method.
200 *
201 * <p>The servlet container must implement the URL handlers
202 * and <code>URLConnection</code> objects necessary to access
203 * the resource.
204 *
205 * <p>This method is different from
206 * <code>java.lang.Class.getResourceAsStream</code>,
207 * which uses a class loader. This method allows servlet containers
208 * to make a resource available
209 * to a servlet from any location, without using a class loader.
210 *
211 * @param path a <code>String</code> specifying the path
212 * to the resource
213 *
214 * @return the <code>InputStream</code> returned to the
215 * servlet, or <code>null</code> if no resource exists at the
216 * specified path
217 */
218 public InputStream getResourceAsStream(String path);
219
220 /**
221 * Returns a {@link RequestDispatcher} object that acts
222 * as a wrapper for the resource located at the given path.
223 * A <code>RequestDispatcher</code> object can be used to forward
224 * a request to the resource or to include the resource in a response.
225 * The resource can be dynamic or static.
226 *
227 * <p>The pathname must begin with a "/" and is interpreted as relative
228 * to the current context root. Use <code>getContext</code> to obtain
229 * a <code>RequestDispatcher</code> for resources in foreign contexts.
230 * This method returns <code>null</code> if the <code>ServletContext</code>
231 * cannot return a <code>RequestDispatcher</code>.
232 *
233 * @param path a <code>String</code> specifying the pathname
234 * to the resource
235 *
236 * @return a <code>RequestDispatcher</code> object that acts as a wrapper
237 * for the resource at the specified path, or <code>null</code> if the
238 * <code>ServletContext</code> cannot return a <code>RequestDispatcher</code>
239 *
240 * @see RequestDispatcher
241 * @see ServletContext#getContext
242 */
243 public RequestDispatcher getRequestDispatcher(String path);
244
245 /**
246 * Returns a {@link RequestDispatcher} object that acts
247 * as a wrapper for the named servlet.
248 *
249 * <p>Servlets (and JSP pages also) may be given names via server
250 * administration or via a web application deployment descriptor.
251 * A servlet instance can determine its name using
252 * {@link ServletConfig#getServletName}.
253 *
254 * <p>This method returns <code>null</code> if the
255 * <code>ServletContext</code>
256 * cannot return a <code>RequestDispatcher</code> for any reason.
257 *
258 * @param name a <code>String</code> specifying the name
259 * of a servlet to wrap
260 *
261 * @return a <code>RequestDispatcher</code> object
262 * that acts as a wrapper for the named servlet,
263 * or <code>null</code> if the <code>ServletContext</code>
264 * cannot return a <code>RequestDispatcher</code>
265 *
266 * @see RequestDispatcher
267 * @see ServletContext#getContext
268 * @see ServletConfig#getServletName
269 */
270 public RequestDispatcher getNamedDispatcher(String name);
271
272 /**
273 * @deprecated As of Java Servlet API 2.1, with no direct replacement.
274 *
275 * <p>This method was originally defined to retrieve a servlet
276 * from a <code>ServletContext</code>. In this version, this method
277 * always returns <code>null</code> and remains only to preserve
278 * binary compatibility. This method will be permanently removed
279 * in a future version of the Java Servlet API.
280 *
281 * <p>In lieu of this method, servlets can share information using the
282 * <code>ServletContext</code> class and can perform shared business logic
283 * by invoking methods on common non-servlet classes.
284 */
285 public Servlet getServlet(String name) throws ServletException;
286
287 /**
288 * @deprecated As of Java Servlet API 2.0, with no replacement.
289 *
290 * <p>This method was originally defined to return an <code>Enumeration</code>
291 * of all the servlets known to this servlet context. In this
292 * version, this method always returns an empty enumeration and
293 * remains only to preserve binary compatibility. This method
294 * will be permanently removed in a future version of the Java
295 * Servlet API.
296 */
297 public Enumeration getServlets();
298
299 /**
300 * @deprecated As of Java Servlet API 2.1, with no replacement.
301 *
302 * <p>This method was originally defined to return an
303 * <code>Enumeration</code>
304 * of all the servlet names known to this context. In this version,
305 * this method always returns an empty <code>Enumeration</code> and
306 * remains only to preserve binary compatibility. This method will
307 * be permanently removed in a future version of the Java Servlet API.
308 */
309 public Enumeration getServletNames();
310
311 /**
312 * Writes the specified message to a servlet log file, usually
313 * an event log. The name and type of the servlet log file is
314 * specific to the servlet container.
315 *
316 * @param msg a <code>String</code> specifying the
317 * message to be written to the log file
318 */
319 public void log(String msg);
320
321 /**
322 * @deprecated As of Java Servlet API 2.1, use
323 * {@link #log(String message, Throwable throwable)}
324 * instead.
325 *
326 * <p>This method was originally defined to write an
327 * exception's stack trace and an explanatory error message
328 * to the servlet log file.
329 */
330 public void log(Exception exception, String msg);
331
332 /**
333 * Writes an explanatory message and a stack trace
334 * for a given <code>Throwable</code> exception
335 * to the servlet log file. The name and type of the servlet log
336 * file is specific to the servlet container, usually an event log.
337 *
338 * @param message a <code>String</code> that
339 * describes the error or exception
340 *
341 * @param throwable the <code>Throwable</code> error
342 * or exception
343 */
344 public void log(String message, Throwable throwable);
345
346 /**
347 * Returns a <code>String</code> containing the real path
348 * for a given virtual path. For example, the path "/index.html"
349 * returns the absolute file path on the server's filesystem would be
350 * served by a request for "http://host/contextPath/index.html",
351 * where contextPath is the context path of this ServletContext..
352 *
353 * <p>The real path returned will be in a form
354 * appropriate to the computer and operating system on
355 * which the servlet container is running, including the
356 * proper path separators. This method returns <code>null</code>
357 * if the servlet container cannot translate the virtual path
358 * to a real path for any reason (such as when the content is
359 * being made available from a <code>.war</code> archive).
360 *
361 * @param path a <code>String</code> specifying a virtual path
362 *
363 * @return a <code>String</code> specifying the real path,
364 * or null if the translation cannot be performed
365 */
366 public String getRealPath(String path);
367
368 /**
369 * Returns the name and version of the servlet container on which
370 * the servlet is running.
371 *
372 * <p>The form of the returned string is
373 * <i>servername</i>/<i>versionnumber</i>.
374 * For example, the JavaServer Web Development Kit may return the string
375 * <code>JavaServer Web Dev Kit/1.0</code>.
376 *
377 * <p>The servlet container may return other optional information
378 * after the primary string in parentheses, for example,
379 * <code>JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)</code>.
380 *
381 * @return a <code>String</code> containing at least the
382 * servlet container name and version number
383 */
384 public String getServerInfo();
385
386 /**
387 * Returns a <code>String</code> containing the value of the named
388 * context-wide initialization parameter, or <code>null</code> if the
389 * parameter does not exist.
390 *
391 * <p>This method can make available configuration information useful
392 * to an entire "web application". For example, it can provide a
393 * webmaster's email address or the name of a system that holds
394 * critical data.
395 *
396 * @param name a <code>String</code> containing the name of the
397 * parameter whose value is requested
398 *
399 * @return a <code>String</code> containing at least the
400 * servlet container name and version number
401 *
402 * @see ServletConfig#getInitParameter
403 */
404 public String getInitParameter(String name);
405
406 /**
407 * Returns the names of the context's initialization parameters as an
408 * <code>Enumeration</code> of <code>String</code> objects, or an
409 * empty <code>Enumeration</code> if the context has no initialization
410 * parameters.
411 *
412 * @return an <code>Enumeration</code> of <code>String</code>
413 * objects containing the names of the context's initialization parameters
414 *
415 * @see ServletConfig#getInitParameter
416 */
417 public Enumeration getInitParameterNames();
418
419
420 /**
421 * Returns the servlet container attribute with the given name,
422 * or <code>null</code> if there is no attribute by that name.
423 * An attribute allows a servlet container to give the
424 * servlet additional information not
425 * already provided by this interface. See your
426 * server documentation for information about its attributes.
427 * A list of supported attributes can be retrieved using
428 * <code>getAttributeNames</code>.
429 *
430 * <p>The attribute is returned as a <code>java.lang.Object</code>
431 * or some subclass.
432 * Attribute names should follow the same convention as package
433 * names. The Java Servlet API specification reserves names
434 * matching <code>java.*</code>, <code>javax.*</code>,
435 * and <code>sun.*</code>.
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> if no attribute
442 * exists matching the given name
443 *
444 * @see ServletContext#getAttributeNames
445 */
446 public Object getAttribute(String name);
447
448 /**
449 * Returns an <code>Enumeration</code> containing the
450 * attribute names available
451 * within this servlet context. Use the
452 * {@link #getAttribute} method with an attribute name
453 * to get the value of an attribute.
454 *
455 * @return an <code>Enumeration</code> of attribute names
456 *
457 * @see #getAttribute
458 */
459 public Enumeration getAttributeNames();
460
461 /**
462 * Binds an object to a given attribute name in this servlet context. If
463 * the name specified is already used for an attribute, this
464 * method will replace the attribute with the new to the new attribute.
465 * <p>If listeners are configured on the <code>ServletContext</code> the
466 * container notifies them accordingly.
467 * <p>
468 * If a null value is passed, the effect is the same as calling
469 * <code>removeAttribute()</code>.
470 *
471 * <p>Attribute names should follow the same convention as package
472 * names. The Java Servlet API specification reserves names
473 * matching <code>java.*</code>, <code>javax.*</code>, and
474 * <code>sun.*</code>.
475 *
476 * @param name a <code>String</code> specifying the name
477 * of the attribute
478 *
479 * @param object an <code>Object</code> representing the
480 * attribute to be bound
481 */
482 public void setAttribute(String name, Object object);
483
484 /**
485 * Removes the attribute with the given name from
486 * the servlet context. After removal, subsequent calls to
487 * {@link #getAttribute} to retrieve the attribute's value
488 * will return <code>null</code>.
489 *
490 * <p>If listeners are configured on the <code>ServletContext</code> the
491 * container notifies them accordingly.
492 *
493 * @param name a <code>String</code> specifying the name
494 * of the attribute to be removed
495 */
496 public void removeAttribute(String name);
497
498 /**
499 * Returns the name of this web application correponding to this ServletContext as specified in the deployment
500 * descriptor for this web application by the display-name element.
501 *
502 * @return The name of the web application or null if no name has been declared in the deployment descriptor.
503 * @since Servlet 2.3
504 */
505 public String getServletContextName();
506 }