What is Java Servlet Life Cycle and how to explain it

A servlet is managed through a well defined life cycle that defines how it is loaded and instantiated, is initialized, handles requests from clients, and is taken out of service. This life cycle is expressed in the API by the init, service, and destroy methods of the javax.servlet.Servlet interface that all servlets must implement directly or indirectly through the GenericServlet or HttpServlet abstract classes.

Stages of Servlet Lifecycle:

A servlet passes through the following stages in its life.
  • Initialize
  • Service
  • Destroy




What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, what is java servlet based web server, java web action, java web applications development, javawebaction

Initialize

When the servlet is first created, it is in the initialization stage. The web server invokes the init() method of the servlet in this stage. It should be noted here that init() is only called once and is not called for each request. Since there is no constructor available in Servlet so this urges its use for one time initialization just as the init() method of applet

Initialize stage has the following characteristics and usage
  • Executed once, when the servlet gets loaded for the first time
  • Not called for each client request
  • The above two points make it an ideal place to perform the startup tasks which are done in constructor in a normal class.

Service

The service() method is the engine of the servlet, which actually processes the client’s request. On every request from the client, the server spawns a new thread and calls the service() method as shown in the figure below. This makes it more efficient as compared to the technologies that se single thread to respond to requests.

what is java servlet based web server, What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle,  java web development, java web action, javawebaction, java web hosting

The figure below show both version of the implementation of service cycle. In the upper part of diagram we assume that servlet is made by sub-classing from GenericServlet (Remember, GenericServlet is used for constructing protocol independent servlets.). To provide the desired functionality, service() method is overridden. The client sends a request to the web server, a new thread is created to serve this request followed by calling the service() method. Finally a response is prepared and sent back to the user according to the request.

What is Java Servlet Life Cycle, java servlet httpServlet and GenericServlet subclass, Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle, servlet request responce, java web development, java web action, javawebaction, java web hosting


The second part of the figure illustrates a situation in which servlet is made using HttpServlet class. Now, this servlet can only serves the HTTP type requests. In these servlets doGet() and doPost() are overridden to provide desired behaviors. When a request is sent to the web server, the web server after creating a thread, passes on this request to service() method. The Service() method checks the HTTP requests type (GET, POST) and calls the doGet() or doPost method depending on how the request is originally sent. After forming the response by doGet() or doPost() method, the response is sent back to service() method that is finally sent to the user by the web server.

Destroy

The web server may decide to remove a previously loaded servlet instance, perhaps because it is explicitly asked to do so by the server administrator, or perhaps serlet container shuts down or the servlet is idle for a long time, or may be server is overloaded. Before it does, however it calls the servlets destroy() method. This makes it a perfect spot for releasing the acquired resources.


The following figure can help to summarize the life cycle of the Servlet


What is Java Servlet Life Cycle,Stages of java Servlet Lifecycle, what are different java servlet lifecycle stages, what is java servlet,  what is java, java tutorial, java servlet tutorial, java ee tutorials, explain java servlet life cycle, servlet request responce, java web development, java web hosting, java web action, javawebaction

The web sever creates a servlet instance. After successful creation, the servlet enters into initialization phase. Here, init() method is invoked for once. In case web server fails in previous two stages, the servlet instance is unloaded from the server. After initialization stage, the Servlet becomes available to serve the clients requests and to generate response accordingly. Finally, the servlet is destroyed and unloaded from web server.

Related Topics:

What is Java Servlet
What is Servlet Class Hierarchy
What are Java Servlet different HTTP Request Handling Methods

57 Comments

Please enter relevant questions and information.

  1. Very nice presentation. I have put across some interview questions regarding servlet life cycle like why no constructor in servlets. Wanted to share those with readers.

    ReplyDelete
    Replies
    1. Interview Questions! Then I must prepare for these! However I easily learned about servlet life cycle. Thanks for sharing this info! Here's a lot more to learn!

      Delete
  2. Hmm is anyone else experiencing problems with the pictures on this blog loading?

    I'm trying to determine if its a problem on my end or if it's the blog.
    Any responses would be greatly appreciated.

    Take a look at my blog post: attic mice removal

    ReplyDelete
  3. I am sure this article has touched all the internet users, its really really good paragraph on building up new website.


    My blog ... Asian massage in London ()

    ReplyDelete
  4. You are so cool! I don't believe I've truly read
    something like this before. So great to discover somebody with unique thoughts on this
    subject. Really.. thank you for starting this up. This website is one thing that's needed on the internet, someone with a little originality!



    my web site - daemon tools serial number

    ReplyDelete
  5. My developer is trying to persuade me to move to .net from
    PHP. I have always disliked the idea because
    of the costs. But he's tryiong none the less. I've been using WordPress on a variety
    of websites for about a year and am worried about switching
    to another platform. I have heard good things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into it?
    Any help would be really appreciated!

    my site ... http://News.goldgrey.org/silver/

    ReplyDelete
  6. Your style is very unique in comparison to other people I have
    read stuff from. I appreciate you for posting when you have the opportunity,
    Guess I'll just bookmark this blog.

    Feel free to visit my page: big dick

    ReplyDelete
  7. I got this website from my pal who shared with me about this site and at the
    moment this time I am browsing this web page and reading very
    informative articles at this time.

    Review my site: battlefront Heroes hack tool

    ReplyDelete
  8. Hey! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same
    niche. Your blog provided us beneficial information to
    work on. You have done a extraordinary job!

    Also visit my web site ... host boot

    ReplyDelete
  9. This piece of writing is really a good one
    it assists new net visitors, who are wishing in favor of blogging.


    Have a look at my blog post: real estate agent []

    ReplyDelete
  10. I visit day-to-day a few web sites and sites to read posts,
    however this web site provides quality based articles.



    Also visit my webpage ... Ricky Salvador

    ReplyDelete
  11. Wow! This blog looks just like my oold one! It's on a entirely different topic but it has pretty mch the same page layout and design. Excellent choice of colors!


    Feel free to surf to mmy site m88

    ReplyDelete
  12. It's going to be finish of mine day, except before finish I am reading this
    wonderful paragraph to increase my know-how.

    Here is my blog post: cialis

    ReplyDelete
  13. I very happy to read this informative post. Its a very useful for everyone.
    java training institute

    ReplyDelete

  14. Your post is just outstanding! thanx for such a post,its really going great and great work.You have provided great knowledge about the java web development.Website Design Bangalore | Website Design Agency Bangalore

    ReplyDelete
  15. When the servlet is first created, it is in the initialization stage. The web server invokes the init() method of the servlet in this stage. It should be noted here that init() is only called once and is not called for each request. Since there is no constructor available in Servlet so this urges its use for one time initialization just as the init() method of applet.erfect explanation about java programming .its very useful.thanks for your valuable information. best java training in chennai | best java training in velachery

    ReplyDelete
  16. Hvantage Technologies is leading offshore Mobile application development , and Web Development Company in USA offering various integrated IT Services including Ecommerce Solution, SEO Services & Digital Marketing Services.

    ReplyDelete

  17. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Fitness SMS
    Salon SMS
    Investor Relation SMS

    ReplyDelete
  18. I have never understood the concept of Java lifecycle but after reading this article I can comfortably explain the cycle comprehensively. Thanks for sharing such an informative article with us and I will be recommending this site to programming clients who access our professional writing services when they have requests such as “I Need Lit Review Rewriting Help

    ReplyDelete
  19. Great tutorial on servlet. I am a regular visitor of you website. Thanks for sharing such a nice tutorial.

    PHP Development Help

    PHP Development Solution

    Web Development Solution

    ReplyDelete
  20. This post is really nice and pretty well maintained thanks for it and keep updating.
    Web Designing course in Uttam Nagar

    ReplyDelete
  21. It is really interesting for me to read this article. Thanks for it. I like such topics and everything connected to them.
    SEO Experts in Bangalore

    Best SEO Services in Bangalore
    low cost seo packages

    ReplyDelete
  22. Excellent and very cool idea and great content of different kinds of the valuable information's.
    DevOps Online Training

    ReplyDelete
  23. Your own ideas tend to be amazing. We frequently study your site and it is very useful. Web designers in Arlington

    ReplyDelete
  24. hey nice articles posted.you shared really helpfull blog.thankyou for shareing.
    professional-web-design-company

    ReplyDelete
  25. I have been browsing online more than 3 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. Personally, if all site owners and bloggers made good
    content as you did, the internet will be a lot more useful
    than ever before.
    mobile app development company | ios app development company

    ReplyDelete
  26. website seo services in Singapore are basically a way to achieve high rankings in search engine results for your website, thereby increasing traffic on your website.

    ReplyDelete
  27. With pay per click package in singapore, it is possible to steer your PPC spending towards profitable keywords and phrases, as your SEO campaign is ramping up in the first few months.
    ------------------------------

    ReplyDelete

  28. Proven to rank page # 1 for your keywords, drive traffic, lead and sales, and help grow your business. Our result-oriented seo packages are designed to keep you ahead of the chase.

    ReplyDelete

  29. Thank you for showing your thoughts by posting article. It is really nice one.
    Idesignpassion
    Best web design company
    Best web design firms

    ReplyDelete
  30. Searching for Mobile app Development Company in Delhi? 3sstudio is the Best mobile app development Company in Delhi Specializing in android & IOS app development Services, with more than 1000+ Satisfied Clients.

    contact us on :+91 9811241103

    ReplyDelete
  31. Nice Post! Very good writing.....I am enjoy your post. Thanks!!!
    IT helpdesk support
    IT support

    ReplyDelete
  32. Anchor Green tea is an excellent combination of 10 superb herbs & naturals including Terminalia Arjuna, Cardamom, Green Tea, Cinnamon, Senna Leaves, Aniseed
    fire alarm system

    hearing aid Pakistan

    hearing aids in lahore

    custom suits

    best seo company in lahore

    calcium carbonate manufacturers

    chocolate tea in pakistan

    speech therapy in faisalabad

    Termite Treatment in lahore

    ReplyDelete
  33. urrah, that's what I was looking for, what a material!

    existing here at this website, thanks admin of this web page.

    Feel free to surf to my webpage - 부산오피

    ReplyDelete
  34. Great share and want to here about SEO Company in pune and want best seo pune team, then visit us SEO Pune

    ReplyDelete
  35. Really nice article. its really helpful me. Very interesting and good post thanks for sharing such a good blog.
    -Web Development Services

    ReplyDelete
  36. After a long time i found a unique and on purpose information about Custom Website. Can't wait to have more from you.

    ReplyDelete
  37. Thank you so much for sharing this vital information with us. This is fantastic.Custom Website Development

    ReplyDelete
  38. Your Blog is very nice.Wish to see much more like this. Thanks for sharing your information.
    SEM Agency

    ReplyDelete

  39. I seriously don't have enough words to thank you.
    SEO company

    ReplyDelete
  40. Great share!One of the best blogs I have Ever Seen. Keep going!
    Search Engine Marketing Service

    ReplyDelete
  41. This is a fantastic article. Your essay is quite simple to comprehend. Thank you for sharing this informative information with us.
    Custom Web Development

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
Previous Post Next Post