File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpSessionListener" Watch "HttpSessionListener" New topic
Author

HttpSessionListener

Selva Prasad Rajendran
Ranch Hand

Joined: Sep 07, 2004
Posts: 38
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

Joined: Sep 07, 2004
Posts: 38
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

Joined: May 10, 2005
Posts: 87
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

Joined: Nov 15, 2004
Posts: 1026
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 ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSessionListener