Top Description Fields Constructors Methods
org.springframework.web.context.request

public Class ServletRequestAttributes

extends AbstractRequestAttributes
Class Inheritance
Known Direct Subclasses
org.springframework.web.context.request.ServletWebRequest
Imports
java.util.HashSet, .Map, .Set, java.util.concurrent.ConcurrentHashMap, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, .HttpSession, org.springframework.lang.Nullable, org.springframework.util.Assert, .NumberUtils, .StringUtils, org.springframework.web.util.WebUtils

Servlet-based implementation of the RequestAttributes interface.

Accesses objects from servlet request and HTTP session scope, with no distinction between "session" and "global session".

Author
Juergen Hoeller
Since
2.0
See Also
jakarta.servlet.ServletRequest#getAttribute, jakarta.servlet.http.HttpSession#getAttribute

Field Summary

Modifier and TypeField and Description
public static final String
DESTRUCTION_CALLBACK_NAME_PREFIX

Constant identifying the String prefixed to the name of a destruction callback when it is stored in a HttpSession.

protected static final Set<Class<?>>
private final HttpServletRequest
private HttpServletResponse
private volatile HttpSession
private final Map<String, Object>
Inherited from org.springframework.web.context.request.AbstractRequestAttributes:
requestDestructionCallbacks

Constructor Summary

AccessConstructor and Description
public
ServletRequestAttributes(HttpServletRequest
current HTTP request
request
)

Create a new ServletRequestAttributes instance for the given request.

public
ServletRequestAttributes(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response (for optional exposure)
response
)

Create a new ServletRequestAttributes instance for the given request.

Method Summary

Modifier and TypeMethod and Description
public Object
getAttribute(String
the name of the attribute
name
,
int
the scope identifier
scope
)

Implements org.springframework.web.context.request.RequestAttributes.getAttribute.

Return the value for the scoped attribute of the given name, if any.

public String[]
getAttributeNames(int
the scope identifier
scope
)

Implements org.springframework.web.context.request.RequestAttributes.getAttributeNames.

Retrieve the names of all attributes in the scope.

public final HttpServletRequest
getRequest()

Exposes the native HttpServletRequest that we're wrapping.

public final HttpServletResponse
getResponse()

Exposes the native HttpServletResponse that we're wrapping (if any).

protected final HttpSession
getSession(boolean
whether to allow creation of a new session if none exists yet
allowCreate
)

Exposes the HttpSession that we're wrapping.

public String
getSessionId()

Implements org.springframework.web.context.request.RequestAttributes.getSessionId.

Return an id for the current underlying session.

public Object
getSessionMutex()

Implements org.springframework.web.context.request.RequestAttributes.getSessionMutex.

Expose the best available mutex for the underlying session: that is, an object to synchronize on for the underlying session.

protected boolean

Returns:

true if the value is to be considered as immutable for the purposes of session attribute management; false otherwise
isImmutableSessionAttribute
(String
the name of the attribute
name
,
Object
the corresponding value to check
value
)

Determine whether the given value is to be considered as an immutable session attribute, that is, doesn't have to be re-set via session.setAttribute since its value cannot meaningfully change internally.

private HttpSession
public void
registerDestructionCallback(String
the name of the attribute to register the callback for
name
,
Runnable
the destruction callback to be executed
callback
,
int
the scope identifier
scope
)

Implements org.springframework.web.context.request.RequestAttributes.registerDestructionCallback.

Register a callback to be executed on destruction of the specified attribute in the given scope.

protected void
registerSessionDestructionCallback(String
the name of the attribute to register the callback for
name
,
Runnable
the callback to be executed for destruction
callback
)

Register the given callback as to be executed after session termination.

public void
removeAttribute(String
the name of the attribute
name
,
int
the scope identifier
scope
)

Implements org.springframework.web.context.request.RequestAttributes.removeAttribute.

Remove the scoped attribute of the given name, if it exists.

public Object
resolveReference(String
the contextual key
key
)

Implements org.springframework.web.context.request.RequestAttributes.resolveReference.

Resolve the contextual reference for the given key, if any.

public void
setAttribute(String
the name of the attribute
name
,
Object
the value for the attribute
value
,
int
the scope identifier
scope
)

Implements org.springframework.web.context.request.RequestAttributes.setAttribute.

Set the value for the scoped attribute of the given name, replacing an existing value (if any).

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

protected void
updateAccessedSessionAttributes()

Implements abstract org.springframework.web.context.request.AbstractRequestAttributes.updateAccessedSessionAttributes.

Update all accessed session attributes through session.setAttribute calls, explicitly indicating to the container that they might have been modified.

Inherited from org.springframework.web.context.request.AbstractRequestAttributes:
isRequestActiveregisterRequestDestructionCallbackremoveRequestDestructionCallbackrequestCompleted

Field Detail

DESTRUCTION_CALLBACK_NAME_PREFIXback to summary
public static final String DESTRUCTION_CALLBACK_NAME_PREFIX

Constant identifying the String prefixed to the name of a destruction callback when it is stored in a HttpSession.

immutableValueTypesback to summary
protected static final Set<Class<?>> immutableValueTypes
requestback to summary
private final HttpServletRequest request
responseback to summary
private HttpServletResponse response
Annotations
@Nullable
sessionback to summary
private volatile HttpSession session
Annotations
@Nullable
sessionAttributesToUpdateback to summary
private final Map<String, Object> sessionAttributesToUpdate

Constructor Detail

ServletRequestAttributesback to summary
public ServletRequestAttributes(HttpServletRequest request)

Create a new ServletRequestAttributes instance for the given request.

Parameters
request:HttpServletRequest

current HTTP request

ServletRequestAttributesback to summary
public ServletRequestAttributes(HttpServletRequest request, HttpServletResponse response)

Create a new ServletRequestAttributes instance for the given request.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response (for optional exposure)

Method Detail

getAttributeback to summary
public Object getAttribute(String name, int scope)

Implements org.springframework.web.context.request.RequestAttributes.getAttribute.

Doc from org.springframework.web.context.request.RequestAttributes.getAttribute.

Return the value for the scoped attribute of the given name, if any.

Parameters
name:String

the name of the attribute

scope:int

the scope identifier

Returns:Object

the current attribute value, or null if not found

Annotations
@Override
getAttributeNamesback to summary
public String[] getAttributeNames(int scope)

Implements org.springframework.web.context.request.RequestAttributes.getAttributeNames.

Doc from org.springframework.web.context.request.RequestAttributes.getAttributeNames.

Retrieve the names of all attributes in the scope.

Parameters
scope:int

the scope identifier

Returns:String[]

the attribute names as String array

Annotations
@Override
getRequestback to summary
public final HttpServletRequest getRequest()

Exposes the native HttpServletRequest that we're wrapping.

getResponseback to summary
public final HttpServletResponse getResponse()

Exposes the native HttpServletResponse that we're wrapping (if any).

Annotations
@Nullable
getSessionback to summary
protected final HttpSession getSession(boolean allowCreate)

Exposes the HttpSession that we're wrapping.

Parameters
allowCreate:boolean

whether to allow creation of a new session if none exists yet

Annotations
@Nullable
getSessionIdback to summary
public String getSessionId()

Implements org.springframework.web.context.request.RequestAttributes.getSessionId.

Doc from org.springframework.web.context.request.RequestAttributes.getSessionId.

Return an id for the current underlying session.

Returns:String

the session id as String (never null)

Annotations
@Override
getSessionMutexback to summary
public Object getSessionMutex()

Implements org.springframework.web.context.request.RequestAttributes.getSessionMutex.

Doc from org.springframework.web.context.request.RequestAttributes.getSessionMutex.

Expose the best available mutex for the underlying session: that is, an object to synchronize on for the underlying session.

Returns:Object

the session mutex to use (never null)

Annotations
@Override
isImmutableSessionAttributeback to summary
protected boolean isImmutableSessionAttribute(String name, Object value)

Determine whether the given value is to be considered as an immutable session attribute, that is, doesn't have to be re-set via session.setAttribute since its value cannot meaningfully change internally.

The default implementation returns true for String, Character, Boolean and standard Number values.

Parameters
name:String

the name of the attribute

value:Object

the corresponding value to check

Returns:boolean

true if the value is to be considered as immutable for the purposes of session attribute management; false otherwise

See Also
updateAccessedSessionAttributes()
obtainSessionback to summary
private HttpSession obtainSession()
registerDestructionCallbackback to summary
public void registerDestructionCallback(String name, Runnable callback, int scope)

Implements org.springframework.web.context.request.RequestAttributes.registerDestructionCallback.

Doc from org.springframework.web.context.request.RequestAttributes.registerDestructionCallback.

Register a callback to be executed on destruction of the specified attribute in the given scope.

Implementations should do their best to execute the callback at the appropriate time: that is, at request completion or session termination, respectively. If such a callback is not supported by the underlying runtime environment, the callback must be ignored and a corresponding warning should be logged.

Note that 'destruction' usually corresponds to destruction of the entire scope, not to the individual attribute having been explicitly removed by the application. If an attribute gets removed via this facade's removeAttribute(String, int) method, any registered destruction callback should be disabled as well, assuming that the removed object will be reused or manually destroyed.

Note

Callback objects should generally be serializable if they are being registered for a session scope. Otherwise the callback (or even the entire session) might not survive web app restarts.

Parameters
name:String

the name of the attribute to register the callback for

callback:Runnable

the destruction callback to be executed

scope:int

the scope identifier

Annotations
@Override
registerSessionDestructionCallbackback to summary
protected void registerSessionDestructionCallback(String name, Runnable callback)

Register the given callback as to be executed after session termination.

Note

The callback object should be serializable in order to survive web app restarts.

Parameters
name:String

the name of the attribute to register the callback for

callback:Runnable

the callback to be executed for destruction

removeAttributeback to summary
public void removeAttribute(String name, int scope)

Implements org.springframework.web.context.request.RequestAttributes.removeAttribute.

Doc from org.springframework.web.context.request.RequestAttributes.removeAttribute.

Remove the scoped attribute of the given name, if it exists.

Note that an implementation should also remove a registered destruction callback for the specified attribute, if any. It does, however, not need to execute a registered destruction callback in this case, since the object will be destroyed by the caller (if appropriate).

Parameters
name:String

the name of the attribute

scope:int

the scope identifier

Annotations
@Override
resolveReferenceback to summary
public Object resolveReference(String key)

Implements org.springframework.web.context.request.RequestAttributes.resolveReference.

Doc from org.springframework.web.context.request.RequestAttributes.resolveReference.

Resolve the contextual reference for the given key, if any.

At a minimum: the HttpServletRequest reference for key "request", and the HttpSession reference for key "session".

Parameters
key:String

the contextual key

Returns:Object

the corresponding object, or null if none found

Annotations
@Override
setAttributeback to summary
public void setAttribute(String name, Object value, int scope)

Implements org.springframework.web.context.request.RequestAttributes.setAttribute.

Doc from org.springframework.web.context.request.RequestAttributes.setAttribute.

Set the value for the scoped attribute of the given name, replacing an existing value (if any).

Parameters
name:String

the name of the attribute

value:Object

the value for the attribute

scope:int

the scope identifier

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
updateAccessedSessionAttributesback to summary
protected void updateAccessedSessionAttributes()

Implements abstract org.springframework.web.context.request.AbstractRequestAttributes.updateAccessedSessionAttributes.

Update all accessed session attributes through session.setAttribute calls, explicitly indicating to the container that they might have been modified.

Annotations
@Override