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