• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem registering ServletContextListener

 
raghu dubey
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to run the HFSJ example on pg 170...for the ContextListener topic. I get an nullpointer when I use the URL. Here are the lines I found in tomcat logs:

SEVERE: Error configuring application listener of class com.example.MyServletContextListener
java.lang.ClassNotFoundException: com.example.MyServletContextListener
at org.apache.catalina.loader.WebappClassLoader.loadClass

SEVERE: Servlet.service() for servlet ListenerTester threw exception
java.lang.NullPointerException
at com.example.ListenerTester.doGet(ListenerTester.java:16)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
==========
I checked the listener name in DD, but is is fine. All the classes alos compile good. Can you please help on whats goingt wrong...using tomcat 5.5.25.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post MyServletContextListener.java, and also the way you registered it in web.xml.
 
raghu dubey
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

Here is the Web.xml:

=======
<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.ListenerTester</servlet-class>

<init-param>
<param-name>adminEmail</param-name>
<param-value>meraghudy@gmail.com</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>

<listener>
<listener-class>
com.example.MyServletContextListener
</listener-class>

</listener>
=====================

package com.example;

import javax.servlet.*;

public class MyServletContextListener implements ServletContextListener{

public void contextInitialized(ServletContextEvent sce){

ServletContext sc = sce.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
System.out.println(d);
sc.setAttribute("dog",d);

}

public void contextDestroyed(ServletContextEvent sce){

}

}
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you put MyServletContextListener.class ? Under WEB-INF/classes/com/example ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic