0

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.

Community
  • 1
  • 1
shazinltc
  • 3,616
  • 7
  • 34
  • 49

3 Answers3

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

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • The second method is from a controller in a spring application for an ajax call. So how does it get the session object. – shazinltc Sep 27 '12 at 06:34
  • @shazinltc.. In that case, you don't want to pass your `session` object.. rather pass `request` object.. I don't have much idea Ajax.. So, can't understand the flow of your code.. – Rohit Jain Sep 27 '12 at 06:38
  • @shazinltc.. In your controller I think you would have your `request` object.. You can get `session` object from that.. – Rohit Jain Sep 27 '12 at 06:39
  • Thanx Rohit, but as I have shown the session object passed as a method parameter directly. How does that work? – shazinltc Sep 27 '12 at 06:45
  • Can't understand what do you mean by "How does that work?". Do you want to ask how you pass a session object in a method?? – Rohit Jain Sep 27 '12 at 06:50
  • I know a session object is associated with an HttpRequest. So we can obtain the session from the request object. But how are the session attributes loaded to the HttpSession object by simply passing it as a parameter in the method. – shazinltc Sep 27 '12 at 06:58
  • @shazinltc.. Why is this troubling you?? That works just like you pass normal variable as a parameter.. You are passing your session object which contains your attributes as parameter.. So, you will have al those attributes in your parameter also.. – Rohit Jain Sep 27 '12 at 07:00
  • The point is when i make the call from javascript, I dont pass anything at all. So how come the object has all the attributes. Btw thanks for your patience :) – shazinltc Sep 27 '12 at 07:05
  • @shazinltc.. What have you tried?? Did you get any error.. Please post your Javascript, and the place where your method is.. – Rohit Jain Sep 27 '12 at 07:50
0

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.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
0

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.

prajeesh kumar
  • 1,916
  • 1
  • 17
  • 17
  • The session object is obtained and bound to the request object by your servlet container (tomcat or jboss or whatever) by using the session id parameter (sent by the browser) when (and like) the request object is created for every request. The sessions are stored in the server by session id, like a Map by the servlet container. – prajeesh kumar Sep 27 '12 at 06:48
  • So basically in every method call (request) i can obtain the session either by loading the request object or by loading the session object directly. Is that right? – shazinltc Sep 27 '12 at 07:13
  • You will get the session object only from the request object, by the `request.getSession()` method. The request object will be mostly passed to you by the servlet container or your framework. – prajeesh kumar Sep 27 '12 at 07:36
  • What I meant was even when if I pass an HttpSession object as a parameter in my method, as in the second example, Tomcat will bind the session to that. I mean I can directly use that as the my session object. Isn't that rt? – shazinltc Sep 27 '12 at 14:23
  • No, tomcat doesn't pass the session object directly, it will create a request object,set the session in it, and pass the request object to your (or framework's) servlet's methods (doPost,doGet etc..) – prajeesh kumar Sep 27 '12 at 17:00