The WsResponse object represents a REST-WS response and is only valid while handling a REST-WS request. It allows manipulation of response headers, content type, status, and other properties essential for crafting HTTP responses.
The WsResponse object provides several properties to configure the HTTP response. For instance, characterEncoding sets the MIME charset, such as UTF-8. The contentType allows specifying the content type of the response, including optional character encoding. The localeLanguageTag retrieves the locale of the response, and the status property sets the HTTP status code for the response.
Methods like addCookie, addHeader, and setHeader are available for adding or setting cookies and headers in the response. Additional methods, such as sendError, allow setting error codes and messages, while setDateHeader and addDateHeader handle date-related headers. Other methods like getHeader and getHeaderNames facilitate inspecting headers in the response.
For overall REST-WS operations, refer to the Creating REST API section.
Properties Summarized
Type
Name
Summary
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.
Sets the content type of the response being sent to the client, if the response has not been committed yet.
Returns the locale specified for this response using the #setLocale method.
Sets the status code for this response.
Methods Summarized
Type
Name
Summary
void
Adds the specified cookie to the response.
void
Adds a response header with the given name and date-value.
void
Adds a response header with the given name and value.
void
Adds a response header with the given name and integer value.
Returns a boolean indicating whether the named response header has already been set.
Gets the value of the response header with the given name.
Gets the names of the headers of this response.
Gets the values of the response header with the given name.
void
void
void
Sets a response header with the given name and date-value.
void
Sets a response header with the given name and value.
void
Sets a response header with the given name and integer value.
Properties Detailed
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.
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 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:
- 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:
```js
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:
```js
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.
Type
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.
Type
Methods Detailed
addCookie(cookie)
Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.
Parameters
cookie the Cookie to return to the client
Returns: void
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
name the name of the header to set
date the additional date value
Returns: void
addHeader(name, value)
Adds a response header with the given name and value.
This method allows response headers to have multiple values.
Parameters
name the name of the header
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### 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](../../../servoycore/dev-api/js-lib/string.md) **name** the name of the header* [Number](../../../servoycore/dev-api/js-lib/number.md) **value** the assigned integer value**Returns:** void### containsHeader\(name\)Returns a boolean indicating whether the named response header has already been set\.**Parameters*** [String](../../../servoycore/dev-api/js-lib/string.md) **name** the header name**Returns:** [Boolean](../../../servoycore/dev-api/js-lib/boolean.md) `true` if the named response header has already been set; `false` otherwise### 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](../../../servoycore/dev-api/js-lib/string.md) **name** the name of the response header whose value to return**Returns:** [String](../../../servoycore/dev-api/js-lib/string.md) the value of the response header with the given name, or \<tt\>null\</tt\> if no header with the given name has been set on this response### 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](../../../servoycore/dev-api/js-lib/array.md) a \(possibly empty\) array of the names of the headers of this response### 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](../../../servoycore/dev-api/js-lib/string.md) **name** the name of the response header whose values to return**Returns:** [Array](../../../servoycore/dev-api/js-lib/array.md) a \(possibly empty\) array of the values of the response header with the given name### sendError\(sc\)**Parameters*** [Number](../../../servoycore/dev-api/js-lib/number.md) **sc** ;**Returns:** void### sendError\(sc, msg\)**Parameters*** [Number](../../../servoycore/dev-api/js-lib/number.md) **sc** ;* [String](../../../servoycore/dev-api/js-lib/string.md) **msg** ;**Returns:** void### 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](../../../servoycore/dev-api/js-lib/string.md) **name** the name of the header to set* [Number](../../../servoycore/dev-api/js-lib/number.md) **date** the assigned date value**Returns:** void### 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](../../../servoycore/dev-api/js-lib/string.md) **name** the name of the header* [String](../../../servoycore/dev-api/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
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
name the name of the header
value the assigned integer value
Returns: void