NAME

Servlet::ServletRequest - servlet request interface

SYNOPSIS

for my $name ($request->getAttributeNames()) {
    my $val = $request->getAttribute($name);
    $request->removeAttribute($name);
    # or
    $request->setAttribute($name, $newValue);
}

my $encoding = $request->getCharacterEncoding();
$request->setCharacterEncoding($newEncoding);

my $length = $request->getContentLength();

my $type = $request->getContentType();

# gets request body as binary data
my $input = $request->getInputHandle();

# gets preferred locale
my $locale = $request->getLocale();

# gets all locales in descending order of preference
my @locales = $request->getLocales();

my %paramMap = $request->getParameterMap();
for my $name ($request->getParameterNames()) {
    my $val = $request->getParameter($name);
    # or
    my @vals = $request->getParameterValues($name);
}

my $protocol = $request->getProtocol();

# gets request body as character data, converted from bytes using
# the request's character encoding
my $reader = $request->getReader();

my $addr = $request->getRemoteAddr();

my $host = $request->getRemoteHost();

# get a request dispatcher in order to do an include or forward
my $dispatcher = $request->getRequestDispatcher($path);

my $scheme = $request->getScheme();

my $server = $request->getServerName();

my $port = $request->getServerPort();

my $flag = $request->isSecure();

DESCRIPTION

This interface defines an object that provides client request information to a servlet. The servlet container creates a request object and passes it as an argument to the servlet's service() method.

A Servlet::ServletRequest object provides data including parameter name and values, attributes, and an input handle. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by Servlet::Http::HttpServletRequest.

METHODS

getAttribute($name)

Returns the value of the named attribute, or undef if no attribute of the given name exists.

Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute Servlet::Request::X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using setAttribute(). This allows information to be embedded into a request before a Servlet::RequestDispatcher call.

Attribute names should follow the same convention as package names. The Servlet API specification reserves names matching main::*, CORE::*, UNIVERSAL::*, and any other standard reserved package names.

Parameters:

$name

The name of the attribute

getAttributeNames()

Returns an array containing the names of the attributes available to this request, or an empty array if the request has no attributes available to it.

getCharacterEncoding()

Returns the name of the character encoding used in the body of this request, or undef if the request does not specify a character encoding.

getContentLength()

Returns the length, in bytes, of the request body and made available by the input handle, or undef if the length is not known. For HTTP servlets, same as the value of the CGI variable CONTENT_LENGTH.

getContentType()

Returns the MIME type of the body of the request, or undef if the type is not known. For HTTP servlets, same as the value of the CGI variable CONTENT_TYPE.

getInputHandle()

Retrieves the body of the request as binary data using a IO::Handle. Either this method or getReader() may be called to read the body, not both.

Throws:

Servlet::Util::IllegalStateException

if the getReader() method has already been called for this request

Servlet::Util::IOException

if an input or output exception occurred

getLocale()

Returns the preferred locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server.

getLocales()

Returns an array of locales indicating in decreasing order of preference the locales that are acceptable to the client based on the Accept-Language header. If the client request doesn't provde an Accept-Language header, this method returns an array containing one locale, the default locale for the server.

getParameter($name)

Returns the value of a request parameter, or undef if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues().

If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues().

If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputHandle() or getReader() can interfere with the execution of this method.

Parameters:

$name

The name of the parameter

getParameterMap()

Returns a hash of the parameters of this request. The keys of the hash are the parameter names, and the values of the hash are arrays of parameter values.

See getParameter() for more information about parameters and usage.

getParameterNames()

Returns an array containing the names of the parameters contained in this request. If the request has no parameters, the array is empty.

See getParameter() for more information about parameters and usage.

getParameterValues($name)

Returns an array containing all of the values of the given request parameter, or undef if the parameter does not exist.

If the parameter has a single value, the array has a length of 1. If the parameter has no value, the array is empty.

See getParameter() for more information about parameters and usage.

Parameters:

$name

The name of the parameter

getProtocol()

Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1. For HTTP servlets, the value returned is the same as the value of the CGI variable SERVER_PROTOCOL.

getReader()

Retrieves the body of the request as character data using a XXX. The reader translates the character data according to the character encoding used on the body. Either this method or getInputHandle() may be called to read the body, not both.

Throws:

Servlet::Util::UnsupportedEncodingException

if the character encoding used is not supported and the text cannot be decoded

Servlet::Util::IllegalStateException

if the getInputHandle() method has already been called for this request

Servlet::Util::IOException

if an input or output exception occurred

getRemoteAddr()

Returns the Internet Protocol (IP) address of the client that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR.

getRemoteHost()

Returns the fully qualified name of the client that sent the request, or the IP address of the client if the name cannot be determined. For HTTP servlets, same as the value of the CGI variable REMOTE_HOST.

getRequestDispatcher($path)

Returns a Servlet::RequestDispatcher object that acts as a wrapper for the resource located at the given path. The object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/", it is interpreted as relative to the current context root. This method returns undef if the servlet cannot return a dispatcher.

The difference between this method and the one provided by Servlet::ServletContext is that this method can take a relative path.

Parameters:

$path

The path to the resource

getScheme()

Returns the name of th scheme used to make this request, for example, http, https, or ftp. Different schemes have different rules for constructing URLs, as noted in RFC 1738.

getServerName()

Returns the host name of the server that received the request. For HTTP servlets, same as the value of the CGI variable SERVER_NAME.

getServerPort()

Returns the port number on which this request was received. For HTTP servlets, same as the value of the CGI variable SERVER_PORT.

isSecure()

Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

removeAttribute($name)

Removes an attribute from this request. This method is not generally needed as attributes only persist as long as the request is being handled.

See getAttribute() for information about allowable attribute names.

Parameters:

$name

The name of the attribute to remove

setAttribute($name, $object)

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with Servlet::RequestDispatcher.

See getAttribute() for information about allowable attribute names.

Parameters:

$name

The name of the attribute to set

$object

The object to be stored. Can be a scalar or a reference to an arbitrary data structure.

setCharacterEncoding($name)

Overrides the name of the character encoding used for the body of this request. This method must be called prior to reading request parameters or reading input using getReader().

Parameters:

$name

The name of the encoding to set

Throws:

Servlet::Util::UnsupportedEncodingException

if this is not a valid encoding

SEE ALSO

IO::Handle, Servlet::RequestDispatcher

AUTHOR

Brian Moseley, bcm@maz.org