aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt in HTTPSessionListener implementation? 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 "doubt in HTTPSessionListener implementation?" Watch "doubt in HTTPSessionListener implementation?" New topic
Author

doubt in HTTPSessionListener implementation?

yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
in this below script I implemeted HttpSessionListener and executed.

-------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestCustAttrListener extends HttpServlet implements HttpSessionListener
{
String var = "rome";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
out.println("hello"+var);
out.close();
}

public void sessionCreated(HttpSessionEvent e)
{
var = "italy";
System.out.println("test - session created");
}

public void sessionDestroyed(HttpSessionEvent e)
{
System.out.println("test - session destroyed");
}
}
---------------
in DD I kept as

<listener>
<description>raja session listener</description>
<listener-class>TestCustAttrListener</listener-class>
</listener>

<servlet>
<servlet-name>TestCustAttrListener</servlet-name>
<servlet-class>TestCustAttrListener</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestCustAttrListener</servlet-name>
<url-pattern>/cservlet/TestCustAttrListener</url-pattern>
</servlet-mapping>
-----
(1) output on the explorer is "hello rome" but according to my expectations it should be "hello italy".
(2) on the tomcat debug window it printed "test - session created"
(3)when I killed the Internet explorer window then sessionDestroyed() has to get execute and tomcat window it has to print "test - session destroyed". but it is not printing like this.? why?
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
If you read the javadocs you will notice that getSession() doesn't create one if it doesn't exist. getSession(true) will. Therefore, your logic is incorrect and what you see truly is what you should be seeing.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
V Sharma
Greenhorn

Joined: Jun 26, 2004
Posts: 26
Hi,

This is what javadocs says and Servlet Spec says this is equivalent to getSession(true),

public HttpSession getSession()

Returns the current session associated with this request, or if the request does not have a session, creates one.So I think author is write.

Yamini,

I tried with your code with little changes and it proves few things :

1) Container creates 2 different instances 1 act as listener and 1 act as servlet.

Run this and check your log (stdout.txt) things will become clear.

package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestCustAttrListener extends HttpServlet implements HttpSessionListener{
String var = "rome";

public TestCustAttrListener(){
System.out.println("Bloody Instance"+var);
}


public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
out.println("hello"+var);
out.close();
}

public void sessionCreated(HttpSessionEvent e)
{
var = "italy";
System.out.println("test - session created"+var);
}

public void sessionDestroyed(HttpSessionEvent e)
{
System.out.println("test - session destroyed");
}

}


===It you want italy make field static.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in HTTPSessionListener implementation?