I came across a code where the session object is obtained in two different ways (or rather wrote in two different ways).
using HttpServletRequest
someMethod(HttpServletRequest request){
HttpSession session = request.getSession();
//getAttribute from session
}
And using HttpSession
anotherMethod(HttpSession session){
//getAttribute from session
}
I went through this article and a question on SO. But i am still have some doubts.
Can someone help me understand what is the difference between these?
UPDATE
Both of these are methods in a spring controller and these are mapped to different ajax calls. I understand that there is a session associated with every request object but when you pass an HttpSession object where does it find the current session object(load all the attributes) or how is it obtained? When I call the method from javascript, I don’t pass anything at all.
There is no huge difference between these two, the second method may be used if called multiple times to eliminate one extra method call
request.getSession()by keeping session as somewhat like a local cache (with ignorable performance improvement unless called 100s of times).eg,.
If you use the first method, then all the times that method is called one extra
request.getSession()is called.