Spring MVC - Session Differences

Are there any differences between getting a session using HttpServletRequest.getSession()

and HttpSession

injected into a controller method?

+3


source to share


1 answer


Basically there is no difference between an object session

injected into a Spring MVC controller:

@RequestMapping(value = "/somepath", method = RequestMethod.POST)
@ResponseBody
public JsonResponse someMethod (HttpSession session)
{
 // play with session attributes
}

      

And an object session

derived from 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.
}

      

In the old style, you are given the option to get the context object HttpSession

by injecting it as a controller argument so that Spring takes care of all the dirty things for you.

+4


source







All Articles