1
2
3 /*
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 *
6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
7 *
8 * Portions Copyright Apache Software Foundation.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.servlet.http;
42
43 import java.io.IOException;
44
45 import javax.servlet.ServletResponse;
46
47 /**
48 *
49 * Extends the {@link ServletResponse} interface to provide HTTP-specific
50 * functionality in sending a response. For example, it has methods
51 * to access HTTP headers and cookies.
52 *
53 * <p>The servlet container creates an <code>HttpServletResponse</code> object
54 * and passes it as an argument to the servlet's service methods
55 * (<code>doGet</code>, <code>doPost</code>, etc).
56 *
57 *
58 * @author Various
59 *
60 * @see javax.servlet.ServletResponse
61 *
62 */
63
64
65
66 public interface HttpServletResponse extends ServletResponse {
67
68 /**
69 * Adds the specified cookie to the response. This method can be called
70 * multiple times to set more than one cookie.
71 *
72 * @param cookie the Cookie to return to the client
73 *
74 */
75
76 public void addCookie(Cookie cookie);
77
78 /**
79 * Returns a boolean indicating whether the named response header
80 * has already been set.
81 *
82 * @param name the header name
83 * @return <code>true</code> if the named response header
84 * has already been set;
85 * <code>false</code> otherwise
86 */
87
88 public boolean containsHeader(String name);
89
90 /**
91 * Encodes the specified URL by including the session ID in it,
92 * or, if encoding is not needed, returns the URL unchanged.
93 * The implementation of this method includes the logic to
94 * determine whether the session ID needs to be encoded in the URL.
95 * For example, if the browser supports cookies, or session
96 * tracking is turned off, URL encoding is unnecessary.
97 *
98 * <p>For robust session tracking, all URLs emitted by a servlet
99 * should be run through this
100 * method. Otherwise, URL rewriting cannot be used with browsers
101 * which do not support cookies.
102 *
103 * @param url the url to be encoded.
104 * @return the encoded URL if encoding is needed;
105 * the unchanged URL otherwise.
106 */
107
108 public String encodeURL(String url);
109
110 /**
111 * Encodes the specified URL for use in the
112 * <code>sendRedirect</code> method or, if encoding is not needed,
113 * returns the URL unchanged. The implementation of this method
114 * includes the logic to determine whether the session ID
115 * needs to be encoded in the URL. Because the rules for making
116 * this determination can differ from those used to decide whether to
117 * encode a normal link, this method is separated from the
118 * <code>encodeURL</code> method.
119 *
120 * <p>All URLs sent to the <code>HttpServletResponse.sendRedirect</code>
121 * method should be run through this method. Otherwise, URL
122 * rewriting cannot be used with browsers which do not support
123 * cookies.
124 *
125 * @param url the url to be encoded.
126 * @return the encoded URL if encoding is needed;
127 * the unchanged URL otherwise.
128 *
129 * @see #sendRedirect
130 * @see #encodeUrl
131 */
132
133 public String encodeRedirectURL(String url);
134
135 /**
136 * @deprecated As of version 2.1, use encodeURL(String url) instead
137 *
138 * @param url the url to be encoded.
139 * @return the encoded URL if encoding is needed;
140 * the unchanged URL otherwise.
141 */
142
143 public String encodeUrl(String url);
144
145 /**
146 * @deprecated As of version 2.1, use
147 * encodeRedirectURL(String url) instead
148 *
149 * @param url the url to be encoded.
150 * @return the encoded URL if encoding is needed;
151 * the unchanged URL otherwise.
152 */
153
154 public String encodeRedirectUrl(String url);
155
156 /**
157 * Sends an error response to the client using the specified
158 * status. The server defaults to creating the
159 * response to look like an HTML-formatted server error page
160 * containing the specified message, setting the content type
161 * to "text/html", leaving cookies and other headers unmodified.
162 *
163 * If an error-page declaration has been made for the web application
164 * corresponding to the status code passed in, it will be served back in
165 * preference to the suggested msg parameter.
166 *
167 * <p>If the response has already been committed, this method throws
168 * an IllegalStateException.
169 * After using this method, the response should be considered
170 * to be committed and should not be written to.
171 *
172 * @param sc the error status code
173 * @param msg the descriptive message
174 * @exception IOException If an input or output exception occurs
175 * @exception IllegalStateException If the response was committed
176 */
177
178 public void sendError(int sc, String msg) throws IOException;
179
180 /**
181 * Sends an error response to the client using the specified status
182 * code and clearing the buffer.
183 * <p>If the response has already been committed, this method throws
184 * an IllegalStateException.
185 * After using this method, the response should be considered
186 * to be committed and should not be written to.
187 *
188 * @param sc the error status code
189 * @exception IOException If an input or output exception occurs
190 * @exception IllegalStateException If the response was committed
191 * before this method call
192 */
193
194 public void sendError(int sc) throws IOException;
195
196 /**
197 * Sends a temporary redirect response to the client using the
198 * specified redirect location URL. This method can accept relative URLs;
199 * the servlet container must convert the relative URL to an absolute URL
200 * before sending the response to the client. If the location is relative
201 * without a leading '/' the container interprets it as relative to
202 * the current request URI. If the location is relative with a leading
203 * '/' the container interprets it as relative to the servlet container root.
204 *
205 * <p>If the response has already been committed, this method throws
206 * an IllegalStateException.
207 * After using this method, the response should be considered
208 * to be committed and should not be written to.
209 *
210 * @param location the redirect location URL
211 * @exception IOException If an input or output exception occurs
212 * @exception IllegalStateException If the response was committed or
213 if a partial URL is given and cannot be converted into a valid URL
214 */
215
216 public void sendRedirect(String location) throws IOException;
217
218 /**
219 *
220 * Sets a response header with the given name and
221 * date-value. The date is specified in terms of
222 * milliseconds since the epoch. If the header had already
223 * been set, the new value overwrites the previous one. The
224 * <code>containsHeader</code> method can be used to test for the
225 * presence of a header before setting its value.
226 *
227 * @param name the name of the header to set
228 * @param date the assigned date value
229 *
230 * @see #containsHeader
231 * @see #addDateHeader
232 */
233
234 public void setDateHeader(String name, long date);
235
236 /**
237 *
238 * Adds a response header with the given name and
239 * date-value. The date is specified in terms of
240 * milliseconds since the epoch. This method allows response headers
241 * to have multiple values.
242 *
243 * @param name the name of the header to set
244 * @param date the additional date value
245 *
246 * @see #setDateHeader
247 */
248
249 public void addDateHeader(String name, long date);
250
251 /**
252 *
253 * Sets a response header with the given name and value.
254 * If the header had already been set, the new value overwrites the
255 * previous one. The <code>containsHeader</code> method can be
256 * used to test for the presence of a header before setting its
257 * value.
258 *
259 * @param name the name of the header
260 * @param value the header value If it contains octet string,
261 * it should be encoded according to RFC 2047
262 * (http://www.ietf.org/rfc/rfc2047.txt)
263 *
264 * @see #containsHeader
265 * @see #addHeader
266 */
267
268 public void setHeader(String name, String value);
269
270 /**
271 * Adds a response header with the given name and value.
272 * This method allows response headers to have multiple values.
273 *
274 * @param name the name of the header
275 * @param value the additional header value If it contains
276 * octet string, it should be encoded
277 * according to RFC 2047
278 * (http://www.ietf.org/rfc/rfc2047.txt)
279 *
280 * @see #setHeader
281 */
282
283 public void addHeader(String name, String value);
284
285 /**
286 * Sets a response header with the given name and
287 * integer value. If the header had already been set, the new value
288 * overwrites the previous one. The <code>containsHeader</code>
289 * method can be used to test for the presence of a header before
290 * setting its value.
291 *
292 * @param name the name of the header
293 * @param value the assigned integer value
294 *
295 * @see #containsHeader
296 * @see #addIntHeader
297 */
298
299 public void setIntHeader(String name, int value);
300
301 /**
302 * Adds a response header with the given name and
303 * integer value. This method allows response headers to have multiple
304 * values.
305 *
306 * @param name the name of the header
307 * @param value the assigned integer value
308 *
309 * @see #setIntHeader
310 */
311
312 public void addIntHeader(String name, int value);
313
314
315
316 /**
317 * Sets the status code for this response. This method is used to
318 * set the return status code when there is no error (for example,
319 * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there
320 * is an error, and the caller wishes to invoke an error-page defined
321 * in the web application, the <code>sendError</code> method should be used
322 * instead.
323 * <p> The container clears the buffer and sets the Location header, preserving
324 * cookies and other headers.
325 *
326 * @param sc the status code
327 *
328 * @see #sendError
329 */
330
331 public void setStatus(int sc);
332
333 /**
334 * @deprecated As of version 2.1, due to ambiguous meaning of the
335 * message parameter. To set a status code
336 * use <code>setStatus(int)</code>, to send an error with a description
337 * use <code>sendError(int, String)</code>.
338 *
339 * Sets the status code and message for this response.
340 *
341 * @param sc the status code
342 * @param sm the status message
343 */
344
345 public void setStatus(int sc, String sm);
346
347
348 /*
349 * Server status codes; see RFC 2068.
350 */
351
352 /**
353 * Status code (100) indicating the client can continue.
354 */
355
356 public static final int SC_CONTINUE = 100;
357
358
359 /**
360 * Status code (101) indicating the server is switching protocols
361 * according to Upgrade header.
362 */
363
364 public static final int SC_SWITCHING_PROTOCOLS = 101;
365
366 /**
367 * Status code (200) indicating the request succeeded normally.
368 */
369
370 public static final int SC_OK = 200;
371
372 /**
373 * Status code (201) indicating the request succeeded and created
374 * a new resource on the server.
375 */
376
377 public static final int SC_CREATED = 201;
378
379 /**
380 * Status code (202) indicating that a request was accepted for
381 * processing, but was not completed.
382 */
383
384 public static final int SC_ACCEPTED = 202;
385
386 /**
387 * Status code (203) indicating that the meta information presented
388 * by the client did not originate from the server.
389 */
390
391 public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
392
393 /**
394 * Status code (204) indicating that the request succeeded but that
395 * there was no new information to return.
396 */
397
398 public static final int SC_NO_CONTENT = 204;
399
400 /**
401 * Status code (205) indicating that the agent <em>SHOULD</em> reset
402 * the document view which caused the request to be sent.
403 */
404
405 public static final int SC_RESET_CONTENT = 205;
406
407 /**
408 * Status code (206) indicating that the server has fulfilled
409 * the partial GET request for the resource.
410 */
411
412 public static final int SC_PARTIAL_CONTENT = 206;
413
414 /**
415 * Status code (300) indicating that the requested resource
416 * corresponds to any one of a set of representations, each with
417 * its own specific location.
418 */
419
420 public static final int SC_MULTIPLE_CHOICES = 300;
421
422 /**
423 * Status code (301) indicating that the resource has permanently
424 * moved to a new location, and that future references should use a
425 * new URI with their requests.
426 */
427
428 public static final int SC_MOVED_PERMANENTLY = 301;
429
430 /**
431 * Status code (302) indicating that the resource has temporarily
432 * moved to another location, but that future references should
433 * still use the original URI to access the resource.
434 *
435 * This definition is being retained for backwards compatibility.
436 * SC_FOUND is now the preferred definition.
437 */
438
439 public static final int SC_MOVED_TEMPORARILY = 302;
440
441 /**
442 * Status code (302) indicating that the resource reside
443 * temporarily under a different URI. Since the redirection might
444 * be altered on occasion, the client should continue to use the
445 * Request-URI for future requests.(HTTP/1.1) To represent the
446 * status code (302), it is recommended to use this variable.
447 */
448
449 public static final int SC_FOUND = 302;
450
451 /**
452 * Status code (303) indicating that the response to the request
453 * can be found under a different URI.
454 */
455
456 public static final int SC_SEE_OTHER = 303;
457
458 /**
459 * Status code (304) indicating that a conditional GET operation
460 * found that the resource was available and not modified.
461 */
462
463 public static final int SC_NOT_MODIFIED = 304;
464
465 /**
466 * Status code (305) indicating that the requested resource
467 * <em>MUST</em> be accessed through the proxy given by the
468 * <code><em>Location</em></code> field.
469 */
470
471 public static final int SC_USE_PROXY = 305;
472
473 /**
474 * Status code (307) indicating that the requested resource
475 * resides temporarily under a different URI. The temporary URI
476 * <em>SHOULD</em> be given by the <code><em>Location</em></code>
477 * field in the response.
478 */
479
480 public static final int SC_TEMPORARY_REDIRECT = 307;
481
482 /**
483 * Status code (400) indicating the request sent by the client was
484 * syntactically incorrect.
485 */
486
487 public static final int SC_BAD_REQUEST = 400;
488
489 /**
490 * Status code (401) indicating that the request requires HTTP
491 * authentication.
492 */
493
494 public static final int SC_UNAUTHORIZED = 401;
495
496 /**
497 * Status code (402) reserved for future use.
498 */
499
500 public static final int SC_PAYMENT_REQUIRED = 402;
501
502 /**
503 * Status code (403) indicating the server understood the request
504 * but refused to fulfill it.
505 */
506
507 public static final int SC_FORBIDDEN = 403;
508
509 /**
510 * Status code (404) indicating that the requested resource is not
511 * available.
512 */
513
514 public static final int SC_NOT_FOUND = 404;
515
516 /**
517 * Status code (405) indicating that the method specified in the
518 * <code><em>Request-Line</em></code> is not allowed for the resource
519 * identified by the <code><em>Request-URI</em></code>.
520 */
521
522 public static final int SC_METHOD_NOT_ALLOWED = 405;
523
524 /**
525 * Status code (406) indicating that the resource identified by the
526 * request is only capable of generating response entities which have
527 * content characteristics not acceptable according to the accept
528 * headers sent in the request.
529 */
530
531 public static final int SC_NOT_ACCEPTABLE = 406;
532
533 /**
534 * Status code (407) indicating that the client <em>MUST</em> first
535 * authenticate itself with the proxy.
536 */
537
538 public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
539
540 /**
541 * Status code (408) indicating that the client did not produce a
542 * request within the time that the server was prepared to wait.
543 */
544
545 public static final int SC_REQUEST_TIMEOUT = 408;
546
547 /**
548 * Status code (409) indicating that the request could not be
549 * completed due to a conflict with the current state of the
550 * resource.
551 */
552
553 public static final int SC_CONFLICT = 409;
554
555 /**
556 * Status code (410) indicating that the resource is no longer
557 * available at the server and no forwarding address is known.
558 * This condition <em>SHOULD</em> be considered permanent.
559 */
560
561 public static final int SC_GONE = 410;
562
563 /**
564 * Status code (411) indicating that the request cannot be handled
565 * without a defined <code><em>Content-Length</em></code>.
566 */
567
568 public static final int SC_LENGTH_REQUIRED = 411;
569
570 /**
571 * Status code (412) indicating that the precondition given in one
572 * or more of the request-header fields evaluated to false when it
573 * was tested on the server.
574 */
575
576 public static final int SC_PRECONDITION_FAILED = 412;
577
578 /**
579 * Status code (413) indicating that the server is refusing to process
580 * the request because the request entity is larger than the server is
581 * willing or able to process.
582 */
583
584 public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;
585
586 /**
587 * Status code (414) indicating that the server is refusing to service
588 * the request because the <code><em>Request-URI</em></code> is longer
589 * than the server is willing to interpret.
590 */
591
592 public static final int SC_REQUEST_URI_TOO_LONG = 414;
593
594 /**
595 * Status code (415) indicating that the server is refusing to service
596 * the request because the entity of the request is in a format not
597 * supported by the requested resource for the requested method.
598 */
599
600 public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
601
602 /**
603 * Status code (416) indicating that the server cannot serve the
604 * requested byte range.
605 */
606
607 public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
608
609 /**
610 * Status code (417) indicating that the server could not meet the
611 * expectation given in the Expect request header.
612 */
613
614 public static final int SC_EXPECTATION_FAILED = 417;
615
616 /**
617 * Status code (500) indicating an error inside the HTTP server
618 * which prevented it from fulfilling the request.
619 */
620
621 public static final int SC_INTERNAL_SERVER_ERROR = 500;
622
623 /**
624 * Status code (501) indicating the HTTP server does not support
625 * the functionality needed to fulfill the request.
626 */
627
628 public static final int SC_NOT_IMPLEMENTED = 501;
629
630 /**
631 * Status code (502) indicating that the HTTP server received an
632 * invalid response from a server it consulted when acting as a
633 * proxy or gateway.
634 */
635
636 public static final int SC_BAD_GATEWAY = 502;
637
638 /**
639 * Status code (503) indicating that the HTTP server is
640 * temporarily overloaded, and unable to handle the request.
641 */
642
643 public static final int SC_SERVICE_UNAVAILABLE = 503;
644
645 /**
646 * Status code (504) indicating that the server did not receive
647 * a timely response from the upstream server while acting as
648 * a gateway or proxy.
649 */
650
651 public static final int SC_GATEWAY_TIMEOUT = 504;
652
653 /**
654 * Status code (505) indicating that the server does not support
655 * or refuses to support the HTTP protocol version that was used
656 * in the request message.
657 */
658
659 public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
660 }