• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletContextListener

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am finding problems using the ServletContextListener. I am using Tomcat4.1.12
package com.listeners;
import javax.servlet.*;
public final class MyContextListener
implements ServletContextListener {
private ServletContext context = null;
public MyContextListener() {}
public void contextDestroyed(ServletContextEvent event)
{
System.out.println("The Simple Web App. Has Been Removed");
this.context = null;
}
public void contextInitialized(ServletContextEvent event)
{
this.context = event.getServletContext();
//Output a simple message to the server's console
System.out.println("The Simple Web App. Is Ready");
}
}
I have placed the MyContextListener.class file in D:\Tomcat 4.1\webapps\ROOT\WEB-INF\classes\com\listeners
Following are the changes made to the Deployment Descriptor (web.xml file located in D:\Tomcat 4.1\webapps\ROOT\WEB-INF)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<!-- Define application events listeners -->
<listener>
<listener-class>
com.listeners.MyContextListener
</listener-class>
</listener>
</web-app>
Now when I start the Serve, the screen is cluttered with a huge stack trace. There seems to be some problem with the <listener> tag. How can I make this code run?
Regards,
Kunal Jaggi
SCJP2
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post web.xml ? That might help identify the problem .
Thanks,
Bhusahn
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using the DTD declaration as follows:
"http://java.sun.com/dtd/web-app_2_3.dtd"
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complete web.xml file is given below :
<?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>
<!--
<listener><listener-class>MyConnectionManager</listener-class></listener>
-->
<servlet>
<servlet-name>
One
</servlet-name>
<servlet-class>
HolisticCounter
</servlet-class>
</servlet>

<servlet>
<servlet-name>
Two
</servlet-name>
<servlet-class>
HolisticCounter
</servlet-class>
</servlet>
</web-app>
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jaggi
What does the stack trace say?
Also, are you supplying the fully qualified class name in the <listener-class> tag of your deployment descriptor?
Mark.
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd doesn't have <listener-class> defined. You need to use the dtd that I mentioned in my previous reply.
 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sai,
How do I make use of the DTD you have ref.
Please bear with me as I am quite new to Servlet Programming.
Regards,
Kunal Jaggi
SCJP2
 
reply
    Bookmark Topic Watch Topic
  • New Topic