I have been working with Struts 2 for a long time.
In case of implementing SessionAware
interface to our action class we will get SessionMap
but not HttpSession
object.
In case of ServletRequestAware
and ServletResposeAware
we get HttpServletRequest
and HttpServletResponse
object but not wrapper objects like SessionMap
in case of SessionAware
.
My question is, if Struts is giving us SessionMap
instead of HttpSession
to decouple our action classes from Servlet API and Http protocol,then why it is giving us HttpServletRequest
and HttpServletResponse
objects in case ServletRequestAware
and ServletResponseAware
.
If Struts doesn't want to decouple Servlet API and HTTP protocol from the action classes then why it is giving us SessionMap
in case of SessionAware
interface.
Why we don't get HttpSession
object?
In case of ServlectRequestAware and ServletResposeAware we get HttpServletRequest and HttpServletRespose object but not wrapper objects like SessionMap in case of SessionAware.
Because those directly expose the servlet request and response on rare occasions where they're actually necessary (or at least useful).
My question is, if struts is giving us SessionMap instead of HttpSession to decouple our action classes from Servlet API and Http protocol,then why it is giving us HttpServletRequest and HttpServletRespose objects in case ServlectRequestAware and ServletResposeAware.
Because it's much less likely you'd specifically need an HttpSession
than the actual request or response.
If struts don't want to decouple Servlet API and HTTP protocol from the action classes then why it is giving us SessionMap in case of SessionAware interface.
It does want to decouple the Servlet API, for good reasons. It forces you to explicitly ask for Servlet API artifacts because they're a code smell. It doesn't prevent you from getting them because on rare occasions they're important.
An HttpSession
is pretty much just an attribute map, it doesn't contain information generally useful in an action. On the even-rarer occasions you need one you can still get it.
Why we don't get HttpSession
object?
You can get this object from the servlet's HTTP request. No reason to define additional interface to inject the HttpSession
into the action. On the other hand Struts defines maps for HTTP request, session, application for easier access/modify its attributes using a Map
interface. The servletConfig
interceptor can inject these objects to the action if it implements corresponding xxxAware
interface.
The list of interfaces that could be injected by this interceptor:
ServletContextAware
ServletRequestAware
ServletResponseAware
ParameterAware
RequestAware
SessionAware
ApplicationAware
PrincipalAware