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.

5
  • Ideally neither one is a good way to work with session.. Why you are passing your session as a parameter to a method? Commented Sep 27, 2012 at 6:21
  • You question is not clear. You can get reference to HTTPsession object from HTTPServletRequest Object.You can then pass this object to any method. Wats the point here? Commented Sep 27, 2012 at 6:22
  • @rohit: that is my question :) Commented Sep 27, 2012 at 6:30
  • @metalhead: My question is what is the difference in the way it works? Commented Sep 27, 2012 at 6:31
  • @shazinltc.. As such there in not much difference in the way they work.. See my post below.. Commented Sep 27, 2012 at 6:33

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

9 Comments

The second method is from a controller in a spring application for an ajax call. So how does it get the session object.
@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..
@shazinltc.. In your controller I think you would have your request object.. You can get session object from that..
Thanx Rohit, but as I have shown the session object passed as a method parameter directly. How does that work?
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??
|
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.

5 Comments

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<String,HttpSession> by the servlet container.
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?
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.
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?
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..)
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.

1 Comment

Thanks, But that doesn't answer my question. I have updated my question :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.