This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in HTTPSessionListener implementation?

 
yamini nadella
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • 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?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • 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.
 
V Sharma
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic