• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to get it any other way than from the request?
 
Agarwal Priyanka
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic