Java – can we call the service () method from the destroy () method in the servlet?

This is one of the questions I interviewed the other day:

Can I call the service () method from destroy()?

Thank you in advance

Solution

Destroy () is a lifecycle method called by the servlet container when unloading a specific instance of a servlet Similarly, when a client requests a servlet, the container will call service ()

Short answer: Yes, because service () is a method like other methods

Long answer: you can, but it doesn't make sense Service () needs the request and response parameters provided by the container when calling the servlet If you call service (), how do you provide these parameters? What's your job? Do you both want to use null? Service() has two empty parameters?

Yes, again, you can call destroy () from service (), because it is also a way like other methods. It's still strange, but sometimes it makes sense because destroy () will perform any logic you define (cleaning, deleting attributes, etc.)

Important: remember that a simple call to destroy () will not uninstall the servlet instance The servlet container does not manage the lifecycle of servlets in the program

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>