Tomcat Java Servlet - 在应用程序启动时初始化类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6206996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat Java Servlet - Initialize Class on Application Startup
提问by Petey B
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?
我有一个需要一些时间来启动的类(进行一些 JNI 调用和什么不),所以每次加载页面时初始化这个类是不可行的。是否可以在应用程序启动时初始化此类,然后在提供页面时访问其方法?
For Example:
例如:
I have MyClass. When the application (tomcat) starts up I would like it to initialze my calss as follows:
我有我的课堂。当应用程序 (tomcat) 启动时,我希望它按如下方式初始化我的 calss:
MyClass myClassInstance = new MyClass("arg1", "arg2");
Then when a page is called, say /testpage, I would like to make calls to myClassInstance:
然后当一个页面被调用时,比如 /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>");
}
}
Is this possible?
这可能吗?
回答by Tomasz Nurkiewicz
You have two choices:
你有两个选择:
Initialize your class in servlet's
init()
method. You may add<load-on-startup>
attribute to make sure your servlet is created at application startup and not on first access.Add
ServletContextListener
and usecontextInitialized()
callback method. UseServletContext#setAttribute
to store created object for future use.
在 servlet 的
init()
方法中初始化您的类。您可以添加<load-on-startup>
属性以确保您的 servlet 在应用程序启动时创建,而不是在第一次访问时创建。添加
ServletContextListener
和使用contextInitialized()
回调方法。使用ServletContext#setAttribute
存储创建的对象以备将来使用。
回答by QuantumMechanic
If you want it to happen once for the whole app, and happen before any servlet is run, implement ServletContextListener
and put your startup code in contextInitialized()
. Then set up your web.xml
to specify your class as a listener.
如果您希望它在整个应用程序中发生一次,并且在任何 servlet 运行之前发生,请实现ServletContextListener
并将您的启动代码放入contextInitialized()
. 然后设置您web.xml
以将您的类指定为侦听器。
Otherwise, you can do what the other answer says and put it in the init()
method of the servlet.
否则,您可以按照其他答案所说的进行操作,并将其放入init()
servlet的方法中。
回答by Cratylus
You can do the initialize of the class inside the servlet's init
method.init()
method is invoked when the servlet instance is loaded so it is a good place for expensive operations.
您可以在 servlet 的init
方法中初始化类。init()
方法在加载 servlet 实例时被调用,因此它是进行昂贵操作的好地方。