java 从servlet中的服务方法调用init()方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10243730/
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
calling init() method from service method in servlet
提问by user2434
Can we call init() method from service() method in servlet ? I got this as an interview question. Why would anyone do this anyway ?
我们可以从 servlet 中的 service() 方法调用 init() 方法吗?我得到了这个作为面试问题。为什么会有人这样做呢?
回答by maksimov
No reason to call init
from service
, init
is meant to be called by the container - to initialise it with configuration (as the name suggests) and to allow it to do any expensive operations it needs to do (setting up connections to database or whatever).
没有理由init
从service
,调用init
意味着由容器调用 - 用配置初始化它(顾名思义)并允许它执行它需要做的任何昂贵的操作(设置到数据库的连接或其他)。
It may make some sense if you wanted to programmatically reconfigure your servlet to call the init
from the servlet itself, but I struggle to see this use case.
如果您想以编程方式重新配置 servlet 以init
从 servlet 本身调用 ,这可能是有意义的,但我很难看到这个用例。
回答by MykoB
In typical servlet lifecycle service() method won't be called before you call init() method
在典型的 servlet 生命周期 service() 方法在调用 init() 方法之前不会被调用
回答by Anandhakrishnan
You can call init as a method , not as you want it should be called in a lifecycle of servlet .
您可以将 init 作为方法调用,而不是在 servlet 的生命周期中调用它。
even the service method will be called only after init.
甚至只有在 init 之后才会调用 service 方法。
回答by GingerHead
init()
method is called to initialize all the needed things before the servlet can be put to work.
init()
在 servlet 开始工作之前,调用方法来初始化所有需要的东西。
The question asked in the interview was meant to make your knowledge knowable to them in the following fields:
面试中提出的问题旨在让他们了解您在以下领域的知识:
- What is
init()
- Why is it called for
- Where do we call it
- What will happen if we called it from the service
- 什么是
init()
- 为什么被称为
- 我们在哪里称呼它
- 如果我们从服务中调用它会发生什么
Number 4. It's like to construct a building and then to call for the buyers to settle in it and then begin putting its structure. They were asking this to know your managing and organizational talents over project handling.
第 4 点。这就像建造一座建筑物,然后呼吁买家在其中安顿下来,然后开始建造它的结构。他们要求这样做是为了了解您在项目处理方面的管理和组织才能。
In other words, doing that is to no avail, and your servlet will not be effected!
换句话说,这样做是无济于事的,您的 servlet 将不会受到影响!
回答by Sumit Singh
void init(ServletConfig config)throws ServletException
The servlet
container calls the init method
exactly once after instantiating the servlet
. The init method
must complete successfully before the servlet can receive any requests.
The servlet
container cannot place the servlet into service if the init method.
该servlet
容器调用init method
实例化后,只出现一次servlet
。在init method
必须成功完成之前,servlet可以接受任何请求。
如果出现以下情况,servlet
容器无法将 servlet 置于服务中init method.
But:if u want to call init
explicitly than u call as normal function of your class.
但是:如果你想init
显式调用而不是像你的类的普通函数那样调用。
回答by Hrant
The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.
init 方法被设计为只被调用一次。它在首次创建 servlet 时调用,不会为每个用户请求再次调用。因此,它用于一次性初始化,就像小程序的 init 方法一样。
The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started.
servlet 通常在用户第一次调用与 servlet 对应的 URL 时创建,但您也可以指定在服务器首次启动时加载 servlet。
When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet.
当用户调用 servlet 时,会创建每个 servlet 的单个实例,每个用户请求都会产生一个新线程,根据需要将其移交给 doGet 或 doPost。init() 方法只是创建或加载一些将在 servlet 的整个生命周期中使用的数据。
回答by Anand Vh
No i suggest not to call ,because init() method should be called only once to make some initializations (like Data base connection code ,this has to be called only once ,no reason to call it more than once ) where as service method will be called for every user request (with user specific request and response objects).Imagine if u make call for init from service method then init will called so many times.So it is not a good idea to call like that.
不,我建议不要调用,因为 init() 方法应该只调用一次来进行一些初始化(如数据库连接代码,这必须只调用一次,没有理由多次调用它),而服务方法将为每个用户请求调用(使用用户特定的请求和响应对象)。想象一下,如果你从服务方法调用 init,那么 init 将调用很多次。所以这样调用不是一个好主意。