简体   繁体   English

当servlets有构造函数时,为什么我们需要servlets中的Init()方法?

[英]Why do we need Init() method in servlets when servlets has it Construtor?

在java中构造函数用于初始化为什么我们需要init()进行初始化....这个问题是在面试中问到的

The constructor is for normal Java initialization of an object (though typically a Servlet implementation is expected to have a no-arg constructor).构造函数用于对象的正常 Java 初始化(尽管通常Servlet实现应该具有无参数构造函数)。

The init() method is a method provided by the Servlet interface which a Servlet container will run to configure the Servlet . init()方法是Servlet接口提供的方法,Servlet 容器将运行该方法来配置Servlet The Servlet container will provide a ServletConfig object which gives the Servlet instance access to the ServletContext and other configuration elements from the deployment descriptor. Servlet 容器将提供一个ServletConfig对象,该对象使Servlet实例可以从部署描述符访问ServletContext和其他配置元素。

the init() method is a part of Servlet and ServletConfig protocol. init()方法是ServletServletConfig协议的一部分。 you can do what is related to the web-context in init() and what is private to Servlet class in constructor.您可以在init()与 web-context 相关的操作,以及在构造函数中执行 Servlet 类私有的操作。

在此处输入图片说明

The constructor is not part of the servlet lifecycle.构造函数不是 servlet 生命周期的一部分。

As per the javadocs根据javadocs

init and destroy, to manage resources that are held for the life of the servlet init 和 destroy,用于管理在 servlet 生命周期内保留的资源

and

The ServletConfig object can still be retrieved via getServletConfig().仍然可以通过 getServletConfig() 检索 ServletConfig 对象。

Init() method is called by the servlet container to indicate to a servlet that the servlet is being placed into service. Init()方法由 servlet 容器调用,以向 servlet 指示该 servlet 正被置于服务中。

The servlet container calls the init method exactly once after instantiating the servlet. servlet 容器在实例化 servlet 后只调用一次 init 方法。 The init method must complete successfully before the servlet can receive any requests.在 servlet 可以接收任何请求之前,init 方法必须成功完成。 This is the reason we need init() method.这就是我们需要init()方法的原因。

Refer these links:请参阅这些链接:

http://www.tutorialspoint.com/servlets/servlets-life-cycle.htm http://www.tutorialspoint.com/servlets/servlets-life-cycle.htm
http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html

1) Constructors are used by "Web Container(eg of Tomcat, WebSphere etc.) to instantiate GenericServlet/HttpServlet. 1) 构造函数被“Web Container(例如Tomcat、WebSphere 等)”用来实例化GenericServlet/HttpServlet。

2) Role of "Servlet.init()" method is to inject defined in web.xml. 2)“Servlet.init()”方法的作用是注入定义在web.xml中。 Yes, you can define servlet level parameters in constructor as well, but then, as a developer you shall unnecessarily invest time in doing the same stuff which can be done implicitly for you by (Container+Servlet API).是的,您也可以在构造函数中定义 servlet 级别的参数,但是,作为开发人员,您将不必要地投入时间做同样的事情,这些事情可以通过(Container+Servlet API)隐式为您完成。

3) Moreover, "Servlet.init()" is also used by web container to inject "ServletContext" object where you can't use constructor for that purpose. 3) 此外, Web 容器还使用“Servlet.init()” 来注入“ServletContext”对象,您不能为此目的使用构造函数。

  1. In JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments.在 JDK 1.0(最初为其编写 servlet)中,动态加载的 Java 类(例如 servlet)的构造函数不能接受参数。 So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface.因此,为了向新的 servlet 提供有关其自身及其环境的任何信息,服务器必须调用 servlet 的 init() 方法并传递一个实现 ServletConfig 接口的对象。

  2. Also, Java doesn't allow interfaces to declare constructors.此外,Java 不允许接口声明构造函数。 This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter.这意味着 javax.servlet.Servlet 接口不能声明接受 ServletConfig 参数的构造函数。 It has to declare another method, like init().它必须声明另一个方法,例如 init()。

  3. It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.当然,您仍然可以为您的 servlet 定义构造函数,但在构造函数中,您无权访问 ServletConfig 对象或抛出 ServletException 的能力。

I used init method to prepare my PreparedStatement once, so that the next time my servlet is called, the prepared statement already created.我使用 init 方法准备了我的 PreparedStatement 一次,以便下次调用我的 servlet 时,准备好的语句已经创建。 Please note that when a servlet first loaded in the app server, the init method will be executed.请注意,当 servlet 首次加载到应用服务器中时,将执行 init 方法。 The subsequent called to the servlet, the init method will be ignored.后续调用 servlet 时,init 方法将被忽略。 It's only called once for the lifecycle of the servlet它只在 servlet 的生命周期内被调用一次

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

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