The error message indicates that I'm not overriding the abstract class and/or method appropriately. Interestingly, I'm following code that's in a book; and it's supposed to be a compilable example (Head First Java Servlets and JSP)
Command/Error:
javac -classpath /usr/local/Cellar/tomcat/8.5.13/libexec/lib/servlet-
api.jar:classes:. -d classes
src/com/example/MyServletContextListener.java
src/com/example/MyServletContextListener.java:6: error:
MyServletContextListener is not abstract and does not override abstract
method contextDestroyed(ServletContextEvent) in ServletContextListener
public class MyServletContextListener implements ServletContextListener
{
^1 error
Here's my code:
package com.example;
import javax.servlet.*;
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event){
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);
}
public void contextDestroyed(ServletContext event) {
//System.out.println("1");
}
}
You'll notice that I tried overriding contextDestroyed() (now commented out), as the error implies that it might be the issue, but I get the same result. However the book I'm using even just says to not worry about it because we don't need to clean up because the context only goes away when the app goes down.
<context-param>
<param-name>adminEmail</param-name>
<param-value>someemail@gmail.com</param-value>
<param-name>mainEmail</param-name>
<param-value>someother@place.com</param-value>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>
</web-app>
Does it matter where this web.xml is when I compile? I've tried with it in the same dir, and in the etc directory that I setup as part of the tutorial. I would have thought it only mattered at runtime. Also, as fyi, I have multiple servlets in the same web.xml.
here's my version info: java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
The error message is telling you exactly what is wrong:
MyServletContextListener is not abstract and does not override abstract
method contextDestroyed(ServletContextEvent) in ServletContextListener
The class of the parameter for the contextDestroyed
method needs to be ServletContextEvent
not ServletContext