简体   繁体   中英

Session in java - Any best practises?

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.

someMethod(HttpServletRequest request)

In this you are passing the current request object, from which you can obtain your current session and then you can get attributes from it.. You can get the current session object from your request object by using : -

request.getSession(false)

*NOTE: - We pass false as a parameter to getSession(false) to get any existing session.. If no session exist it will return null .. whereas, request.getSession() will always create a new session, so you won't get any prevoius attribute store in other session..

anotherMethod(HttpSession session)

Here you are passing the session object itself from somewhere.. Might be because, your session object contains many attributes, and you don't want to many parameters in the method..

But you should do all this session related task in your Servlet and pass the attribute to the methods of other class..

You don't need to float session objects if you have single attribute. Just simply access it using session object.

HttpSession session = request.getSession(true);
session.getAttribute(name);

Now only sensible case where you can float session objects is you have large number of attributes and you want each method to access its own set of attributes. In any case the method depends on session passed to it so it should not care how it was obtained.

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,.

HttpSession session=request.getSession();
getFirstAttr(session);
getSecondAttr(session);
....
getHundredthAttr(session);

If you use the first method, then all the times that method is called one extra request.getSession() is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM