1

Need to access HTTP session for fetching as well storing some information.I am using Spring-MVC for my application and i have 2 options here.

  1. User Request/ Session in my Controller method and do my work
  2. Use RequestContextHolde to access Session information.

I am separating some calculation logic from Controller and want to access Session information in this new layer and for that i have 2 options

  1. Pass session or Request object to other method in other layer and perform my work.
  2. use RequestContextHolder.currentRequestAttributes() to access request/ session and perform my work.

I am not sure which is right way to go? with second approach, i can see that method calling will be more clean and i need not to pass request/ session each time.

1 Answer 1

2

I would approach this problem a bit differently. Let's assume you need only an attribute "attr1" from the session. So what you actually should do is to get this attribute in @Controller pass it to your calculation logic class (@Service), return the result of the calculation back to @Controller and store it in session. Thanks to that your calculation logic will not be independent from a web layer (HttpSession), hence reusable.

E.g.:

@Controller
public class AController {

    @Autowired
    private SomeCalculationService someCalculationService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public Account accounts(HttpSession httpSession) {      
        MyData myData = httpSession.getAttribute("attr1");
        SomeResult someResult = someCalculationService.calculate(myData);
        httpSession.setAttribute("attr2", someResult);
    }
}
1
  • +1 for suggestion, i was thinking on this approach also. Commented Oct 28, 2013 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.