Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextListener

 
Jaggi Kunal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
Bhushan Jawle
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post web.xml ? That might help identify the problem .
Thanks,
Bhusahn
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • 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
  • 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>
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic