wood burning stoves 2.0*
The moose likes Servlets and the fly likes ServletContextAttribute Listener doesnot works with Vector object change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextAttribute Listener doesnot works with Vector object change" Watch "ServletContextAttribute Listener doesnot works with Vector object change" New topic
Author

ServletContextAttribute Listener doesnot works with Vector object change

Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Hi all,
This one is interesting i am trying a simple chat application in struts just go through the following code:
I want to catch the event when any new user logs in, i add every user id in to vector list. i update the orignal vector with new element.
As you should be knowing ServletContextAttributeListener has attributeReplaced method which i think should get called but it's not getting called same thing when i tries with a single String object it is working fine(but not with vector value updated and that means rest of the code is fine i also added listner tag in web.xml properly dont worry about it.) following is the code for it with class

try
{
ServletContext ctx = getServlet().getServletContext();
if(ctx.getAttribute("ctx_userList") != null)
{
Vector userList = (Vector)ctx.getAttribute("ctx_userList");
//ctx.removeAttribute("ctx_userList");
userList.addElement(id);
ctx.setAttribute("ctx_userList",tempList);
}
else
{
Vector userlist1=new Vector();
userlist1.addElement(id);
ctx.setAttribute("ctx_userList",userlist1);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}


/----------------------------------------------------------------

package ftp;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.*;

public class MyListener implements ServletContextAttributeListener
{
public void attributeAdded(ServletContextAttributeEvent scab)
{
System.out.println("Attribute added");
}
public void attributeRemoved(ServletContextAttributeEvent scab)
{
System.out.println("Attribute removed");
}
public void attributeReplaced(ServletContextAttributeEvent scab)
{
System.out.println("Attribute replaced");
}
}

Please reply quickly
thanks in advance...

Manoj Amrutkar
[ September 02, 2006: Message edited by: Bear Bibeault ]
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245


Monoj,

May you should look at "tempList" . Not sure if this what you want to do after reading your application logic

if "tempList" is null , then setAttribute is equivalent to removeAttribute

HTH


Vikrant Pandit
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Sorry it should be like this i mean userList instead of tempList

try
{
ServletContext ctx = getServlet().getServletContext();
if(ctx.getAttribute("ctx_userList") != null)
{
Vector userList = (Vector)ctx.getAttribute("ctx_userList");
//ctx.removeAttribute("ctx_userList");
userList.addElement(id);
ctx.setAttribute("ctx_userList",userList);
}
else
{
Vector userlist1=new Vector();
userlist1.addElement(id);
ctx.setAttribute("ctx_userList",userlist1);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}


/----------------------------------------------------------------

package ftp;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.*;

public class MyListener implements ServletContextAttributeListener
{
public void attributeAdded(ServletContextAttributeEvent scab)
{
System.out.println("Attribute added");
}
public void attributeRemoved(ServletContextAttributeEvent scab)
{
System.out.println("Attribute removed");
}
public void attributeReplaced(ServletContextAttributeEvent scab)
{
System.out.println("Attribute replaced");
}
}

Please reply quickly
thanks in advance...

Manoj Amrutkar
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245
Hi Manoj,

Have you run your modified code and are still getting the same behaviour ?

If yes, tell us the servlet container you are using.
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
yes, please read my problem bit carefully what i am emphasizing on is i. i am not changing the actual object see it is same but the value of the object is changed. is that why that function addedAttributeListener not getting called...?

Thanks and Regards


Manoj Amrutkar
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245
I ran the code sample below with a ServletContextAttributeListener configured in Tomcat 5.5.27



It gave the output given below

added
replced
removed

Therefore the behaviour seems to be container specific . It might be comparing the old value object reference with the new value object reference . since they are same , it might not be generating the event.

HTH
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Manoj Amrutkar,

Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Thanks for the reply,
I am using weblogic 8.1 is that why the problem is coming or is there any other reason for it?
And if it is so? what could be the solution to it?

Thanks
Manoj Amrutkar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Working for me too with Tomcat. I can't imagine that Weblogic would have such a bug. You said that it works with Strings. Can you post the code you've used ?

(as Ben pointed out, please use UBB Codes)


[My Blog]
All roads lead to JavaRanch
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Following is the code as asked for..

//LoginAction.java



--------------------------------------------------------------------------
//web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>Struts Blank Application</display-name>


<listener>
<listener-class>
ftp.MyListener
</listener-class>
</listener>


<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

----------------------------------------------------------------------------

//Listener class

---------------------------------------------------------------------------

and LoginForm ....


Thanks
Manoj Amrutkar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Is "Attribute added" being output ?
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Sorry i did not get what do you mean?
But i will write the further code in attributeReplaced method when new user Logs-in to chat room. for that the function must get invoked but it is not, and that is the problem?
Please be more elaborative on questions?

Thanks
Manoj Amrutkar
Manoj Amrutkar
Ranch Hand

Joined: Apr 15, 2006
Posts: 47
Another geniune problem i am facing is, if i press f5 i mean refresh a page in the list of chatters one more time same userid gets added.
So problem is how to distinguish between new users action and refresh page action.

Thanks
Manoj Amrutkar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You said in your first post that attributeReplaced was not being called. I was wondering if attributeAdded was called, therefore "Attribute Added" being output to the log.
 
 
subject: ServletContextAttribute Listener doesnot works with Vector object change