001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 018 // 019 // This source code implements specifications defined by the Java 020 // Community Process. In order to remain compliant with the specification 021 // DO NOT add / change / or delete method signatures! 022 // 023 024 package javax.servlet.http; 025 026 import java.io.IOException; 027 import javax.servlet.ServletResponse; 028 029 /** 030 * Extends the {@link ServletResponse} interface to provide HTTP-specific 031 * functionality in sending a response. For example, it has methods 032 * to access HTTP headers and cookies. 033 * 034 * <p>The servlet container creates an <code>HttpServletResponse</code> object 035 * and passes it as an argument to the servlet's service methods 036 * (<code>doGet</code>, <code>doPost</code>, etc). 037 * 038 * @see javax.servlet.ServletResponse 039 * 040 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 041 */ 042 public interface HttpServletResponse extends ServletResponse { 043 /** 044 * Adds the specified cookie to the response. This method can be called 045 * multiple times to set more than one cookie. 046 * 047 * @param cookie the Cookie to return to the client 048 */ 049 public void addCookie(Cookie cookie); 050 051 /** 052 * Returns a boolean indicating whether the named response header has 053 * already been set. 054 * 055 * @param name the header name 056 * @return <code>true</code> if the named response header has already been 057 * set; <code>false</code> otherwise 058 */ 059 public boolean containsHeader(String name); 060 061 /** 062 * Encodes the specified URL by including the session ID in it, 063 * or, if encoding is not needed, returns the URL unchanged. 064 * The implementation of this method includes the logic to 065 * determine whether the session ID needs to be encoded in the URL. 066 * For example, if the browser supports cookies, or session 067 * tracking is turned off, URL encoding is unnecessary. 068 * 069 * <p>For robust session tracking, all URLs emitted by a servlet 070 * should be run through this 071 * method. Otherwise, URL rewriting cannot be used with browsers 072 * which do not support cookies. 073 * 074 * @param url the url to be encoded. 075 * @return the encoded URL if encoding is needed; the unchanged URL 076 * otherwise. 077 */ 078 public String encodeURL(String url); 079 080 /** 081 * Encodes the specified URL for use in the 082 * <code>sendRedirect</code> method or, if encoding is not needed, 083 * returns the URL unchanged. The implementation of this method 084 * includes the logic to determine whether the session ID 085 * needs to be encoded in the URL. Because the rules for making 086 * this determination can differ from those used to decide whether to 087 * encode a normal link, this method is separete from the 088 * <code>encodeURL</code> method. 089 * 090 * <p>All URLs sent to the <code>HttpServletResponse.sendRedirect</code> 091 * method should be run through this method. Otherwise, URL 092 * rewriting cannot be used with browsers which do not support 093 * cookies. 094 * 095 * @param url the url to be encoded. 096 * @return the encoded URL if encoding is needed; the unchanged URL 097 * otherwise. 098 * 099 * @see #sendRedirect 100 * @see #encodeUrl 101 */ 102 public String encodeRedirectURL(String url); 103 104 /** 105 * @deprecated As of version 2.1, use encodeURL(String url) instead 106 * 107 * @param url the url to be encoded. 108 * @return the encoded URL if encoding is needed; the unchanged URL 109 * otherwise. 110 */ 111 public String encodeUrl(String url); 112 113 /** 114 * @deprecated As of version 2.1, use encodeRedirectURL(String url) instead 115 * 116 * @param url the url to be encoded. 117 * @return the encoded URL if encoding is needed; the unchanged URL 118 * otherwise. 119 */ 120 public String encodeRedirectUrl(String url); 121 122 /** 123 * Sends an error response to the client using the specified 124 * status. The server defaults to creating the 125 * response to look like an HTML-formatted server error page 126 * containing the specified message, setting the content type 127 * to "text/html", leaving cookies and other headers unmodified. 128 * 129 * If an error-page declaration has been made for the web application 130 * corresponding to the status code passed in, it will be served back in 131 * preference to the suggested msg parameter. 132 * 133 * <p>If the response has already been committed, this method throws 134 * an IllegalStateException. 135 * After using this method, the response should be considered 136 * to be committed and should not be written to. 137 * 138 * @param sc the error status code 139 * @param msg the descriptive message 140 * @exception IOException If an input or output exception occurs 141 * @exception IllegalStateException If the response was committed 142 */ 143 public void sendError(int sc, String msg) throws IOException; 144 145 /** 146 * Sends an error response to the client using the specified status 147 * code and clearing the buffer. 148 * <p>If the response has already been committed, this method throws 149 * an IllegalStateException. 150 * After using this method, the response should be considered 151 * to be committed and should not be written to. 152 * 153 * @param sc the error status code 154 * @exception IOException If an input or output exception occurs 155 * @exception IllegalStateException If the response was committed 156 * before this method call 157 */ 158 public void sendError(int sc) throws IOException; 159 160 /** 161 * Sends a temporary redirect response to the client using the 162 * specified redirect location URL. This method can accept relative URLs; 163 * the servlet container must convert the relative URL to an absolute URL 164 * before sending the response to the client. If the location is relative 165 * without a leading '/' the container interprets it as relative to 166 * the current request URI. If the location is relative with a leading 167 * '/' the container interprets it as relative to the servlet container root. 168 * 169 * <p>If the response has already been committed, this method throws 170 * an IllegalStateException. 171 * After using this method, the response should be considered 172 * to be committed and should not be written to. 173 * 174 * @param location the redirect location URL 175 * @exception IOException If an input or output exception occurs 176 * @exception IllegalStateException If the response was committed or 177 * if a partial URL is given and cannot be converted into a valid URL 178 */ 179 public void sendRedirect(String location) throws IOException; 180 181 /** 182 * Sets a response header with the given name and 183 * date-value. The date is specified in terms of 184 * milliseconds since the epoch. If the header had already 185 * been set, the new value overwrites the previous one. The 186 * <code>containsHeader</code> method can be used to test for the 187 * presence of a header before setting its value. 188 * 189 * @param name the name of the header to set 190 * @param date the assigned date value 191 * 192 * @see #containsHeader 193 * @see #addDateHeader 194 */ 195 public void setDateHeader(String name, long date); 196 197 /** 198 * Adds a response header with the given name and 199 * date-value. The date is specified in terms of 200 * milliseconds since the epoch. This method allows response headers 201 * to have multiple values. 202 * 203 * @param name the name of the header to set 204 * @param date the additional date value 205 * 206 * @see #setDateHeader 207 */ 208 public void addDateHeader(String name, long date); 209 210 /** 211 * Sets a response header with the given name and value. 212 * If the header had already been set, the new value overwrites the 213 * previous one. The <code>containsHeader</code> method can be 214 * used to test for the presence of a header before setting its 215 * value. 216 * 217 * @param name the name of the header 218 * @param value the header value If it contains octet string, 219 * it should be encoded according to RFC 2047 220 * (http://www.ietf.org/rfc/rfc2047.txt) 221 * 222 * @see #containsHeader 223 * @see #addHeader 224 */ 225 public void setHeader(String name, String value); 226 227 /** 228 * Adds a response header with the given name and value. 229 * This method allows response headers to have multiple values. 230 * 231 * @param name the name of the header 232 * @param value the additional header value If it contains octet string, 233 * it should be encoded according to RFC 2047 234 * (http://www.ietf.org/rfc/rfc2047.txt) 235 * 236 * @see #setHeader 237 */ 238 public void addHeader(String name, String value); 239 240 /** 241 * Sets a response header with the given name and 242 * integer value. If the header had already been set, the new value 243 * overwrites the previous one. The <code>containsHeader</code> 244 * method can be used to test for the presence of a header before 245 * setting its value. 246 * 247 * @param name the name of the header 248 * @param value the assigned integer value 249 * 250 * @see #containsHeader 251 * @see #addIntHeader 252 */ 253 public void setIntHeader(String name, int value); 254 255 /** 256 * Adds a response header with the given name and 257 * integer value. This method allows response headers to have multiple 258 * values. 259 * 260 * @param name the name of the header 261 * @param value the assigned integer value 262 * 263 * @see #setIntHeader 264 */ 265 public void addIntHeader(String name, int value); 266 267 /** 268 * Sets the status code for this response. This method is used to 269 * set the return status code when there is no error (for example, 270 * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there 271 * is an error, and the caller wishes to invoke an error-page defined 272 * in the web applicaion, the <code>sendError</code> method should be used 273 * instead. 274 * <p> The container clears the buffer and sets the Location header, preserving 275 * cookies and other headers. 276 * 277 * @param sc the status code 278 * 279 * @see #sendError 280 */ 281 public void setStatus(int sc); 282 283 /** 284 * @deprecated As of version 2.1, due to ambiguous meaning of the 285 * message parameter. To set a status code 286 * use <code>setStatus(int)</code>, to send an error with a description 287 * use <code>sendError(int, String)</code>. 288 * 289 * Sets the status code and message for this response. 290 * 291 * @param sc the status code 292 * @param sm the status message 293 */ 294 public void setStatus(int sc, String sm); 295 296 // 297 // Server status codes; see RFC 2068. 298 // 299 300 /** 301 * Status code (100) indicating the client can continue. 302 */ 303 public static final int SC_CONTINUE = 100; 304 305 /** 306 * Status code (101) indicating the server is switching protocols 307 * according to Upgrade header. 308 */ 309 public static final int SC_SWITCHING_PROTOCOLS = 101; 310 311 /** 312 * Status code (200) indicating the request succeeded normally. 313 */ 314 public static final int SC_OK = 200; 315 316 /** 317 * Status code (201) indicating the request succeeded and created 318 * a new resource on the server. 319 */ 320 public static final int SC_CREATED = 201; 321 322 /** 323 * Status code (202) indicating that a request was accepted for 324 * processing, but was not completed. 325 */ 326 public static final int SC_ACCEPTED = 202; 327 328 /** 329 * Status code (203) indicating that the meta information presented 330 * by the client did not originate from the server. 331 */ 332 public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203; 333 334 /** 335 * Status code (204) indicating that the request succeeded but that 336 * there was no new information to return. 337 */ 338 public static final int SC_NO_CONTENT = 204; 339 340 /** 341 * Status code (205) indicating that the agent <em>SHOULD</em> reset 342 * the document view which caused the request to be sent. 343 */ 344 public static final int SC_RESET_CONTENT = 205; 345 346 /** 347 * Status code (206) indicating that the server has fulfilled 348 * the partial GET request for the resource. 349 */ 350 public static final int SC_PARTIAL_CONTENT = 206; 351 352 /** 353 * Status code (300) indicating that the requested resource 354 * corresponds to any one of a set of representations, each with 355 * its own specific location. 356 */ 357 public static final int SC_MULTIPLE_CHOICES = 300; 358 359 /** 360 * Status code (301) indicating that the resource has permanently 361 * moved to a new location, and that future references should use a 362 * new URI with their requests. 363 */ 364 public static final int SC_MOVED_PERMANENTLY = 301; 365 366 /** 367 * Status code (302) indicating that the resource has temporarily 368 * moved to another location, but that future references should 369 * still use the original URI to access the resource. 370 * 371 * This definition is being retained for backwards compatibility. 372 * SC_FOUND is now the preferred definition. 373 */ 374 public static final int SC_MOVED_TEMPORARILY = 302; 375 376 /** 377 * Status code (302) indicating that the resource reside 378 * temporarily under a different URI. Since the redirection might 379 * be altered on occasion, the client should continue to use the 380 * Request-URI for future requests.(HTTP/1.1) To represent the 381 * status code (302), it is recommended to use this variable. 382 */ 383 public static final int SC_FOUND = 302; 384 385 /** 386 * Status code (303) indicating that the response to the request 387 * can be found under a different URI. 388 */ 389 public static final int SC_SEE_OTHER = 303; 390 391 /** 392 * Status code (304) indicating that a conditional GET operation 393 * found that the resource was available and not modified. 394 */ 395 public static final int SC_NOT_MODIFIED = 304; 396 397 /** 398 * Status code (305) indicating that the requested resource 399 * <em>MUST</em> be accessed through the proxy given by the 400 * <code><em>Location</em></code> field. 401 */ 402 public static final int SC_USE_PROXY = 305; 403 404 /** 405 * Status code (307) indicating that the requested resource 406 * resides temporarily under a different URI. The temporary URI 407 * <em>SHOULD</em> be given by the <code><em>Location</em></code> 408 * field in the response. 409 */ 410 public static final int SC_TEMPORARY_REDIRECT = 307; 411 412 /** 413 * Status code (400) indicating the request sent by the client was 414 * syntactically incorrect. 415 */ 416 public static final int SC_BAD_REQUEST = 400; 417 418 /** 419 * Status code (401) indicating that the request requires HTTP 420 * authentication. 421 */ 422 public static final int SC_UNAUTHORIZED = 401; 423 424 /** 425 * Status code (402) reserved for future use. 426 */ 427 public static final int SC_PAYMENT_REQUIRED = 402; 428 429 /** 430 * Status code (403) indicating the server understood the request 431 * but refused to fulfill it. 432 */ 433 public static final int SC_FORBIDDEN = 403; 434 435 /** 436 * Status code (404) indicating that the requested resource is not 437 * available. 438 */ 439 public static final int SC_NOT_FOUND = 404; 440 441 /** 442 * Status code (405) indicating that the method specified in the 443 * <code><em>Request-Line</em></code> is not allowed for the resource 444 * identified by the <code><em>Request-URI</em></code>. 445 */ 446 public static final int SC_METHOD_NOT_ALLOWED = 405; 447 448 /** 449 * Status code (406) indicating that the resource identified by the 450 * request is only capable of generating response entities which have 451 * content characteristics not acceptable according to the accept 452 * headers sent in the request. 453 */ 454 public static final int SC_NOT_ACCEPTABLE = 406; 455 456 /** 457 * Status code (407) indicating that the client <em>MUST</em> first 458 * authenticate itself with the proxy. 459 */ 460 public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407; 461 462 /** 463 * Status code (408) indicating that the client did not produce a 464 * request within the time that the server was prepared to wait. 465 */ 466 public static final int SC_REQUEST_TIMEOUT = 408; 467 468 /** 469 * Status code (409) indicating that the request could not be 470 * completed due to a conflict with the current state of the 471 * resource. 472 */ 473 public static final int SC_CONFLICT = 409; 474 475 /** 476 * Status code (410) indicating that the resource is no longer 477 * available at the server and no forwarding address is known. 478 * This condition <em>SHOULD</em> be considered permanent. 479 */ 480 public static final int SC_GONE = 410; 481 482 /** 483 * Status code (411) indicating that the request cannot be handled 484 * without a defined <code><em>Content-Length</em></code>. 485 */ 486 public static final int SC_LENGTH_REQUIRED = 411; 487 488 /** 489 * Status code (412) indicating that the precondition given in one 490 * or more of the request-header fields evaluated to false when it 491 * was tested on the server. 492 */ 493 public static final int SC_PRECONDITION_FAILED = 412; 494 495 /** 496 * Status code (413) indicating that the server is refusing to process 497 * the request because the request entity is larger than the server is 498 * willing or able to process. 499 */ 500 public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413; 501 502 /** 503 * Status code (414) indicating that the server is refusing to service 504 * the request because the <code><em>Request-URI</em></code> is longer 505 * than the server is willing to interpret. 506 */ 507 public static final int SC_REQUEST_URI_TOO_LONG = 414; 508 509 /** 510 * Status code (415) indicating that the server is refusing to service 511 * the request because the entity of the request is in a format not 512 * supported by the requested resource for the requested method. 513 */ 514 public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415; 515 516 /** 517 * Status code (416) indicating that the server cannot serve the 518 * requested byte range. 519 */ 520 public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; 521 522 /** 523 * Status code (417) indicating that the server could not meet the 524 * expectation given in the Expect request header. 525 */ 526 public static final int SC_EXPECTATION_FAILED = 417; 527 528 /** 529 * Status code (500) indicating an error inside the HTTP server 530 * which prevented it from fulfilling the request. 531 */ 532 public static final int SC_INTERNAL_SERVER_ERROR = 500; 533 534 /** 535 * Status code (501) indicating the HTTP server does not support 536 * the functionality needed to fulfill the request. 537 */ 538 public static final int SC_NOT_IMPLEMENTED = 501; 539 540 /** 541 * Status code (502) indicating that the HTTP server received an 542 * invalid response from a server it consulted when acting as a 543 * proxy or gateway. 544 */ 545 public static final int SC_BAD_GATEWAY = 502; 546 547 /** 548 * Status code (503) indicating that the HTTP server is 549 * temporarily overloaded, and unable to handle the request. 550 */ 551 public static final int SC_SERVICE_UNAVAILABLE = 503; 552 553 /** 554 * Status code (504) indicating that the server did not receive 555 * a timely response from the upstream server while acting as 556 * a gateway or proxy. 557 */ 558 public static final int SC_GATEWAY_TIMEOUT = 504; 559 560 /** 561 * Status code (505) indicating that the server does not support 562 * or refuses to support the HTTP protocol version that was used 563 * in the request message. 564 */ 565 public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505; 566 }