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.http;
  61. import javax.servlet.ServletResponse;
  62. import javax.servlet.ServletException;
  63. import java.io.IOException;
  64. /**
  65. *
  66. * Extends the {@link ServletResponse} interface to provide HTTP-specific
  67. * functionality in sending a response. For example, it has methods
  68. * to access HTTP headers and cookies.
  69. *
  70. * <p>The servlet container creates an <code>HttpServletRequest</code> object
  71. * and passes it as an argument to the servlet's service methods
  72. * (<code>doGet</code>, <code>doPost</code>, etc).
  73. *
  74. *
  75. * @author Various
  76. * @version $Version$
  77. *
  78. * @see javax.servlet.ServletResponse
  79. *
  80. */
  81. public interface HttpServletResponse extends ServletResponse {
  82. /**
  83. * Adds the specified cookie to the response. This method can be called
  84. * multiple times to set more than one cookie.
  85. *
  86. * @param cookie the Cookie to return to the client
  87. *
  88. */
  89. public void addCookie(Cookie cookie);
  90. /**
  91. * Returns a boolean indicating whether the named response header
  92. * has already been set.
  93. *
  94. * @param name the header name
  95. * @return <code>true</code> if the named response header
  96. * has already been set;
  97. * <code>false</code> otherwise
  98. */
  99. public boolean containsHeader(String name);
  100. /**
  101. * Encodes the specified URL by including the session ID in it,
  102. * or, if encoding is not needed, returns the URL unchanged.
  103. * The implementation of this method includes the logic to
  104. * determine whether the session ID needs to be encoded in the URL.
  105. * For example, if the browser supports cookies, or session
  106. * tracking is turned off, URL encoding is unnecessary.
  107. *
  108. * <p>For robust session tracking, all URLs emitted by a servlet
  109. * should be run through this
  110. * method. Otherwise, URL rewriting cannot be used with browsers
  111. * which do not support cookies.
  112. *
  113. * @param url the url to be encoded.
  114. * @return the encoded URL if encoding is needed;
  115. * the unchanged URL otherwise.
  116. */
  117. public String encodeURL(String url);
  118. /**
  119. * Encodes the specified URL for use in the
  120. * <code>sendRedirect</code> method or, if encoding is not needed,
  121. * returns the URL unchanged. The implementation of this method
  122. * includes the logic to determine whether the session ID
  123. * needs to be encoded in the URL. Because the rules for making
  124. * this determination can differ from those used to decide whether to
  125. * encode a normal link, this method is seperate from the
  126. * <code>encodeURL</code> method.
  127. *
  128. * <p>All URLs sent to the <code>HttpServletResponse.sendRedirect</code>
  129. * method should be run through this method. Otherwise, URL
  130. * rewriting cannot be used with browsers which do not support
  131. * cookies.
  132. *
  133. * @param url the url to be encoded.
  134. * @return the encoded URL if encoding is needed;
  135. * the unchanged URL otherwise.
  136. *
  137. * @see #sendRedirect
  138. * @see #encodeUrl
  139. */
  140. public String encodeRedirectURL(String url);
  141. /**
  142. * @deprecated As of version 2.1, use encodeURL(String url) instead
  143. *
  144. * @param url the url to be encoded.
  145. * @return the encoded URL if encoding is needed;
  146. * the unchanged URL otherwise.
  147. */
  148. public String encodeUrl(String url);
  149. /**
  150. * @deprecated As of version 2.1, use
  151. * encodeRedirectURL(String url) instead
  152. *
  153. * @param url the url to be encoded.
  154. * @return the encoded URL if encoding is needed;
  155. * the unchanged URL otherwise.
  156. */
  157. public String encodeRedirectUrl(String url);
  158. /**
  159. * Sends an error response to the client using the specified
  160. * status clearing the buffer. The server defaults to creating the
  161. * response to look like an HTML-formatted server error page containing the specified message, setting the content type
  162. * to "text/html", leaving cookies and other headers unmodified.
  163. *
  164. * If an error-page declaration has been made for the web application
  165. * corresponding to the status code passed in, it will be served back in
  166. * preference to the suggested msg parameter.
  167. *
  168. * <p>If the response has already been committed, this method throws
  169. * an IllegalStateException.
  170. * After using this method, the response should be considered
  171. * to be committed and should not be written to.
  172. *
  173. * @param sc the error status code
  174. * @param msg the descriptive message
  175. * @exception IOException If an input or output exception occurs
  176. * @exception IllegalStateException If the response was committed
  177. */
  178. public void sendError(int sc, String msg) throws IOException;
  179. /**
  180. * Sends an error response to the client using the specified status
  181. * code and clearing the buffer.
  182. * <p>If the response has already been committed, this method throws
  183. * an IllegalStateException.
  184. * After using this method, the response should be considered
  185. * to be committed and should not be written to.
  186. *
  187. * @param sc the error status code
  188. * @exception IOException If an input or output exception occurs
  189. * @exception IllegalStateException If the response was committed
  190. * before this method call
  191. */
  192. public void sendError(int sc) throws IOException;
  193. /**
  194. * Sends a temporary redirect response to the client using the
  195. * specified redirect location URL. This method can accept relative URLs;
  196. * the servlet container must convert the relative URL to an absolute URL
  197. * before sending the response to the client. If the location is relative
  198. * without a leading '/' the container interprets it as relative to
  199. * the current request URI. If the location is relative with a leading
  200. * '/' the container interprets it as relative to the servlet container root.
  201. *
  202. * <p>If the response has already been committed, this method throws
  203. * an IllegalStateException.
  204. * After using this method, the response should be considered
  205. * to be committed and should not be written to.
  206. *
  207. * @param location the redirect location URL
  208. * @exception IOException If an input or output exception occurs
  209. * @exception IllegalStateException If the response was committed
  210. */
  211. public void sendRedirect(String location) throws IOException;
  212. /**
  213. *
  214. * Sets a response header with the given name and
  215. * date-value. The date is specified in terms of
  216. * milliseconds since the epoch. If the header had already
  217. * been set, the new value overwrites the previous one. The
  218. * <code>containsHeader</code> method can be used to test for the
  219. * presence of a header before setting its value.
  220. *
  221. * @param name the name of the header to set
  222. * @param value the assigned date value
  223. *
  224. * @see #containsHeader
  225. * @see #addDateHeader
  226. */
  227. public void setDateHeader(String name, long date);
  228. /**
  229. *
  230. * Adds a response header with the given name and
  231. * date-value. The date is specified in terms of
  232. * milliseconds since the epoch. This method allows response headers
  233. * to have multiple values.
  234. *
  235. * @param name the name of the header to set
  236. * @param value the additional date value
  237. *
  238. * @see #setDateHeader
  239. */
  240. public void addDateHeader(String name, long date);
  241. /**
  242. *
  243. * Sets a response header with the given name and value.
  244. * If the header had already been set, the new value overwrites the
  245. * previous one. The <code>containsHeader</code> method can be
  246. * used to test for the presence of a header before setting its
  247. * value.
  248. *
  249. * @param name the name of the header
  250. * @param value the header value
  251. *
  252. * @see #containsHeader
  253. * @see #addHeader
  254. */
  255. public void setHeader(String name, String value);
  256. /**
  257. * Adds a response header with the given name and value.
  258. * This method allows response headers to have multiple values.
  259. *
  260. * @param name the name of the header
  261. * @param value the additional header value
  262. *
  263. * @see #setHeader
  264. */
  265. public void addHeader(String name, String value);
  266. /**
  267. * Sets a response header with the given name and
  268. * integer value. If the header had already been set, the new value
  269. * overwrites the previous one. The <code>containsHeader</code>
  270. * method can be used to test for the presence of a header before
  271. * setting its value.
  272. *
  273. * @param name the name of the header
  274. * @param value the assigned integer value
  275. *
  276. * @see #containsHeader
  277. * @see #addIntHeader
  278. */
  279. public void setIntHeader(String name, int value);
  280. /**
  281. * Adds a response header with the given name and
  282. * integer value. This method allows response headers to have multiple
  283. * values.
  284. *
  285. * @param name the name of the header
  286. * @param value the assigned integer value
  287. *
  288. * @see #setIntHeader
  289. */
  290. public void addIntHeader(String name, int value);
  291. /**
  292. * Sets the status code for this response. This method is used to
  293. * set the return status code when there is no error (for example,
  294. * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there
  295. * is an error, and the caller wishes to invoke an <error-page> defined
  296. * in the web applicaion, the <code>sendError</code> method should be used
  297. * instead.
  298. * <p> The container clears the buffer and sets the Location header, preserving
  299. * cookies and other headers.
  300. *
  301. * @param sc the status code
  302. *
  303. * @see #sendError
  304. */
  305. public void setStatus(int sc);
  306. /**
  307. * @deprecated As of version 2.1, due to ambiguous meaning of the
  308. * message parameter. To set a status code
  309. * use <code>setStatus(int)</code>, to send an error with a description
  310. * use <code>sendError(int, String)</code>.
  311. *
  312. * Sets the status code and message for this response.
  313. *
  314. * @param sc the status code
  315. * @param sm the status message
  316. */
  317. public void setStatus(int sc, String sm);
  318. /*
  319. * Server status codes; see RFC 2068.
  320. */
  321. /**
  322. * Status code (100) indicating the client can continue.
  323. */
  324. public static final int SC_CONTINUE = 100;
  325. /**
  326. * Status code (101) indicating the server is switching protocols
  327. * according to Upgrade header.
  328. */
  329. public static final int SC_SWITCHING_PROTOCOLS = 101;
  330. /**
  331. * Status code (200) indicating the request succeeded normally.
  332. */
  333. public static final int SC_OK = 200;
  334. /**
  335. * Status code (201) indicating the request succeeded and created
  336. * a new resource on the server.
  337. */
  338. public static final int SC_CREATED = 201;
  339. /**
  340. * Status code (202) indicating that a request was accepted for
  341. * processing, but was not completed.
  342. */
  343. public static final int SC_ACCEPTED = 202;
  344. /**
  345. * Status code (203) indicating that the meta information presented
  346. * by the client did not originate from the server.
  347. */
  348. public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
  349. /**
  350. * Status code (204) indicating that the request succeeded but that
  351. * there was no new information to return.
  352. */
  353. public static final int SC_NO_CONTENT = 204;
  354. /**
  355. * Status code (205) indicating that the agent <em>SHOULD</em> reset
  356. * the document view which caused the request to be sent.
  357. */
  358. public static final int SC_RESET_CONTENT = 205;
  359. /**
  360. * Status code (206) indicating that the server has fulfilled
  361. * the partial GET request for the resource.
  362. */
  363. public static final int SC_PARTIAL_CONTENT = 206;
  364. /**
  365. * Status code (300) indicating that the requested resource
  366. * corresponds to any one of a set of representations, each with
  367. * its own specific location.
  368. */
  369. public static final int SC_MULTIPLE_CHOICES = 300;
  370. /**
  371. * Status code (301) indicating that the resource has permanently
  372. * moved to a new location, and that future references should use a
  373. * new URI with their requests.
  374. */
  375. public static final int SC_MOVED_PERMANENTLY = 301;
  376. /**
  377. * Status code (302) indicating that the resource has temporarily
  378. * moved to another location, but that future references should
  379. * still use the original URI to access the resource.
  380. */
  381. public static final int SC_MOVED_TEMPORARILY = 302;
  382. /**
  383. * Status code (303) indicating that the response to the request
  384. * can be found under a different URI.
  385. */
  386. public static final int SC_SEE_OTHER = 303;
  387. /**
  388. * Status code (304) indicating that a conditional GET operation
  389. * found that the resource was available and not modified.
  390. */
  391. public static final int SC_NOT_MODIFIED = 304;
  392. /**
  393. * Status code (305) indicating that the requested resource
  394. * <em>MUST</em> be accessed through the proxy given by the
  395. * <code><em>Location</em></code> field.
  396. */
  397. public static final int SC_USE_PROXY = 305;
  398. /**
  399. * Status code (307) indicating that the requested resource
  400. * resides temporarily under a different URI. The temporary URI
  401. * <em>SHOULD</em> be given by the <code><em>Location</em></code>
  402. * field in the response.
  403. */
  404. public static final int SC_TEMPORARY_REDIRECT = 307;
  405. /**
  406. * Status code (400) indicating the request sent by the client was
  407. * syntactically incorrect.
  408. */
  409. public static final int SC_BAD_REQUEST = 400;
  410. /**
  411. * Status code (401) indicating that the request requires HTTP
  412. * authentication.
  413. */
  414. public static final int SC_UNAUTHORIZED = 401;
  415. /**
  416. * Status code (402) reserved for future use.
  417. */
  418. public static final int SC_PAYMENT_REQUIRED = 402;
  419. /**
  420. * Status code (403) indicating the server understood the request
  421. * but refused to fulfill it.
  422. */
  423. public static final int SC_FORBIDDEN = 403;
  424. /**
  425. * Status code (404) indicating that the requested resource is not
  426. * available.
  427. */
  428. public static final int SC_NOT_FOUND = 404;
  429. /**
  430. * Status code (405) indicating that the method specified in the
  431. * <code><em>Request-Line</em></code> is not allowed for the resource
  432. * identified by the <code><em>Request-URI</em></code>.
  433. */
  434. public static final int SC_METHOD_NOT_ALLOWED = 405;
  435. /**
  436. * Status code (406) indicating that the resource identified by the
  437. * request is only capable of generating response entities which have
  438. * content characteristics not acceptable according to the accept
  439. * headerssent in the request.
  440. */
  441. public static final int SC_NOT_ACCEPTABLE = 406;
  442. /**
  443. * Status code (407) indicating that the client <em>MUST</em> first
  444. * authenticate itself with the proxy.
  445. */
  446. public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
  447. /**
  448. * Status code (408) indicating that the client did not produce a
  449. * requestwithin the time that the server was prepared to wait.
  450. */
  451. public static final int SC_REQUEST_TIMEOUT = 408;
  452. /**
  453. * Status code (409) indicating that the request could not be
  454. * completed due to a conflict with the current state of the
  455. * resource.
  456. */
  457. public static final int SC_CONFLICT = 409;
  458. /**
  459. * Status code (410) indicating that the resource is no longer
  460. * available at the server and no forwarding address is known.
  461. * This condition <em>SHOULD</em> be considered permanent.
  462. */
  463. public static final int SC_GONE = 410;
  464. /**
  465. * Status code (411) indicating that the request cannot be handled
  466. * without a defined <code><em>Content-Length</em></code>.
  467. */
  468. public static final int SC_LENGTH_REQUIRED = 411;
  469. /**
  470. * Status code (412) indicating that the precondition given in one
  471. * or more of the request-header fields evaluated to false when it
  472. * was tested on the server.
  473. */
  474. public static final int SC_PRECONDITION_FAILED = 412;
  475. /**
  476. * Status code (413) indicating that the server is refusing to process
  477. * the request because the request entity is larger than the server is
  478. * willing or able to process.
  479. */
  480. public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
  481. /**
  482. * Status code (414) indicating that the server is refusing to service
  483. * the request because the <code><em>Request-URI</em></code> is longer
  484. * than the server is willing to interpret.
  485. */
  486. public static final int SC_REQUEST_URI_TOO_LONG = 414;
  487. /**
  488. * Status code (415) indicating that the server is refusing to service
  489. * the request because the entity of the request is in a format not
  490. * supported by the requested resource for the requested method.
  491. */
  492. public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
  493. /**
  494. * Status code (416) indicating that the server cannot serve the
  495. * requested byte range.
  496. */
  497. public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
  498. /**
  499. * Status code (417) indicating that the server could not meet the
  500. * expectation given in the Expect request header.
  501. */
  502. public static final int SC_EXPECTATION_FAILED = 417;
  503. /**
  504. * Status code (500) indicating an error inside the HTTP server
  505. * which prevented it from fulfilling the request.
  506. */
  507. public static final int SC_INTERNAL_SERVER_ERROR = 500;
  508. /**
  509. * Status code (501) indicating the HTTP server does not support
  510. * the functionality needed to fulfill the request.
  511. */
  512. public static final int SC_NOT_IMPLEMENTED = 501;
  513. /**
  514. * Status code (502) indicating that the HTTP server received an
  515. * invalid response from a server it consulted when acting as a
  516. * proxy or gateway.
  517. */
  518. public static final int SC_BAD_GATEWAY = 502;
  519. /**
  520. * Status code (503) indicating that the HTTP server is
  521. * temporarily overloaded, and unable to handle the request.
  522. */
  523. public static final int SC_SERVICE_UNAVAILABLE = 503;
  524. /**
  525. * Status code (504) indicating that the server did not receive
  526. * a timely response from the upstream server while acting as
  527. * a gateway or proxy.
  528. */
  529. public static final int SC_GATEWAY_TIMEOUT = 504;
  530. /**
  531. * Status code (505) indicating that the server does not support
  532. * or refuses to support the HTTP protocol version that was used
  533. * in the request message.
  534. */
  535. public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
  536. }