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?
Because those directly expose the servlet request and response on rare occasions where they're actually necessary (or at least useful).
Because it's much less likely you'd specifically need an
HttpSession
than the actual request or response.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.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 aMap
interface. TheservletConfig
interceptor can inject these objects to the action if it implements correspondingxxxAware
interface.The list of interfaces that could be injected by this interceptor:
ServletContextAware
ServletRequestAware
ServletResponseAware
ParameterAware
RequestAware
SessionAware
ApplicationAware
PrincipalAware