WsResponse

Property Summary

Methods Summary

Properties Details

characterEncoding

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by #setContentType(String) or #setLocale, this method overrides it. Calling #setContentType(String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Returns String

Sample

contentType

Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.

This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Returns String

Sample

localeLanguageTag

Returns the locale specified for this response using the #setLocale method. Calls made to setLocale after the response is committed have no effect. If no locale has been specified, the container's default locale is returned.

If the specified language tag contains any ill-formed subtags, the first such subtag and all following subtags are ignored. Compare to Locale.Builder#setLanguageTag(String) which throws an exception in this case.

The following conversions are performed:

  • The language code "und" is mapped to language "".

  • The language codes "he", "yi", and "id" are mapped to "iw", "ji", and "in" respectively. (This is the same canonicalization that's done in Locale's constructors.)

  • The portion of a private use subtag prefixed by "lvariant", if any, is removed and appended to the variant field in the result locale (without case normalization). If it is then empty, the private use subtag is discarded:

        Locale loc;
        loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
        loc.getVariant(); // returns "POSIX"
        loc.getExtension('x'); // returns null
    
        loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
        loc.getVariant(); // returns "POSIX_Abc_Def"
        loc.getExtension('x'); // returns "urp"
  • When the languageTag argument contains an extlang subtag, the first such subtag is used as the language, and the primary language subtag and other extlang subtags are ignored:

        Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
        Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
  • Case is normalized except for variant tags, which are left unchanged. Language is normalized to lower case, script to title case, country to upper case, and extensions to lower case.

  • If, after processing, the locale would exactly match either ja_JP_JP or th_TH_TH with no extensions, the appropriate extensions are added as though the constructor had been called:

       Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
       // returns "ja-JP-u-ca-japanese-x-lvariant-JP"
       Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
       // returns "th-TH-u-nu-thai-x-lvariant-TH"

This implements the 'Language-Tag' production of BCP47, and so supports grandfathered (regular and irregular) as well as private use language tags. Stand alone private use tags are represented as empty language and extension 'x-whatever', and grandfathered tags are converted to their canonical replacements where they exist.

Grandfathered tags with canonical replacements are as follows:

Grandfathered tags with no modern replacement will be converted as follows:

For a list of all grandfathered tags, see the IANA Language Subtag Registry (search for "Type: grandfathered").

Note: there is no guarantee that toLanguageTag and forLanguageTag will round-trip. **Returns**\ [String](../../js-lib/string.md) **Sample** ```javascript ``` ### status Sets the status code for this response.

This method is used to set the return status code when there is no error (for example, for the SC_OK or SC_MOVED_TEMPORARILY status codes).

If this method is used to set an error code, then the container's error page mechanism will not be triggered. If there is an error and the caller wishes to invoke an error page defined in the web application, then #sendError(int) must be used instead.

This method preserves any cookies and other response headers.

Valid status codes are those in the 2XX, 3XX, 4XX, and 5XX ranges. Other status codes are treated as container specific. **Returns**\ [Number](../../js-lib/number.md) **Sample** ```javascript ``` ## Methods Details ### addCookie(cookie) Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie. **Parameters**\ [WsCookie](../../wscookie.md) cookie the Cookie to return to the client **Returns**\ void **Sample** ```javascript ``` ### addDateHeader(name, date) Adds a response header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. This method allows response headers to have multiple values. **Parameters**\ [String](../../js-lib/string.md) name the name of the header to set\ [Number](../../js-lib/number.md) date the additional date value **Returns**\ void **Sample** ```javascript ``` ### addHeader(name, value) Adds a response header with the given name and value. This method allows response headers to have multiple values. **Parameters**\ [String](../../js-lib/string.md) name the name of the header\ [String](../../js-lib/string.md) value the additional header value If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) **Returns**\ void **Sample** ```javascript ``` ### addIntHeader(name, value) Adds a response header with the given name and integer value. This method allows response headers to have multiple values. **Parameters**\ [String](../../js-lib/string.md) name the name of the header\ [Number](../../js-lib/number.md) value the assigned integer value **Returns**\ void **Sample** ```javascript ``` ### containsHeader(name) Returns a boolean indicating whether the named response header has already been set. **Parameters**\ [String](../../js-lib/string.md) name the header name **Returns**\ [Boolean](../../js-lib/boolean.md) true if the named response header has already been set; false otherwise **Sample** ```javascript ``` ### getHeader(name) Gets the value of the response header with the given name.

If a response header with the given name exists and contains multiple values, the value that was added first will be returned.

This method considers only response headers set or added via #setHeader(String,String), #addHeader(String,String), #setDateHeader(String,long), #addDateHeader(String,long), #setIntHeader(String,int), or #addIntHeader(String,int), respectively. **Parameters**\ [String](../../js-lib/string.md) name the name of the response header whose value to return **Returns**\ [String](../../js-lib/string.md) the value of the response header with the given name, or null if no header with the given name has been set on this response **Sample** ```javascript ``` ### getHeaderNames() Gets the names of the headers of this response.

This method considers only response headers set or added via #setHeader(String,String), #addHeader(String,String), #setDateHeader(String,long), #addDateHeader(String,long), #setIntHeader(String,int), or #addIntHeader(String,int), respectively. **Returns**\ [Array](../../js-lib/array.md) a (possibly empty) array of the names of the headers of this response **Sample** ```javascript ``` ### getHeaders(name) Gets the values of the response header with the given name.

This method considers only response headers set or added via #setHeader(String,String), #addHeader(String,String), #setDateHeader(String,long), #addDateHeader(String,long), #setIntHeader(String,int), or #addIntHeader(String,int), respectively. **Parameters**\ [String](../../js-lib/string.md) name the name of the response header whose values to return **Returns**\ [Array](../../js-lib/array.md) a (possibly empty) array of the values of the response header with the given name **Sample** ```javascript ``` ### sendError(sc) **Parameters**\ [Number](../../js-lib/number.md) sc ; **Returns**\ void **Sample** ```javascript ``` ### sendError(sc, msg) **Parameters**\ [Number](../../js-lib/number.md) sc ;\ [String](../../js-lib/string.md) msg ; **Returns**\ void **Sample** ```javascript ``` ### setDateHeader(name, date) Sets a response header with the given name and date-value. The date is specified in terms of milliseconds since the epoch. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value. **Parameters**\ [String](../../js-lib/string.md) name the name of the header to set\ [Number](../../js-lib/number.md) date the assigned date value **Returns**\ void **Sample** ```javascript ``` ### setHeader(name, value) Sets a response header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value. **Parameters**\ [String](../../js-lib/string.md) name the name of the header\ [String](../../js-lib/string.md) value the header value If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) **Returns**\ void **Sample** ```javascript ``` ### setIntHeader(name, value) Sets a response header with the given name and integer value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value. **Parameters**\ [String](../../js-lib/string.md) name the name of the header\ [Number](../../js-lib/number.md) value the assigned integer value **Returns**\ void **Sample** ```javascript ```

Last updated