File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes question from JWebplus1.4 about session.invalidate() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "question from JWebplus1.4 about session.invalidate()" Watch "question from JWebplus1.4 about session.invalidate()" New topic

question from JWebplus1.4 about session.invalidate()

air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
Consider the following doGet method code of a servlet:

public void doGet(HttpServletRequest req, HttpServletResponse res)
String command = req.getParameter("command");
HttpSession session = req.getSession();
//insert code here
If the command equals remove, you want to remove all the attributes from the session. Which of the following options will you use?
Select 1 correct option.
a session.unbind();
b session.invalidate();
c session.expunge();
d session.removeAll();
e Enumerate the attribute names and remove them one by one from the session using removeAttribute(name) method.

the answer is e.
but i doubt that why "b" is wrong??
in api, it descrip that "Invalidates this session then unbinds any objects bound to it.".
so i think the answer b is also right.
Mihai Lihatchi
Ranch Hand

Joined: Oct 28, 2005
Posts: 138

I am still a beginner in SCWCD preparation but from my work experience I can tell you that b is right. It just may not be appropriate for this task IMHO.
Remember legal does not make it the best choice.

Better, faster, lighter Java ... you mean Ruby right ?
SCEA5,SCBCD1.3,SCWCD5,SCJP1.4 - memories from my youth.
Mahendar Reddy
Ranch Hand

Joined: Sep 27, 2005
Posts: 73
Hi lulu...

he is asking to remove attributes buut not to invalidate the session
so 'b' is incorrect.
'e' will do that...

SCJP- 98%<br />SCWCD-92%
air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
to Mahendar Reddy:
when a session invalidate, Will the attribute in it be removed??
air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
unbound != removed ???
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
When you invalidate a session,its attributes also must be removed.When you call invalidate method on a session object,the attributeRemoved function in the HttpSessionAttributeListener should invoke.

correct me if i am wrong....

God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Ramasubbhu Allur Kuppusamy
Ranch Hand

Joined: Sep 16, 2005
Posts: 120
The question was just about removing all the attributes from the session.
It was not about going an extra mile and making the session itself non-existent.
What if the same code, later on, had some objective to add some newer attributes to the same session, somewhere down the line?
That would not be possible if you invalidate the session straightaway.
In my book, eventhough session.invalidate() also removes all the attributes associated with the session, the most appropriate choice would be e.
[ March 06, 2006: Message edited by: Ramasubbhu Allur Kuppusamy ]

Regards,<br />Ram.<br />SCJP 1.4
I agree. Here's the link:
subject: question from JWebplus1.4 about session.invalidate()
It's not a secret anymore!