title: Tomcat Java Servlet - Initialize Class on Application Startup
body: <p>I have a class that takes a bit of time to start up (makes some JNI calls and what not), so it is not feasable to initialize this class everytime a page loads. Is it possible to initialize this class on application startup, then access its methods as pages are being served up?</p>
  
  <p>For Example:</p>
  
  <p>I have MyClass. When the application (tomcat) starts up I would like it to initialze my calss as follows:</p>
  
  <pre><code>MyClass myClassInstance = new MyClass("arg1", "arg2");
  </code></pre>
  
  <p>Then when a page is called, say /testpage, I would like to make calls to myClassInstance:</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import MyClass;
  
  public class TestPage extends HttpServlet {
  
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
      {
          String myResult = myClassInstance.getResult("whatever");
  
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("&lt;html&gt;");
          out.println("&lt;head&gt;");
          out.println("&lt;title&gt;Test&lt;/title&gt;");
          out.println("&lt;/head&gt;");
          out.println("&lt;body&gt;");
          out.println("&lt;h1&gt;" +myResult +"&lt;/h1&gt;");
          out.println("&lt;/body&gt;");
          out.println("&lt;/html&gt;");
      }
  }
  </code></pre>
  
  <p>Is this possible?</p>