<p>The problem is simple, but I'm struggling a bit already.</p>

<pre><code>Server server = new Server(8080);  
Context context = new Context(server, "/", Context.NO_SESSIONS);
context.addServlet(MainPageView.class, "/");
context.addServlet(UserView.class, "/signup");
server.start();
</code></pre>

<p>That's a pretty standard piece of code that you can find anywhere in Jetty world. I have an application that embeds Jetty as a servlet engine and has some servlets. </p>

<p>Instantiation of some of these servlets requires heavy work on startup. Say &ndash; reading additional config files, connecting to the database, etc. How can I make the servlet engine instantiate all servlets eagerly, so that I can do all the hard work upfront and not on the first user request?</p>

[{'answer_id': 75424, 'author': 'Justin Rudd', 'author_id': 12968, 'author_profile': 'https://Stackoverflow.com/users/12968', 'pm_score': 0, 'selected': False, 'text': '<p>Use the <code>Context.addServlet</code> overload that takes a <code>ServletHolder</code>.  <code>ServletHolder</code> is a class that accepts either a Class or a Servlet instance.</p>\n\n<pre><code>Servlet myServlet = new MyServlet();\nServletHolder holder = new ServletHolder(myServlet);\ncontext.addServlet(holder, "/");\n</code></pre>\n\n<p>This assumes Jetty 6.  I think it will work for Jetty 7 as well.</p>\n'}, {'answer_id': 75760, 'author': 'delux247', 'author_id': 5569, 'author_profile': 'https://Stackoverflow.com/users/5569', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not sure why using Guice make\'s Justin\'s option not work for you.  What exactly is getting injected in?  I\'m not sure if this would help you at all because it is very similar to what Justin wrote above but if you do it this way, Jetty will do the actually instantiating.</p>\n\n<pre><code>Context context = new Context(server, "/", Context.NO_SESSIONS);\nServletHolder mainPageViewHolder = new ServletHolder(MainPageView.class);\n// Do this to force Jetty to instantiate the servlet\nmainPageViewHolder.getServlet();  \ncontext.addServlet(mainPageViewHolder, "/");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/75379', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3105/']