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.
Initialize stage has the following characteristics and usage
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.
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.
The following figure can help to summarize the life cycle of the Servlet
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:
Stages of Servlet Lifecycle:
A servlet passes through the following stages in its life.- Initialize
- Service
- Destroy
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 appletInitialize 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.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.
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
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:
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.
ReplyDeleteInterview 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!
DeleteGood Explanation
ReplyDeletecool
ReplyDeleteHmm is anyone else experiencing problems with the pictures on this blog loading?
ReplyDeleteI'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
I am sure this article has touched all the internet users, its really really good paragraph on building up new website.
ReplyDeleteMy blog ... Asian massage in London ()
You are so cool! I don't believe I've truly read
ReplyDeletesomething 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
My developer is trying to persuade me to move to .net from
ReplyDeletePHP. 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/
Your style is very unique in comparison to other people I have
ReplyDeleteread 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
I got this website from my pal who shared with me about this site and at the
ReplyDeletemoment this time I am browsing this web page and reading very
informative articles at this time.
Review my site: battlefront Heroes hack tool
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
ReplyDeleteniche. Your blog provided us beneficial information to
work on. You have done a extraordinary job!
Also visit my web site ... host boot
This piece of writing is really a good one
ReplyDeleteit assists new net visitors, who are wishing in favor of blogging.
Have a look at my blog post: real estate agent []
I visit day-to-day a few web sites and sites to read posts,
ReplyDeletehowever this web site provides quality based articles.
Also visit my webpage ... Ricky Salvador
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!
ReplyDeleteFeel free to surf to mmy site m88
It's going to be finish of mine day, except before finish I am reading this
ReplyDeletewonderful paragraph to increase my know-how.
Here is my blog post: cialis
You have shared a superb knowledge...
ReplyDeleteweb application developer company.
I very happy to read this informative post. Its a very useful for everyone.
ReplyDeletejava training institute
Keep writing a great post like this
ReplyDeletewebsite development
Web development Company
Advanced Java Tutorial
ReplyDeleteServlets
Background
The javax.servlet.http package
Using Cookies
Java Data-Base Connectivity
Java as Database Front End
What is JDBC?
The Tour of Swing
Japplet
Trees
ReplyDeleteYour 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
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
ReplyDeleteHvantage 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
ReplyDeleteNice 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
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”
ReplyDeleteThanks for the information. Really enjoyed it. How to start coding from scratch
ReplyDeleteLife Coaching in Chennai
ReplyDeletePersonality Development Training in Chennai
Very nice article!
ReplyDeletemagento developer uk
magento developer australia
magento developer usa
Great tutorial on servlet. I am a regular visitor of you website. Thanks for sharing such a nice tutorial.
ReplyDeletePHP Development Help
PHP Development Solution
Web Development Solution
This post is really nice and pretty well maintained thanks for it and keep updating.
ReplyDeleteWeb Designing course in Uttam Nagar
It is really interesting for me to read this article. Thanks for it. I like such topics and everything connected to them.
ReplyDeleteSEO Experts in Bangalore
Best SEO Services in Bangalore
low cost seo packages
Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleteDevOps Online Training
Your own ideas tend to be amazing. We frequently study your site and it is very useful. Web designers in Arlington
ReplyDeletehey nice articles posted.you shared really helpfull blog.thankyou for shareing.
ReplyDeleteprofessional-web-design-company
thanks!custom app development
ReplyDeletecustom application development
nice content!
ReplyDeletecustom application development company
custom database software
I have been browsing online more than 3 hours today, yet I never found any interesting article like yours.
ReplyDeleteIt 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
Very informative post. Thanks for sharing.
ReplyDeleteApplication Development & Maintenance Services
Mobile Application Development
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.
ReplyDeleteWith 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------------------------------
ReplyDeleteProven 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.
ReplyDeleteThank you for showing your thoughts by posting article. It is really nice one.
Idesignpassion
Best web design company
Best web design firms
Thanks for sharing your information..
ReplyDeleteWeb Designing Services in Hyderabad
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.
ReplyDeletecontact us on :+91 9811241103
Nice Post! Very good writing.....I am enjoy your post. Thanks!!!
ReplyDeleteIT helpdesk support
IT support
Anchor Green tea is an excellent combination of 10 superb herbs & naturals including Terminalia Arjuna, Cardamom, Green Tea, Cinnamon, Senna Leaves, Aniseed
ReplyDeletefire 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
urrah, that's what I was looking for, what a material!
ReplyDeleteexisting here at this website, thanks admin of this web page.
Feel free to surf to my webpage - 부산오피
Web designing institute in Noida
ReplyDeleteWeb designing and development institute in Noida
PHP Training institute in Noida
Great share and want to here about SEO Company in pune and want best seo pune team, then visit us SEO Pune
ReplyDeleteDo you know Narendra Modi ka pura naam kya hai and What are you doing in hindi
ReplyDeleteReally nice article. its really helpful me. Very interesting and good post thanks for sharing such a good blog.
ReplyDelete-Web Development Services
After a long time i found a unique and on purpose information about Custom Website. Can't wait to have more from you.
ReplyDeleteThank you so much for sharing this vital information with us. This is fantastic.Custom Website Development
ReplyDeleteYour Blog is very nice.Wish to see much more like this. Thanks for sharing your information.
ReplyDeleteSEM Agency
ReplyDeleteI seriously don't have enough words to thank you.
SEO company
Great share!One of the best blogs I have Ever Seen. Keep going!
ReplyDeleteSearch Engine Marketing Service
This is a fantastic article. Your essay is quite simple to comprehend. Thank you for sharing this informative information with us.
ReplyDeleteCustom Web Development
This comment has been removed by the author.
ReplyDeleteI love the podcasts available on this site.
ReplyDelete