I am new to Servlets. I want to use a method which is called only once after deploying to server. I looked at HttpServlet#init()
. But I figured out it is called with each request. Did I misunderstand it? What are the alternatives to init()
?
3 Answers
No, it is not called in each request. It is only called during initialization of the servlet which usually happens only once in webapp's lifetime. Also see this answer for a bit more detail how servlets are created and executed.
If you actually want to do some global/applicationwide initialization (which is thus not per se tied to only the particular servlet), then you would normally use the ServletContextListener
for this. You can do the initialization stuff in the contextInitialized()
method.
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class Config implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// Do stuff during webapp's startup.
}
public void contextDestroyed(ServletContextEvent event) {
// Do stuff during webapp's shutdown.
}
}
If you're not on Servlet 3.0
yet and can't upgrade, and thus can't use @WebListener
annotation, then you need to manually register it in /WEB-INF/web.xml
like below:
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
-
thanks,,,so what is the main difference between contextInitialized and init(), what should run first ? – mebada Mar 02 '10 at 16:07
-
1The `ServletContextListener#contextInitialized()` is executed on webapp startup. The `HttpServlet#init()` is executed on servlet startup. I think that it should now be obvious to you that servlets cannot be initialized/started when the webapp isn't started yet. Check the link to the other answer to get the picture. You normally use `contextInitialized()` if you want to hook on webapp's startup. You normally use `init()` if you want to hook on servlet's startup. That's really all. If you don't even know what your own code is supposed to do, then I would rethink once again (or ask a question) – BalusC Mar 02 '10 at 16:12
-
I'll load configuration files to be used later with every request. – mebada Mar 02 '10 at 16:23
-
1If this is to be used applicationwide, then use `ServletContextListener`. If this is to be used by the servlet exclusively, then use `HttpServlet#init()`. It's nothing more than logical. – BalusC Mar 02 '10 at 16:27
-
@BalusC , Can i start Http server in contextInitialized method,As i need it to run only once after deployment. Does it leads to any problem. – Hema Apr 07 '17 at 08:16
init()
is only called upon creation of the servlet. This may happen multiple times during the life of the server. You use it to initialize any variables or logic required for regular use of the servlet.
Edit: After re-reading your post, it is not technically called with each request because the server is creating a new instance of the servlet for each request. Check your server settings as to whether it will get a new servlet of keep a single servlet for the life of the server.

- 2,396
- 16
- 19
Are you looking for a ServletContextListener?