when i define a load-on-startup Servlet without url-patterns if I use the web deployment descriptor(web.xml), I can use the below configuration: <servlet> <servlet-name>timerServlet</servlet-name> <servlet-class>org.crazyit.TimerServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> the Servlet will be created and init method will be invoked, When i started the web app. it means, if i use deploy the loadup-on-startup Servlet with web.xml, everything is ok. but, if i use @WebServlet Annotation, loadOnStartup doesnot work. i use the same Servlet, i don't use the xml configuration,i just use @WebServlet(loadOnStartup=1) init method of the servlet will not be invoked, When i started the web app. unless i use @WebServlet(loadOnStartup=1, urlPatterns={}), but it does not make sense,because i don't need any url-pattern for the servlet
My Servlet is very simple, the Servlet just override the init(ServletConfig) method public class TimerServlet extends HttpServlet { public void init(ServletConfig config)throws ServletException { super.init(config); Timer t = new Timer(1000,new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(new Date()); } }); t.start(); } }
Servlet specification, version 4, section 8.1.1 <quote> The annotated servlet MUST specify at least one url pattern to be deployed. </quote> No such restriction applies to web.xml.
OK, I see that But I have another question, if I use the annotation like that @WebServlet(loadOnStartup=1, urlPatterns={}), it means the Servlet don't have any url pattern, right? But the Servlet still works, It is still a problem?
I have read the original sentence <quote> The urlPatterns or the value attribute on the annotation MUST be present. </quote> thank you very much