Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from JWebplus1.4 about session.invalidate()

 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following doGet method code of a servlet:

public void doGet(HttpServletRequest req, HttpServletResponse res)
{
String command = req.getParameter("command");
if("remove".equals(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
Posts: 138
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lulu...

he is asking to remove attributes buut not to invalidate the session
so 'b' is incorrect.
'e' will do that...
 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Mahendar Reddy:
when a session invalidate, Will the attribute in it be removed??
 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unbound != removed ???
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Ramasubbhu Allur Kuppusamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic