Do I ever have to specify class name for @WebListener annotation like I have to do for @WebServlet("/MyClassname") ?
In my application just @WebListener works fine, but I have only single ServletContextListener - maybe if I had a few it would have been different?
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
...
}
@WebListener
public class InitWebAppServlet implements ServletContextListener {
...
}
The value in
@WebServletis not a class name. It is a URL path. When using only a single parameter like you show it is the equivalent of:Your code could just as easily have been:
That means that you'd access your servlet with the url
/bobsyouruncleinstead of/MyServlet. See the WebServlet javadocs for more details. Note that for annotations if you have only a single parameter it sets the elementvalue.The optional value of the
@WebListenerparameter is for a description of the listener (see the WebListener javadocs). It is, again, not a class name. You can have as many@WebListenerclasses as you'd like though by using the annotation method instead of theweb.xmlmethod you cannot guarantee the order in which the servlet container runs them.