• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why method sessionCreated() is not invoked?

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a listener class implements HttpSessionListener and a servelt class to test it . The listener class is register in DD with no problem.

In my servlet, if I set session attribute with a simple Cat object, then I don't see "session is created." gets invoked.
----------------------------------
HttpSession session = request.getSession(true);
Cat c = new Cat("moooo.");
session.setAttribute("cat", c );
out.println("<br>Cat's breed is: " + c.getBreed());
-------------------------------------

However, if I set session attribute with a string value like below, then message "session is created." is invoked. Why???
-------------------------------------
HttpSession session = request.getSession(true);
session.setAttribute("flower", "nice");
out.println(" " + session.getAttribute("flower") );
System.out.println( " " + session.getAttribute("flower") );
----



-- here's my MySessionListener class-----
package coreservlets.listeners;
import javax.servlet.http.*;
public class MySessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent se)
{
System.out.println("session is created.");
}
public void sessionDestroyed(HttpSessionEvent se)
{
System.out.println("session is Destroyed.");
}
}

----- Cat.java
public class Cat
{
private String breed;
public Cat(String breed) { this.breed = breed;}
public String getBreed(){ return breed; }
}
----

Please help me!!!
Thanks in advance!!!
 
Clifton Eaton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you supply more details about how you tested?

1. Are these two separate servlets that you call in the order you specified? 2. what is the timeout for the session set to?
3. do you ever wait to see if sessionDestroyed() is called?
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiffiny,

If you want to add attributes in HttpSession then you should use HttpSessionAttributeListener. You are implementing wrong listener type. The methods sessionCreated() and sessionDestroyed() from HttpSessionListener would get called when session gets created and destroyed resp. So it's not right choice to use HttpSessionListener for your requirement.

An object that implements the HttpSessionAttributeListener interface is notified when a session attribute is added, removed or replaced. So you need to implement following methods from

The following are the methods which you need to implement from HttpSessionAttributeListener in you class.


void attributeAdded(HttpSessionBindingEvente e)
vod attributeRemoved(HttpSessionBindingEvente e)
void attributeReplaced(HttpSessionBindingEvente e)


Thanks,
Mahesh

-------
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, (SCEA Part I preparing......)
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to how the HttpSessionListener works.
I re-test it again, I found out, no msg is print out from MySessionListener.java.

Here's how I invoke the servlet.
http://localhost:8080/myWeb/servlet/coreservlets.listeners.TestListener

Her's my servlet
-- TestListener.java--
package coreservlets.listeners;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;

public class TestListener extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<font color=green> Testing .</font><br>");

HttpSession session = request.getSession(true);
session.setAttribute("flower", "nice");
out.println(" " + session.getAttribute("flower") );
}
}
--------------------

Thanks again
Tiffiny
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes the server cache the previous session created if not detroyed. In that case sessionCreated() will be called for the first time. For subsequent call it won't be invoked.

So every time you test the code, use a new browser. Or destroy the session calling session.invalidate() before leaving the servlet.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any particular reason why you chose to use request.getsession(true) instead of just request.getSession()?
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for replying my question.

Tridib is right, I have to close and re-open browser, then I will be able to see the message "... session is created.".


Tiffiny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic