• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

doubt in HTTPSessionListener implementation?

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic