Package jakarta.servlet
Class ServletRequestWrapper
java.lang.Object
jakarta.servlet.ServletRequestWrapper
- All Implemented Interfaces:
ServletRequest
- Direct Known Subclasses:
HttpServletRequestWrapper
Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to
adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling
through to the wrapped request object.
- Since:
- Servlet 2.3
- See Also:
-
Constructor Summary
ConstructorDescriptionServletRequestWrapper
(ServletRequest request) Creates a ServletRequest adaptor wrapping the given request object. -
Method Summary
Modifier and TypeMethodDescriptionGets the AsyncContext that was created or reinitialized by the most recent invocation ofstartAsync()
orstartAsync(ServletRequest,ServletResponse)
on the wrapped request.getAttribute
(String name) The default behavior of this method is to call getAttribute(String name) on the wrapped request object.The default behavior of this method is to return getAttributeNames() on the wrapped request object.The default behavior of this method is to return getCharacterEncoding() on the wrapped request object.int
The default behavior of this method is to return getContentLength() on the wrapped request object.long
The default behavior of this method is to return getContentLengthLong() on the wrapped request object.The default behavior of this method is to return getContentType() on the wrapped request object.Gets the dispatcher type of the wrapped request.The default behavior of this method is to return getInputStream() on the wrapped request object.The default behavior of this method is to return getLocalAddr() on the wrapped request object.The default behavior of this method is to return getLocale() on the wrapped request object.The default behavior of this method is to return getLocales() on the wrapped request object.The default behavior of this method is to return getLocalName() on the wrapped request object.int
The default behavior of this method is to return getLocalPort() on the wrapped request object.getParameter
(String name) The default behavior of this method is to return getParameter(String name) on the wrapped request object.The default behavior of this method is to return getParameterMap() on the wrapped request object.The default behavior of this method is to return getParameterNames() on the wrapped request object.String[]
getParameterValues
(String name) The default behavior of this method is to return getParameterValues(String name) on the wrapped request object.The default behavior of this method is to return getProtocol() on the wrapped request object.Gets the protocol defined request ID, if any, for the wrapped request.The default behavior of this method is to return getReader() on the wrapped request object.The default behavior of this method is to return getRemoteAddr() on the wrapped request object.The default behavior of this method is to return getRemoteHost() on the wrapped request object.int
The default behavior of this method is to return getRemotePort() on the wrapped request object.Return the wrapped request object.getRequestDispatcher
(String path) The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.Gets the request ID for the wrapped request.The default behavior of this method is to return getScheme() on the wrapped request object.The default behavior of this method is to return getServerName() on the wrapped request object.int
The default behavior of this method is to return getServerPort() on the wrapped request object.Gets the connection information for the wrapped request.Gets the servlet context to which the wrapped servlet request was last dispatched.boolean
Checks if the wrapped request has been put into asynchronous mode.boolean
Checks if the wrapped request supports asynchronous operation.boolean
isSecure()
The default behavior of this method is to return isSecure() on the wrapped request object.boolean
isWrapperFor
(ServletRequest wrapped) Checks (recursively) if this ServletRequestWrapper wraps the givenServletRequest
instance.boolean
isWrapperFor
(Class<?> wrappedType) Checks (recursively) if this ServletRequestWrapper wraps aServletRequest
of the given class type.void
removeAttribute
(String name) The default behavior of this method is to call removeAttribute(String name) on the wrapped request object.void
setAttribute
(String name, Object o) The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.void
setCharacterEncoding
(String encoding) The default behavior of this method is to set the character encoding on the wrapped request object.void
setCharacterEncoding
(Charset encoding) The default behavior of this method is to set the character encoding on the wrapped request object.void
setRequest
(ServletRequest request) Sets the request object being wrapped.The default behavior of this method is to invokeServletRequest.startAsync()
on the wrapped request object.startAsync
(ServletRequest servletRequest, ServletResponse servletResponse) The default behavior of this method is to invokeServletRequest.startAsync(ServletRequest, ServletResponse)
on the wrapped request object.
-
Constructor Details
-
ServletRequestWrapper
Creates a ServletRequest adaptor wrapping the given request object.- Parameters:
request
- theServletRequest
to be wrapped- Throws:
IllegalArgumentException
- if the request is null
-
-
Method Details
-
getRequest
Return the wrapped request object.- Returns:
- the wrapped
ServletRequest
-
setRequest
Sets the request object being wrapped.- Parameters:
request
- theServletRequest
to be installed- Throws:
IllegalArgumentException
- if the request is null.
-
getAttribute
The default behavior of this method is to call getAttribute(String name) on the wrapped request object.- Specified by:
getAttribute
in interfaceServletRequest
- Parameters:
name
- aString
specifying the name of the attribute- Returns:
- an
Object
containing the value of the attribute, ornull
if the attribute does not exist
-
getAttributeNames
The default behavior of this method is to return getAttributeNames() on the wrapped request object.- Specified by:
getAttributeNames
in interfaceServletRequest
- Returns:
- an
Enumeration
of strings containing the names of the request's attributes
-
getCharacterEncoding
The default behavior of this method is to return getCharacterEncoding() on the wrapped request object.- Specified by:
getCharacterEncoding
in interfaceServletRequest
- Returns:
- a
String
containing the name of the character encoding, ornull
if the request does not specify a character encoding
-
setCharacterEncoding
The default behavior of this method is to set the character encoding on the wrapped request object.- Specified by:
setCharacterEncoding
in interfaceServletRequest
- Parameters:
encoding
-String
containing the name of the character encoding.- Throws:
UnsupportedEncodingException
- if this ServletRequest is still in a state where a character encoding may be set, but the specified encoding is invalid
-
setCharacterEncoding
The default behavior of this method is to set the character encoding on the wrapped request object.- Specified by:
setCharacterEncoding
in interfaceServletRequest
- Parameters:
encoding
-Charset
representing the character encoding.- Since:
- Servlet 6.1
-
getContentLength
public int getContentLength()The default behavior of this method is to return getContentLength() on the wrapped request object.- Specified by:
getContentLength
in interfaceServletRequest
- Returns:
- an integer containing the length of the request body or -1 if the length is not known or is greater than Integer.MAX_VALUE.
-
getContentLengthLong
public long getContentLengthLong()The default behavior of this method is to return getContentLengthLong() on the wrapped request object.- Specified by:
getContentLengthLong
in interfaceServletRequest
- Returns:
- a long containing the length of the request body or -1L if the length is not known
- Since:
- Servlet 3.1
-
getContentType
The default behavior of this method is to return getContentType() on the wrapped request object.- Specified by:
getContentType
in interfaceServletRequest
- Returns:
- a
String
containing the name of the MIME type of the request, or null if the type is not known
-
getInputStream
The default behavior of this method is to return getInputStream() on the wrapped request object.- Specified by:
getInputStream
in interfaceServletRequest
- Returns:
- a
ServletInputStream
object containing the body of the request - Throws:
IOException
- if an input or output exception occurred
-
getParameter
The default behavior of this method is to return getParameter(String name) on the wrapped request object.- Specified by:
getParameter
in interfaceServletRequest
- Parameters:
name
- aString
specifying the name of the parameter- Returns:
- a
String
representing the single value of the parameter - See Also:
-
getParameterMap
The default behavior of this method is to return getParameterMap() on the wrapped request object.- Specified by:
getParameterMap
in interfaceServletRequest
- Returns:
- an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.
-
getParameterNames
The default behavior of this method is to return getParameterNames() on the wrapped request object.- Specified by:
getParameterNames
in interfaceServletRequest
- Returns:
- an
Enumeration
ofString
objects, eachString
containing the name of a request parameter; or an emptyEnumeration
if the request has no parameters
-
getParameterValues
The default behavior of this method is to return getParameterValues(String name) on the wrapped request object.- Specified by:
getParameterValues
in interfaceServletRequest
- Parameters:
name
- aString
containing the name of the parameter whose value is requested- Returns:
- an array of
String
objects containing the parameter's values - See Also:
-
getProtocol
The default behavior of this method is to return getProtocol() on the wrapped request object.- Specified by:
getProtocol
in interfaceServletRequest
- Returns:
- a
String
containing the protocol name and version number
-
getScheme
The default behavior of this method is to return getScheme() on the wrapped request object.- Specified by:
getScheme
in interfaceServletRequest
- Returns:
- a
String
containing the name of the scheme used to make this request
-
getServerName
The default behavior of this method is to return getServerName() on the wrapped request object.- Specified by:
getServerName
in interfaceServletRequest
- Returns:
- a
String
containing the name of the server
-
getServerPort
public int getServerPort()The default behavior of this method is to return getServerPort() on the wrapped request object.- Specified by:
getServerPort
in interfaceServletRequest
- Returns:
- an integer specifying the port number
-
getReader
The default behavior of this method is to return getReader() on the wrapped request object.- Specified by:
getReader
in interfaceServletRequest
- Returns:
- a
BufferedReader
containing the body of the request - Throws:
UnsupportedEncodingException
- if the character set encoding used is not supported and the text cannot be decodedIOException
- if an input or output exception occurred- See Also:
-
getRemoteAddr
The default behavior of this method is to return getRemoteAddr() on the wrapped request object.- Specified by:
getRemoteAddr
in interfaceServletRequest
- Returns:
- a
String
containing an IP address
-
getRemoteHost
The default behavior of this method is to return getRemoteHost() on the wrapped request object.- Specified by:
getRemoteHost
in interfaceServletRequest
- Returns:
- a
String
containing a fully qualified name or IP address.
-
setAttribute
The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.- Specified by:
setAttribute
in interfaceServletRequest
- Parameters:
name
- aString
specifying the name of the attributeo
- theObject
to be stored
-
removeAttribute
The default behavior of this method is to call removeAttribute(String name) on the wrapped request object.- Specified by:
removeAttribute
in interfaceServletRequest
- Parameters:
name
- aString
specifying the name of the attribute to remove
-
getLocale
The default behavior of this method is to return getLocale() on the wrapped request object.- Specified by:
getLocale
in interfaceServletRequest
- Returns:
- the preferred
Locale
for the client
-
getLocales
The default behavior of this method is to return getLocales() on the wrapped request object.- Specified by:
getLocales
in interfaceServletRequest
- Returns:
- an
Enumeration
of preferredLocale
objects for the client
-
isSecure
public boolean isSecure()The default behavior of this method is to return isSecure() on the wrapped request object.- Specified by:
isSecure
in interfaceServletRequest
- Returns:
- a boolean indicating if the request was made using a secure channel
-
getRequestDispatcher
The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.- Specified by:
getRequestDispatcher
in interfaceServletRequest
- Parameters:
path
- aString
specifying the pathname to the resource. If it is relative, it must be relative against the current servlet.- Returns:
- a
RequestDispatcher
object that acts as a wrapper for the resource at the specified path, ornull
if the servlet container cannot return aRequestDispatcher
- See Also:
-
getRemotePort
public int getRemotePort()The default behavior of this method is to return getRemotePort() on the wrapped request object.- Specified by:
getRemotePort
in interfaceServletRequest
- Returns:
- an integer specifying the port number
- Since:
- Servlet 2.4
-
getLocalName
The default behavior of this method is to return getLocalName() on the wrapped request object.- Specified by:
getLocalName
in interfaceServletRequest
- Returns:
- a
String
containing the host name of the IP on which the request was received. - Since:
- Servlet 2.4
-
getLocalAddr
The default behavior of this method is to return getLocalAddr() on the wrapped request object.- Specified by:
getLocalAddr
in interfaceServletRequest
- Returns:
- a
String
containing an IP address. - Since:
- Servlet 2.4
-
getLocalPort
public int getLocalPort()The default behavior of this method is to return getLocalPort() on the wrapped request object.- Specified by:
getLocalPort
in interfaceServletRequest
- Returns:
- an integer specifying a port number
- Since:
- Servlet 2.4
-
getServletContext
Gets the servlet context to which the wrapped servlet request was last dispatched.- Specified by:
getServletContext
in interfaceServletRequest
- Returns:
- the servlet context to which the wrapped servlet request was last dispatched
- Since:
- Servlet 3.0
-
startAsync
The default behavior of this method is to invokeServletRequest.startAsync()
on the wrapped request object.- Specified by:
startAsync
in interfaceServletRequest
- Returns:
- the (re)initialized AsyncContext
- Throws:
IllegalStateException
- if the request is within the scope of a filter or servlet that does not support asynchronous operations (that is,isAsyncSupported()
returns false), or if this method is called again without any asynchronous dispatch (resulting from one of theAsyncContext.dispatch()
methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch, or if the response has already been closed- Since:
- Servlet 3.0
- See Also:
-
startAsync
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException The default behavior of this method is to invokeServletRequest.startAsync(ServletRequest, ServletResponse)
on the wrapped request object.- Specified by:
startAsync
in interfaceServletRequest
- Parameters:
servletRequest
- the ServletRequest used to initialize the AsyncContextservletResponse
- the ServletResponse used to initialize the AsyncContext- Returns:
- the (re)initialized AsyncContext
- Throws:
IllegalStateException
- if the request is within the scope of a filter or servlet that does not support asynchronous operations (that is,isAsyncSupported()
returns false), or if this method is called again without any asynchronous dispatch (resulting from one of theAsyncContext.dispatch()
methods), is called outside the scope of any such dispatch, or is called again within the scope of the same dispatch, or if the response has already been closed- Since:
- Servlet 3.0
- See Also:
-
isAsyncStarted
public boolean isAsyncStarted()Checks if the wrapped request has been put into asynchronous mode.- Specified by:
isAsyncStarted
in interfaceServletRequest
- Returns:
- true if this request has been put into asynchronous mode, false otherwise
- Since:
- Servlet 3.0
- See Also:
-
isAsyncSupported
public boolean isAsyncSupported()Checks if the wrapped request supports asynchronous operation.- Specified by:
isAsyncSupported
in interfaceServletRequest
- Returns:
- true if this request supports asynchronous operation, false otherwise
- Since:
- Servlet 3.0
- See Also:
-
getAsyncContext
Gets the AsyncContext that was created or reinitialized by the most recent invocation ofstartAsync()
orstartAsync(ServletRequest,ServletResponse)
on the wrapped request.- Specified by:
getAsyncContext
in interfaceServletRequest
- Returns:
- the AsyncContext that was created or reinitialized by the most recent invocation of
startAsync()
orstartAsync(ServletRequest,ServletResponse)
on the wrapped request - Throws:
IllegalStateException
- if this request has not been put into asynchronous mode, i.e., if neitherstartAsync()
norstartAsync(ServletRequest,ServletResponse)
has been called- Since:
- Servlet 3.0
- See Also:
-
isWrapperFor
Checks (recursively) if this ServletRequestWrapper wraps the givenServletRequest
instance.- Parameters:
wrapped
- the ServletRequest instance to search for- Returns:
- true if this ServletRequestWrapper wraps the given ServletRequest instance, false otherwise
- Since:
- Servlet 3.0
-
isWrapperFor
Checks (recursively) if this ServletRequestWrapper wraps aServletRequest
of the given class type.- Parameters:
wrappedType
- the ServletRequest class type to search for- Returns:
- true if this ServletRequestWrapper wraps a ServletRequest of the given class type, false otherwise
- Throws:
IllegalArgumentException
- if the given class does not implementServletRequest
- Since:
- Servlet 3.0
-
getDispatcherType
Gets the dispatcher type of the wrapped request.- Specified by:
getDispatcherType
in interfaceServletRequest
- Returns:
- the dispatcher type of the wrapped request
- Since:
- Servlet 3.0
- See Also:
-
getRequestId
Gets the request ID for the wrapped request.- Specified by:
getRequestId
in interfaceServletRequest
- Returns:
- the request ID for the wrapped request
- Since:
- Servlet 6.0
-
getProtocolRequestId
Gets the protocol defined request ID, if any, for the wrapped request.- Specified by:
getProtocolRequestId
in interfaceServletRequest
- Returns:
- the protocol defined request ID, if any, for the wrapped request
- Since:
- Servlet 6.0
-
getServletConnection
Gets the connection information for the wrapped request.- Specified by:
getServletConnection
in interfaceServletRequest
- Returns:
- the connection information for the wrapped request
- Since:
- Servlet 6.0
-