Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Failed to register listener in web.xml

 
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 );
}
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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
    Bookmark Topic Watch Topic
  • New Topic