This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is request.getSession() the only way to get session?

 
Agarwal Priyanka
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

We call request.getSession() to get the session, but is that the only way?
Can ServletContext do the same? Please explain i am with ServletContext.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best place to get answers to this sort of question is the API JavaDocs.

The short answer is no, you can't get the session from the ServletContext. It doesn't make much sense to try either. The ServletContext is an object that encapsulates the environemnt the servlet is deployed in.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to get it any other way than from the request?
 
Agarwal Priyanka
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello sir Ben,

Actually i read on this topic in "The Head First Servlet & JSP", which really confused me and a big question was biting me," is that the only way to get session? Can't we get it from ServletContext?" thats all...And i could not find any proper answer. There is a line written in the book,-But there is another way that can get a session...from a session event object.I am unable to follow this topic in the book, so i needed help to clearify my confusion.
Was my question stupid?then i am really sorry...but i need to be cleared.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64186
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Agarwal Priyanka:
Was my question stupid?

No. Ben asked the question in order to find out what you were really trying to do so that we could help you get it done.

Sometimes questions that get asked that are too specific, and finding out what they are really trying to accomplish is part of helping.

A very common example is the question "How do I disable the back button?". It turns out that the real problem is that they need to turn caching off, not disable anything.
 
R Sriram
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably reading the book a little more deeper would give you some idea. As far as I have gone through, they have mentioned about getting the session from the Servlet Context. If possible go through the complete book. Happy reading!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get to the section that covers event listeners the concept of event objects should become more clear.

There was, in the past, an object called SessionContext that held references to all of the sessions. It allowed you to fetch one by ID. For security reasons, it's been deprecated. These days, with listeners, it's easy to duplicate this functionality yourself if you need it.

As Bear mentioned, your question was not stupid.
Sometimes knowing why someone is asking a question can lead to a better answer than the question itself.

If something doesn't make sense, don't ever be afraid to ask here.
-Ben
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic