• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Failed to register listener in web.xml

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Tiger class implemented HttpSessionAttributeListener, then I register it in the web.xml.

As soon as I put the following code in the web.xml, I get
"SEVER: Context [/myWeb] startup failed due to previous errors" from TomCat console

--- web.xml --
<listener>
<listener-class>coreservlets.listeners.Tiger</listener-class>
</listener>
--

If I remove above code in web.xml, I don't see error message, so i am sure it's above code causing error, but I don't know what's wrong. Below is my Tiger class.

Can anybody help me?

Thanks in advance!!!

-- ---- ---
package coreservlets.listeners;
import javax.servlet.http.*;
public class Tiger implements HttpSessionAttributeListener
{
private String breed;
public Tiger(String breed) { this.breed = breed; }
public String getBreed() {return breed; }

public void attributeAdded (HttpSessionBindingEvent se )
{
System.out.println("a session attribute has been added.");
System.out.println(se.getSession().getId());
System.out.println(se.getName() + "', '" + se.getValue());
}
public void attributeRemoved(HttpSessionBindingEvent se )
{
System.out.println("a session attribute has been removed.");
}
public void attributeReplaced(HttpSessionBindingEvent se )
{
System.out.println("a session attribute has been replaced.");
}
}
--------------------


-- TigerAttributeListenerTester servlet--
package coreservlets.listeners;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TigerAttributeListenerTester extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Test HttpSessionAttributeListener <br>");
HttpSession session = request.getSession(true);
Tiger tiger = new Tiger("ohhh tiger.");
session.setAttribute("tigerTiger", tiger );
}
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Tiger class has no default constructor (the one without paramaters), so the container will fail to make a new instance.

By looking at your code, I'm wondering if you wanted to use HttpSessionBindingListener instead.
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou !!! Your help is much appreciated !!

You're right, it needs a non-arg constructor.


Tiffiny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic