• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question from JWebplus1.4 about session.invalidate()

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 138
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unbound != removed ???
 
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic