Are there any differences between getting session through HttpServletRequest.getSession()
and HttpSession
injected in controller's method?
Asked
Active
Viewed 841 times
3

Arnab Nandy
- 6,472
- 5
- 44
- 50

sidlejinks
- 709
- 1
- 9
- 25
-
i believe both are same :) – Ramesh Kotha Sep 16 '14 at 16:42
1 Answers
7
Basically there is no diffrerence between the session
object injected into a Spring MVC controller:
@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpSession session)
{
// play with session attributes
}
And the session
object retrieved from the HttpServletRequest
:
@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpServletRequest request)
{
Session session = request.getSession();
// You are playin with the same session attributes.
}
The former style just provide you with a facility to get the contextual HttpSession
object by injecting it as a controller argument so that Spring takes care of the all the dirty stuff for you.

tmarwen
- 15,750
- 5
- 43
- 62