It look like that you completely misunderstood the purpose of servlet's init()
. You seem to think somehow that you must override it. This is completely untrue. It just gives you the opportunity to declare a servlet method which should be invoked only once on servlet's initialization during application's startup. Usually, to initialize some local variables based on some services or configuration files or servlet's own <init-param>
. Please note, local variables, thus the ones specific to the servlet instance itself which are declared private
and are never shared/accessed elsewhere outside the servlet.
Particularly the following statement
Or i have to init() only the first serlvet that is called in my web application?
suggests that you're actually looking for an application-wide hook which is invoked during application's startup. In that case, you should be using a ServletContextListener
instead of servlet's init()
as answered in the Using special auto start servlet to initialize on startup and share application data question. Here's a Servlet 3.0 compatible kickoff example without the need to add a <listener>
entry to web.xml
:
@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.
}
}