Bug 62895 - load-on-startup of the @WebServlet does not work
Summary: load-on-startup of the @WebServlet does not work
Status: RESOLVED INVALID
Alias: None
Product: Tomcat 9
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 9.0.x
Hardware: PC All
: P2 normal (vote)
Target Milestone: -----
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-08 23:04 UTC by richard.gang.li8
Modified: 2018-11-10 19:54 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description richard.gang.li8 2018-11-08 23:04:08 UTC
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
Comment 1 richard.gang.li8 2018-11-08 23:07:08 UTC
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();
	}
}
Comment 2 Mark Thomas 2018-11-09 10:51:50 UTC
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.
Comment 3 richard.gang.li8 2018-11-09 18:03:39 UTC
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?
Comment 4 richard.gang.li8 2018-11-09 18:15:13 UTC
I have read the original sentence
<quote>
 The urlPatterns or the value attribute on the annotation MUST be present. 
</quote>

thank you very much