java 我们可以从 Servlet 中的 destroy() 方法调用 service() 方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16745857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:51:25  来源:igfitidea点击:

Can we call service() method from destroy() method in Servlet?

javajakarta-eeservletsservicemethods

提问by JDGuide

This is one of the interview questions I faced a few days ago:

这是我前几天遇到的面试问题之一:

Is it possible to call the service()method from destroy()?

是否可以从 调用该service()方法destroy()

Thanks in advance.

提前致谢。

回答by acdcjunior

destroy()is a lifecyclemethod called by the Servlet container when unloading a specific instance of the Servlet. Similarly, the containerwill call service()when there's a client requesting the Servlet.

destroy()是Servlet 容器在卸载 Servlet 的特定实例时调用的生命周期方法。同样,当有客户端请求 Servlet 时,容器将调用service()

Can we call service()method from destroy()method in Servlet?

我们可以service()destroy()Servlet 中的方法调用方法吗?

Short answer:Yes, as service()is a method like any other.

简短回答:是的service()就像任何其他方法一样。

Long answer:You can, but it doesn't make sense. service()requires a request and a response parameters that are usually provided by the container when the Servlet is called. If you are calling service()by yourself, how are you gonna provide those parameters? What for? Are you gonna use nullon both? What good is service()for two empty parameters?

长答案:可以,但这没有意义。service()需要一个请求和一个响应参数,这些参数通常由容器在调用 Servlet 时提供。如果您service()自己调用,您将如何提供这些参数?做什么的?你会使用null两个?service()两个空参数有什么好处?

Can we call destroy()method from service()method in Servlet?

我们可以destroy()service()Servlet 中的方法调用 方法 吗?

Yes, again, you can call destroy()from within the service()as it is also a method like any other. Although still strange, this could make sense sometimes, as destroy()will do whatever logic you have defined (cleanup, remove attributes, etc.).

是的,同样,您可以destroy()从内部调用,service()因为它也是一种与其他方法一样的方法。尽管仍然很奇怪,但这有时是有意义的,就像destroy()您定义的任何逻辑(清理、删除属性等)一样。



IMPORTANT: Just bear in mind that simply calling destroy()won't unloadthe Servlet instance. You do not manage the lifecycle of Servlets in the program, the Servlet Container does.

重要提示:请记住,简单地调用destroy()不会卸载Servlet 实例。您不会在程序中管理 Servlet 的生命周期,而是由 Servlet 容器来管理。

回答by Alpesh Gediya

Purpose of destroy()is to de-allocated/free all the resources used by Servlet instance. By calling destroy()container deregister servlet and its service.

目的destroy()是取消分配/释放 Servlet 实例使用的所有资源。通过调用destroy()容器注销servlet及其服务。

Yes you can callthe service(request, response)like anyohter method from destroy()but it wont be executed so its useless to call service method from destroy()as those service method never going to be called/executed, request and response will be null as those objects will not be provided by container.

是的,你可以调用service(request, response)像anyohter方法destroy(),但它不会被执行,从而从它的无用的呼叫处理方法destroy()为那些服务的方法永远不会被调用/执行,请求和响应将是空的那些对象不会由容器来提供。

public void destroy() {
      try
      { 
          doPost(null, null); // will not be executed 
          doGet(null, null); // will not be executed 
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }

From Java doc:

来自Java 文档

public void destroy()

公共无效销毁()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed.

由 servlet 容器调用以向 servlet 指示 servlet 正在停止服务。只有在 servlet 的服务方法中的所有线程都退出或超时时间过后,才会调用此方法。

After the servlet container calls this method, it will not call the service method again on this servlet.

servlet 容器调用此方法后,将不会在此 servlet 上再次调用服务方法。

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

此方法使 servlet 有机会清理任何被占用的资源(例如,内存、文件句柄、线程)并确保任何持久状态与 servlet 在内存中的当前状态同步。