servlets - I'm unable to initialize my ServletContextListener. The .java class will not compile. Can anyone point me into what is going on? -
the error message indicates i'm not overriding abstract class and/or method appropriately. interestingly, i'm following code that's in book; , it's supposed compilable example (head first java servlets , 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 not abstract , not override abstract method contextdestroyed(servletcontextevent) in servletcontextlistener public class myservletcontextlistener implements servletcontextlistener { ^1 error
here's 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 tried overriding contextdestroyed() (now commented out), error implies might issue, same result. book i'm using says not worry because don't need clean because context goes away when 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 matter web.xml when compile? i've tried in same dir, , in etc directory setup part of tutorial. have thought mattered @ runtime. also, fyi, have multiple servlets in same web.xml.
here's 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 telling wrong:
myservletcontextlistener not abstract , not override abstract method contextdestroyed(servletcontextevent) in servletcontextlistener
the class of parameter contextdestroyed
method needs servletcontextevent
not servletcontext
Comments
Post a Comment