Saturday, July 9, 2011

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

32 comments:

  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
  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. This is an amazing blog,it gives very helpful messages to us.Besides that Creationinfoways has established himslef as Top 10 seo company in delhi as well as Top 10 website Development company in delhi.Now newly indroduced himself as one of the best growing Digital marketing company in delhi and Ecommerce Website Development Company in Delhi.

    ReplyDelete
  15. Nice Information you have written here. Really Great Stuff. I keep it bookmark for our future purpose. Our Best Presence in Social Media Marketing Find our Facebook Marketing Companies in Nagpur & LinkedIn Marketing Companies in Nagpur .
    AceZed IT Solution

    ReplyDelete
  16. hello!,I really like your writing so so much! percentage we keep up a correspondence more approximately your post on AOL? I need a specialist on this space to solve my problem.
    May be that’s you! Taking a look forward to see you. Java training jalandhar

    ReplyDelete

  17. 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
  18. 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
  19. 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

  20. very useful info, and please keep updating........

    ReplyDelete
  21. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete
  22. I just want to say that all the information you have given here is awesome...great and nice blog thanks sharing..Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Web Design Development Company
    Web design Company in Chennai
    Web development Company in Chennai

    ReplyDelete

  23. 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
  24. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  25. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    iOS App Development Company

    ReplyDelete

Please enter relevant question and information.