File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Is request.getSession() the only way to get session? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is request.getSession() the only way to get session?" Watch "Is request.getSession() the only way to get session?" New topic

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

Agarwal Priyanka

Joined: Jul 25, 2008
Posts: 20

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.

keep trying...thats the way to success...<br />with best regards...<br />p.agarwal
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

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.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Why would you want to get it any other way than from the request?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Agarwal Priyanka

Joined: Jul 25, 2008
Posts: 20
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

Joined: Jan 10, 2002
Posts: 60782

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
R Sriram

Joined: Jun 11, 2008
Posts: 18
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

Joined: Dec 11, 2004
Posts: 13410

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.
I agree. Here's the link:
subject: Is request.getSession() the only way to get session?
Similar Threads
How to invalidate user's session forcefully
servletcontext object
Accessing ServletContext attribute from Action Class.
Null exception retrieving session attribute
PageContext.getSession vs req.getSession vs Session in JSP