Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:37:53+00:00 2026-06-07T23:37:53+00:00

I was wondering what would happen , If I call destroy() method of servlet

  • 0

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..

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:37:54+00:00Added an answer on June 7, 2026 at 11:37 pm

    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. 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.

    Instead it gives you possibility to provide some code that will be executed when destroy method is called by container. In some cases there is need to clean resources (closing database connection for example) when container decides to removes servlet. Container can remove servlet quite much independently: for example if it is short of memory. Method destroy will be called as part of removal.

    If your goal is to destroy servlet instance, destroy method is not the right tool. Once again, call to destroy is part of removing servlet instance, not the cause of removal. Right tool is to throw UnavailableException from doGet (no need for destroy method here). As said in the Javadoc, parameterless constructor creates such a instance which indicates that servlet is permanently unavailable. Further it is containers task to react to this, as said in servlet specification:

    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. Any requests refused by the container by that cause must be
    returned with a SC_NOT_FOUND (404) response.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering what would happen if I tried to read files in Java
I am wondering how would I deal with a call to a function when
I was just wondering, what would happen if key of a HashMap is mutable,
I was wondering would I still need to use a basic game loop for
I'm wondering what would be best to do. Right now I have running a
I am wondering how would you store a list of categories and sub categories
Hey im just wondering what would be the best collection to use when creating
So I am wondering what would cause it so that all events for the
i was wondering what would be the best way to code a browser plugin
I was just wondering which would be cheaper, using a try catch block for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.