简体   繁体   English

在doget()方法中调用servlet的destroy方法

[英]calling destroy method of servlet inside doget() method

I was wondering what would happen , If I call destroy() method of servlet inside doget() method, lets assume this is my first statement inside doget() method itself.Please advise.. 我想知道会发生什么,如果我在doget()方法中调用servlet的destroy()方法,请假设这是我在doget()方法本身中的第一个语句。请注意。

I have tried in my application as shown below.. 我已经在我的应用程序中尝试过,如下所示。

public class MyServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{

     destroy(); //calling destroy


String name=request.getParameter("txtName");
HttpSession ses=request.getSession();
ses.setAttribute("username",name);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html><head><title>Cookie example</title></head><body>");
out.println("welcome,"+name);
out.println("<br><a href=ck>Take a Tour</a></body></html>");
out.close();
}
}

But my application works fine but still please explain me the logic, as I am still not clear. 但是我的应用程序运行正常,但是仍然不清楚,请仍然向我解释一下逻辑。

Please advise what piece of code is need to be written that is I want to override the destroy() such that upon executing of it servlet get destroyed immediately 请告知需要编写的代码片段是我想要覆盖destroy()的代码,以便在执行该代码时立即销毁Servlet。

That of course depends perfectly about your implementation. 当然,这完全取决于您的实现。 If you haven't overridden it, then it does quite much nothing, because implementation of destroy is empty in HttpServlet. 如果您没有重写它,那么它什么都不做,因为在HttpServlet中destroy的实现是空的。 As result application continues to run normally. 结果,应用程序继续正常运行。

Maybe there is some confusion about purpose of destroy method. 关于销毁方法的目的可能有些困惑。 Purpose is not that servlet container provides some method that destroys servlet . 目的不是servlet容器提供某种销毁servlet的方法

Instead it gives you possibility to provide some code that will be executed when destroy method is called by container. 相反,它使您可以提供一些在容器调用destroy方法时将要执行的代码。 In some cases there is need to clean resources (closing database connection for example) when container decides to removes servlet. 在某些情况下,当容器决定删除servlet时,需要清理资源(例如,关闭数据库连接)。 Container can remove servlet quite much independently: for example if it is short of memory. 容器可以非常独立地删除servlet:例如,如果内存不足。 Method destroy will be called as part of removal. 方法destroy将被称为移除的一部分。

If your goal is to destroy servlet instance, destroy method is not the right tool. 如果您的目标是销毁Servlet实例,则destroy方法不是正确的工具。 Once again, call to destroy is part of removing servlet instance, not the cause of removal. 再一次,调用destroy是删除servlet实例的一部分,而不是删除的原因。 Right tool is to throw UnavailableException from doGet (no need for destroy method here). 正确的工具是从doGet抛出UnavailableException (这里不需要destroy方法)。 As said in the Javadoc , parameterless constructor creates such a instance which indicates that servlet is permanently unavailable. Javadoc中所述,无参数构造函数创建这样的实例,该实例指示servlet永久不可用。 Further it is containers task to react to this, as said in servlet specification: 进一步,这是容器的任务,如servlet规范中所述:

If a permanent unavailability is indicated by the UnavailableException, the servlet container must remove the servlet from service, call its destroy method, and releasethe servlet instance. 如果UnavailableException指示永久性不可用性,则servlet容器必须从服务中删除该servlet,调用其destroy方法,并释放该servlet实例。 Any requests refused by the container by that cause must be returned with a SC_NOT_FOUND (404) response. 容器由于该原因拒绝的任何请求都必须返回SC_NOT_FOUND(404)响应。

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

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