我有一個需要一點時間才能啓動的類(創建一些JNI調用,但不是),所以每次加載頁面時都不可能初始化此類。是否可以在應用程序啓動時初始化此類,然後在頁面被提供時訪問它的方法?Tomcat Java Servlet - 在應用程序啓動時初始化類
例如:
我有MyClass。當應用程序(Tomcat)的啓動,我想它initialze我CALSS如下:
MyClass myClassInstance = new MyClass("arg1", "arg2");
然後當一個頁面被調用時,說/ testpage,我想做出myClassInstance電話:
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("<html>");
out.println("<head>");
out.println("<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" +myResult +"</h1>");
out.println("</body>");
out.println("</html>");
}
}
這可能嗎?
相關:http://stackoverflow.com/questions/3468150/using-init-servlet,http://stackoverflow.com/questions/3153739/config-files-for-a-webapplication-load-once-and -store-在哪裏,http://stackoverflow.com/questions/5951294/is-there-a-way-to-do-something-on-a-j2ee-server-start-im-using-tomcat等 – BalusC 2011-06-01 20:19:47