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

HttpSessionListener

 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one give a proper explanation for this question.
... wat will happen when a session is invalidated..

public class MyHttpSessionListener implements HttpSessionListener
{
public void sessionCreated (HttpSessionEvent e)
{
System.out.log ("Session Created");
}

public void sessionDestroyed (HttpSessionEvent e)
{
HttpSession session = e.getSession ();
String name = (String) session.getAttribute ("username");
}
}
Which of the following statements best describes the code?
� A) A compilation error will occur as the HttpSessionListener class is not properly implemented
� B) No errors or exceptions will occur as the code is written correctly
� C) An IllegalStateException will be thrown when the code is executed
� D) None of the above


The answer is given as c. and the explanation is given as
�The HttpSessionListener interface only defines the sessionCreated () and sessionDestroyed () method calls.

Hence, the code properly implements the interface correctly. So a compilation error should NOT occur.

However, at code execution, an IllegalStateException will be thrown when the session.getAttribute ("name") is called.

By the time the servlet container calls the sessionDestroyed () method, the session has already been invalidated. Therefore a call to getAttribute () will throw an IllegalStateException

but my doubt is e.getSession() will return the session tat is changed. so why cant it call the getAttribute ovet that session.


can any one help me
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all...

when a getSession of HttpSessionEvent is called in a HttpSessionListener when a session is invalidated will return the session that is changed but will that session be still a invalidated session or a valid session.
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet specification 2.4 (used in 310-081 exam) says:
sessionDestroyed
notification that a session is about to be invalidated

So, the correct answer is B. I think, you have a question from the previous exam.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Selva Prasad Rajendran

I created a HttpSessionListener and a servlet code to test this case.
here is the code

Servlet Code

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("username","vishnu");
System.out.println("Session Attribute has been set");

session.invalidate();

System.out.println("Session invalidate");
}

HttpSessionListener Code

public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("Session Created");

}

public void sessionDestroyed(HttpSessionEvent e) {
HttpSession session = e.getSession ();
String name = (String) session.getAttribute ("username");
System.out.println("From HttpSessionListener: Attribute value ==> " + name );
}

output

Session Created
Session Attribute has been set
From HttpSessionListener: Attribute value ==> vishnu
Session invalidate


Selva wrote:
when a getSession of HttpSessionEvent is called in a HttpSessionListener when a session is invalidated will return the session that is changed but will that session be still a invalidated session or a valid session.


It is a valid session. The above output proves it. This method is executed before the session gets invalidated.

Read the following about sessionDestroyed method of HttpSessionListener interface

public void sessionDestroyed(HttpSessionEvent se)
Notification that a session is about to be invalidated.


As Sergey Tyulkin has said Option B is correct

Hope this is useful.



[ September 16, 2005: Message edited by: vishnu prakash ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic