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