简体   繁体   English

servlet 的 init() 方法是做什么用的?

[英]What is the servlet's init() method used for?

When I decompile the GenericServlet and check the init() , I see the following code .当我反编译 GenericServlet 并检查 init() 时,我看到以下代码。

public void init(ServletConfig servletconfig)
    throws ServletException
{
    config = servletconfig;
    init();
}

public void init()
    throws ServletException
{
}

What is the init method actually doing here ? init 方法在这里实际做什么? Am I missing something ?我错过了什么吗?

Yes, it does nothing.是的,它什么都不做。 It could have been abstract, but then each servlet would be forced to implement it.它本来可以是抽象的,但是每个 servlet 都将被迫实现它。 This way, by default, nothing happens on init() , and each servlet can override this behaviour.这样,默认情况下, init()不会发生任何事情,并且每个 servlet 都可以覆盖此行为。 For example, you have two servlets:例如,您有两个 servlet:

public PropertiesServlet extends HttpServlet {

   private Properties properties;

   @Override
   public void init() {
       // load properties from disk, do be used by subsequent doGet() calls
   }
}

and

public AnotherServlet extends HttpServlet {

   // you don't need any initialization here, 
   // so you don't override the init method.
}

From the javadoc :javadoc

/**
 *
 * A convenience method which can be overridden so that there's no need
 * to call <code>super.init(config)</code>.
 *
 * <p>Instead of overriding {@link #init(ServletConfig)}, simply override
 * this method and it will be called by
 * <code>GenericServlet.init(ServletConfig config)</code>.
 * The <code>ServletConfig</code> object can still be retrieved via {@link
 * #getServletConfig}. 
 *
 * @exception ServletException  if an exception occurs that
 *                  interrupts the servlet's
 *                  normal operation
 *
 */

So it does nothing and is just a convenience.所以它什么都不做,只是一种方便。

Constructor might not have access to ServletConfig since container have not called init(ServletConfig config) method.构造函数可能无法访问ServletConfig因为容器没有调用init(ServletConfig config)方法。

init() method is inherited from GenericServlet which has a ServletConfig as its property. init()方法继承自GenericServlet ,它具有ServletConfig作为其属性。 thats how HttpServlet and what ever custom servlet you write by extending HttpServlet gets ServletConfig .这就是HttpServlet以及您通过扩展HttpServlet编写的任何自定义 servlet 获取ServletConfig

and GenericServlet implements ServletConfig which has getServletContext method.GenericServlet实现了具有getServletContext方法的ServletConfig so your custom servlets init method will have access to both of those.因此您的自定义 servlet init方法将可以访问这两者。

Servlet container calls servlet init() method before handling client requests. Servlet 容器在处理客户端请求之前调用 servlet init()方法。 It is called just one times after servlet is created.它在 servlet 创建后仅被调用一次。 By default it does nothing.默认情况下它什么都不做。 You can override this method and it is also good for performing one-time activities.您可以覆盖此方法,它也适用于执行一次性活动。 Such as database connection or reading configuration data etc.如数据库连接或读取配置数据等。

public void init(ServletConfig config) throws ServletException {
     super.init(config);

     // You can define your initial parameter in web.xml file.
     String initialParameter = config.getInitParameter("initialParameter");
     // Do some stuff with initial parameters

}

What happens if init() method throw an exception?如果 init() 方法抛出异常会发生什么?

The servlet destroy() will not be called because it is unsuccesfull initialization. servlet destroy()不会被调用,因为它是不成功的初始化。 Servlet container may try to instantiate and initialize a new instance of this failed servlet later.稍后 Servlet 容器可能会尝试实例化并初始化此失败的 servlet 的新实例。

Who said init method do nothing.谁说init方法什么都不做。 It's use for one time servlet initialization of variable etc. For example you can put PreparedStatement initialization in there.它用于一次 servlet 初始化变量等。例如,您可以将 PreparedStatement 初始化放在那里。 So that the next call of the servlet, there is no need anymore for PS initialization.这样下次调用servlet时,就不需要再进行PS初始化了。 You must know that init is part of Servlet lifecycle.您必须知道 init 是 Servlet 生命周期的一部分。

The first time the servlet loaded in Glassfish or Tomcat, the init method will be called and servlet stays in memory of the application server. servlet 首次加载到 Glassfish 或 Tomcat 中时,将调用 init 方法并且 servlet 保留在应用程序服务器的内存中。 The subsequent call will execute servlet and the init method will not be called.后续调用将执行 servlet,不会调用 init 方法。 However the variable initialization is there, already initialized.然而,变量初始化在那里,已经初始化。

The init() method simply creates (once only) and loads some data that will be used throughout the life of the servlet. init() 方法只是创建(仅一次)并加载一些将在 servlet 的整个生命周期中使用的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM