Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextAttribute Listener doesnot works with Vector object change

 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "Attribute added" being output ?
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic