1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. * ====================================================================
  55. *
  56. * This source code implements specifications defined by the Java
  57. * Community Process. In order to remain compliant with the specification
  58. * DO NOT add / change / or delete method signatures!
  59. */
  60. package javax.servlet;
  61. import java.io.InputStream;
  62. import java.io.IOException;
  63. import java.net.URL;
  64. import java.net.MalformedURLException;
  65. import java.util.Enumeration;
  66. import java.util.Set;
  67. /**
  68. *
  69. * Defines a set of methods that a servlet uses to communicate with its
  70. * servlet container, for example, to get the MIME type of a file, dispatch
  71. * requests, or write to a log file.
  72. *
  73. * <p>There is one context per "web application" per Java Virtual Machine. (A
  74. * "web application" is a collection of servlets and content installed under a
  75. * specific subset of the server's URL namespace such as <code>/catalog</code>
  76. * and possibly installed via a <code>.war</code> file.)
  77. *
  78. * <p>In the case of a web
  79. * application marked "distributed" in its deployment descriptor, there will
  80. * be one context instance for each virtual machine. In this situation, the
  81. * context cannot be used as a location to share global information (because
  82. * the information won't be truly global). Use an external resource like
  83. * a database instead.
  84. *
  85. * <p>The <code>ServletContext</code> object is contained within
  86. * the {@link ServletConfig} object, which the Web server provides the
  87. * servlet when the servlet is initialized.
  88. *
  89. * @author Various
  90. * @version $Version$
  91. *
  92. * @see Servlet#getServletConfig
  93. * @see ServletConfig#getServletContext
  94. *
  95. */
  96. public interface ServletContext {
  97. /**
  98. * Returns a <code>ServletContext</code> object that
  99. * corresponds to a specified URL on the server.
  100. *
  101. * <p>This method allows servlets to gain
  102. * access to the context for various parts of the server, and as
  103. * needed obtain {@link RequestDispatcher} objects from the context.
  104. * The given path must be begin with "/", is interpreted relative
  105. * to the server's document root and is matched against the context roots of
  106. * other web applications hosted on this container.
  107. *
  108. * <p>In a security conscious environment, the servlet container may
  109. * return <code>null</code> for a given URL.
  110. *
  111. * @param uripath a <code>String</code> specifying the context path of
  112. * another web application in the container.
  113. * @return the <code>ServletContext</code> object that
  114. * corresponds to the named URL, or null if either
  115. none exists or the container wishes to restrict
  116. * this access.
  117. *
  118. * @see RequestDispatcher
  119. *
  120. */
  121. public ServletContext getContext(String uripath);
  122. /**
  123. * Returns the major version of the Java Servlet API that this
  124. * servlet container supports. All implementations that comply
  125. * with Version 2.3 must have this method
  126. * return the integer 2.
  127. *
  128. * @return 2
  129. *
  130. */
  131. public int getMajorVersion();
  132. /**
  133. * Returns the minor version of the Servlet API that this
  134. * servlet container supports. All implementations that comply
  135. * with Version 2.3 must have this method
  136. * return the integer 3.
  137. *
  138. * @return 3
  139. *
  140. */
  141. public int getMinorVersion();
  142. /**
  143. * Returns the MIME type of the specified file, or <code>null</code> if
  144. * the MIME type is not known. The MIME type is determined
  145. * by the configuration of the servlet container, and may be specified
  146. * in a web application deployment descriptor. Common MIME
  147. * types are <code>"text/html"</code> and <code>"image/gif"</code>.
  148. *
  149. *
  150. * @param file a <code>String</code> specifying the name
  151. * of a file
  152. *
  153. * @return a <code>String</code> specifying the file's MIME type
  154. *
  155. */
  156. public String getMimeType(String file);
  157. /**
  158. * Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path
  159. * matches the supplied path argument. Paths indicating subdirectory paths end with a '/'. The returned paths are all
  160. * relative to the root of the web application and have a leading '/'. For example, for a web application
  161. * containing<br><br>
  162. * /welcome.html<br>
  163. * /catalog/index.html<br>
  164. * /catalog/products.html<br>
  165. * /catalog/offers/books.html<br>
  166. * /catalog/offers/music.html<br>
  167. * /customer/login.jsp<br>
  168. * /WEB-INF/web.xml<br>
  169. * /WEB-INF/classes/com.acme.OrderServlet.class,<br><br>
  170. *
  171. * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}<br>
  172. * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.<br>
  173. *@param the partial path used to match the resources, which must start with a /
  174. *@return a Set containing the directory listing, or null if there are no resources in the web application whose path
  175. * begins with the supplied path.
  176. * @since Servlet 2.3
  177. */
  178. public Set getResourcePaths(String path);
  179. /**
  180. * Returns a URL to the resource that is mapped to a specified
  181. * path. The path must begin with a "/" and is interpreted
  182. * as relative to the current context root.
  183. *
  184. * <p>This method allows the servlet container to make a resource
  185. * available to servlets from any source. Resources
  186. * can be located on a local or remote
  187. * file system, in a database, or in a <code>.war</code> file.
  188. *
  189. * <p>The servlet container must implement the URL handlers
  190. * and <code>URLConnection</code> objects that are necessary
  191. * to access the resource.
  192. *
  193. * <p>This method returns <code>null</code>
  194. * if no resource is mapped to the pathname.
  195. *
  196. * <p>Some containers may allow writing to the URL returned by
  197. * this method using the methods of the URL class.
  198. *
  199. * <p>The resource content is returned directly, so be aware that
  200. * requesting a <code>.jsp</code> page returns the JSP source code.
  201. * Use a <code>RequestDispatcher</code> instead to include results of
  202. * an execution.
  203. *
  204. * <p>This method has a different purpose than
  205. * <code>java.lang.Class.getResource</code>,
  206. * which looks up resources based on a class loader. This
  207. * method does not use class loaders.
  208. *
  209. * @param path a <code>String</code> specifying
  210. * the path to the resource
  211. *
  212. * @return the resource located at the named path,
  213. * or <code>null</code> if there is no resource
  214. * at that path
  215. *
  216. * @exception MalformedURLException if the pathname is not given in
  217. * the correct form
  218. *
  219. */
  220. public URL getResource(String path) throws MalformedURLException;
  221. /**
  222. * Returns the resource located at the named path as
  223. * an <code>InputStream</code> object.
  224. *
  225. * <p>The data in the <code>InputStream</code> can be
  226. * of any type or length. The path must be specified according
  227. * to the rules given in <code>getResource</code>.
  228. * This method returns <code>null</code> if no resource exists at
  229. * the specified path.
  230. *
  231. * <p>Meta-information such as content length and content type
  232. * that is available via <code>getResource</code>
  233. * method is lost when using this method.
  234. *
  235. * <p>The servlet container must implement the URL handlers
  236. * and <code>URLConnection</code> objects necessary to access
  237. * the resource.
  238. *
  239. * <p>This method is different from
  240. * <code>java.lang.Class.getResourceAsStream</code>,
  241. * which uses a class loader. This method allows servlet containers
  242. * to make a resource available
  243. * to a servlet from any location, without using a class loader.
  244. *
  245. *
  246. * @param name a <code>String</code> specifying the path
  247. * to the resource
  248. *
  249. * @return the <code>InputStream</code> returned to the
  250. * servlet, or <code>null</code> if no resource
  251. * exists at the specified path
  252. *
  253. *
  254. */
  255. public InputStream getResourceAsStream(String path);
  256. /**
  257. *
  258. * Returns a {@link RequestDispatcher} object that acts
  259. * as a wrapper for the resource located at the given path.
  260. * A <code>RequestDispatcher</code> object can be used to forward
  261. * a request to the resource or to include the resource in a response.
  262. * The resource can be dynamic or static.
  263. *
  264. * <p>The pathname must begin with a "/" and is interpreted as relative
  265. * to the current context root. Use <code>getContext</code> to obtain
  266. * a <code>RequestDispatcher</code> for resources in foreign contexts.
  267. * This method returns <code>null</code> if the <code>ServletContext</code>
  268. * cannot return a <code>RequestDispatcher</code>.
  269. *
  270. * @param path a <code>String</code> specifying the pathname
  271. * to the resource
  272. *
  273. * @return a <code>RequestDispatcher</code> object
  274. * that acts as a wrapper for the resource
  275. * at the specified path
  276. *
  277. * @see RequestDispatcher
  278. * @see ServletContext#getContext
  279. *
  280. */
  281. public RequestDispatcher getRequestDispatcher(String path);
  282. /**
  283. * Returns a {@link RequestDispatcher} object that acts
  284. * as a wrapper for the named servlet.
  285. *
  286. * <p>Servlets (and JSP pages also) may be given names via server
  287. * administration or via a web application deployment descriptor.
  288. * A servlet instance can determine its name using
  289. * {@link ServletConfig#getServletName}.
  290. *
  291. * <p>This method returns <code>null</code> if the
  292. * <code>ServletContext</code>
  293. * cannot return a <code>RequestDispatcher</code> for any reason.
  294. *
  295. * @param name a <code>String</code> specifying the name
  296. * of a servlet to wrap
  297. *
  298. * @return a <code>RequestDispatcher</code> object
  299. * that acts as a wrapper for the named servlet
  300. *
  301. * @see RequestDispatcher
  302. * @see ServletContext#getContext
  303. * @see ServletConfig#getServletName
  304. *
  305. */
  306. public RequestDispatcher getNamedDispatcher(String name);
  307. /**
  308. *
  309. * @deprecated As of Java Servlet API 2.1, with no direct replacement.
  310. *
  311. * <p>This method was originally defined to retrieve a servlet
  312. * from a <code>ServletContext</code>. In this version, this method
  313. * always returns <code>null</code> and remains only to preserve
  314. * binary compatibility. This method will be permanently removed
  315. * in a future version of the Java Servlet API.
  316. *
  317. * <p>In lieu of this method, servlets can share information using the
  318. * <code>ServletContext</code> class and can perform shared business logic
  319. * by invoking methods on common non-servlet classes.
  320. *
  321. */
  322. public Servlet getServlet(String name) throws ServletException;
  323. /**
  324. *
  325. * @deprecated As of Java Servlet API 2.0, with no replacement.
  326. *
  327. * <p>This method was originally defined to return an <code>Enumeration</code>
  328. * of all the servlets known to this servlet context. In this
  329. * version, this method always returns an empty enumeration and
  330. * remains only to preserve binary compatibility. This method
  331. * will be permanently removed in a future version of the Java
  332. * Servlet API.
  333. *
  334. */
  335. public Enumeration getServlets();
  336. /**
  337. * @deprecated As of Java Servlet API 2.1, with no replacement.
  338. *
  339. * <p>This method was originally defined to return an
  340. * <code>Enumeration</code>
  341. * of all the servlet names known to this context. In this version,
  342. * this method always returns an empty <code>Enumeration</code> and
  343. * remains only to preserve binary compatibility. This method will
  344. * be permanently removed in a future version of the Java Servlet API.
  345. *
  346. */
  347. public Enumeration getServletNames();
  348. /**
  349. *
  350. * Writes the specified message to a servlet log file, usually
  351. * an event log. The name and type of the servlet log file is
  352. * specific to the servlet container.
  353. *
  354. *
  355. * @param msg a <code>String</code> specifying the
  356. * message to be written to the log file
  357. *
  358. */
  359. public void log(String msg);
  360. /**
  361. * @deprecated As of Java Servlet API 2.1, use
  362. * {@link #log(String message, Throwable throwable)}
  363. * instead.
  364. *
  365. * <p>This method was originally defined to write an
  366. * exception's stack trace and an explanatory error message
  367. * to the servlet log file.
  368. *
  369. */
  370. public void log(Exception exception, String msg);
  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. public void log(String message, Throwable throwable);
  386. /**
  387. * Returns a <code>String</code> containing the real path
  388. * for a given virtual path. For example, the path "/index.html"
  389. * returns the absolute file path on the server's filesystem would be
  390. * served by a request for "http://host/contextPath/index.html",
  391. * where contextPath is the context path of this ServletContext..
  392. *
  393. * <p>The real path returned will be in a form
  394. * appropriate to the computer and operating system on
  395. * which the servlet container is running, including the
  396. * proper path separators. This method returns <code>null</code>
  397. * if the servlet container cannot translate the virtual path
  398. * to a real path for any reason (such as when the content is
  399. * being made available from a <code>.war</code> archive).
  400. *
  401. *
  402. * @param path a <code>String</code> specifying a virtual path
  403. *
  404. *
  405. * @return a <code>String</code> specifying the real path,
  406. * or null if the translation cannot be performed
  407. *
  408. *
  409. */
  410. public String getRealPath(String path);
  411. /**
  412. * Returns the name and version of the servlet container on which
  413. * the servlet is running.
  414. *
  415. * <p>The form of the returned string is
  416. * <i>servername</i>/<i>versionnumber</i>.
  417. * For example, the JavaServer Web Development Kit may return the string
  418. * <code>JavaServer Web Dev Kit/1.0</code>.
  419. *
  420. * <p>The servlet container may return other optional information
  421. * after the primary string in parentheses, for example,
  422. * <code>JavaServer Web Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86)</code>.
  423. *
  424. *
  425. * @return a <code>String</code> containing at least the
  426. * servlet container name and version number
  427. *
  428. */
  429. public String getServerInfo();
  430. /**
  431. * Returns a <code>String</code> containing the value of the named
  432. * context-wide initialization parameter, or <code>null</code> if the
  433. * parameter does not exist.
  434. *
  435. * <p>This method can make available configuration information useful
  436. * to an entire "web application". For example, it can provide a
  437. * webmaster's email address or the name of a system that holds
  438. * critical data.
  439. *
  440. * @param name a <code>String</code> containing the name of the
  441. * parameter whose value is requested
  442. *
  443. * @return a <code>String</code> containing at least the
  444. * servlet container name and version number
  445. *
  446. * @see ServletConfig#getInitParameter
  447. */
  448. public String getInitParameter(String name);
  449. /**
  450. * Returns the names of the context's initialization parameters as an
  451. * <code>Enumeration</code> of <code>String</code> objects, or an
  452. * empty <code>Enumeration</code> if the context has no initialization
  453. * parameters.
  454. *
  455. * @return an <code>Enumeration</code> of <code>String</code>
  456. * objects containing the names of the context's
  457. * initialization parameters
  458. *
  459. * @see ServletConfig#getInitParameter
  460. */
  461. public Enumeration getInitParameterNames();
  462. /**
  463. * Returns the servlet container attribute with the given name,
  464. * or <code>null</code> if there is no attribute by that name.
  465. * An attribute allows a servlet container to give the
  466. * servlet additional information not
  467. * already provided by this interface. See your
  468. * server documentation for information about its attributes.
  469. * A list of supported attributes can be retrieved using
  470. * <code>getAttributeNames</code>.
  471. *
  472. * <p>The attribute is returned as a <code>java.lang.Object</code>
  473. * or some subclass.
  474. * Attribute names should follow the same convention as package
  475. * names. The Java Servlet API specification reserves names
  476. * matching <code>java.*</code>, <code>javax.*</code>,
  477. * and <code>sun.*</code>.
  478. *
  479. *
  480. * @param name a <code>String</code> specifying the name
  481. * of the attribute
  482. *
  483. * @return an <code>Object</code> containing the value
  484. * of the attribute, or <code>null</code>
  485. * if no attribute exists matching the given
  486. * name
  487. *
  488. * @see ServletContext#getAttributeNames
  489. *
  490. */
  491. public Object getAttribute(String name);
  492. /**
  493. * Returns an <code>Enumeration</code> containing the
  494. * attribute names available
  495. * within this servlet context. Use the
  496. * {@link #getAttribute} method with an attribute name
  497. * to get the value of an attribute.
  498. *
  499. * @return an <code>Enumeration</code> of attribute
  500. * names
  501. *
  502. * @see #getAttribute
  503. *
  504. */
  505. public Enumeration getAttributeNames();
  506. /**
  507. *
  508. * Binds an object to a given attribute name in this servlet context. If
  509. * the name specified is already used for an attribute, this
  510. * method will replace the attribute with the new to the new attribute.
  511. * <p>If listeners are configured on the <code>ServletContext</code> the
  512. * container notifies them accordingly.
  513. * <p>
  514. * If a null value is passed, the effect is the same as calling
  515. * <code>removeAttribute()</code>.
  516. *
  517. * <p>Attribute names should follow the same convention as package
  518. * names. The Java Servlet API specification reserves names
  519. * matching <code>java.*</code>, <code>javax.*</code>, and
  520. * <code>sun.*</code>.
  521. *
  522. *
  523. * @param name a <code>String</code> specifying the name
  524. * of the attribute
  525. *
  526. * @param object an <code>Object</code> representing the
  527. * attribute to be bound
  528. *
  529. *
  530. *
  531. */
  532. public void setAttribute(String name, Object object);
  533. /**
  534. * Removes the attribute with the given name from
  535. * the servlet context. After removal, subsequent calls to
  536. * {@link #getAttribute} to retrieve the attribute's value
  537. * will return <code>null</code>.
  538. * <p>If listeners are configured on the <code>ServletContext</code> the
  539. * container notifies them accordingly.
  540. *
  541. *
  542. * @param name a <code>String</code> specifying the name
  543. * of the attribute to be removed
  544. *
  545. */
  546. public void removeAttribute(String name);
  547. /**
  548. * Returns the name of this web application correponding to this ServletContext as specified in the deployment
  549. * descriptor for this web application by the display-name element.
  550. *
  551. *
  552. * @return The name of the web application or null if no name has been declared in the deployment descriptor.
  553. * @since Servlet 2.3
  554. */
  555. public String getServletContextName();
  556. }