Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Jakarta Servlet
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Life cycle of a servlet == Three methods are central to the life cycle of a servlet. These are <code>init()</code>, <code>service()</code>, and <code>destroy()</code>. They are implemented by every servlet and are invoked at specific times by the server. * During initialization stage of the servlet [[object lifetime|life cycle]], the web container initializes the servlet instance by calling the [http://docs.oracle.com/javaee/7/api/javax/servlet/Servlet.html#init <code>init()</code>] method,{{sfn | Murach | Urban | 2014 | loc=§15 How to develop servlet - More skills for working with servlets| pp=160-163}} passing an object implementing the [http://docs.oracle.com/javaee/7/api/javax/servlet/ServletConfig.html <code>javax.servlet.ServletConfig</code>] interface. This configuration object allows the servlet to access [[attribute–value pair|name-value]] initialization parameters from the web application. * After initialization, the servlet instance can service client requests. Each [[HTTP request#Request message|request]] is serviced in its own separate thread. The web container calls the <code>service()</code> method of the servlet for every request. The <code>service()</code> method determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the servlet must provide an implementation for these methods. If a request is made for a method that is not implemented by the servlet, the method of the parent class is called, typically resulting in an error being returned to the requester. * Finally, the web container calls the <code>destroy()</code> method that takes the servlet out of service. The <code>destroy()</code> method, like <code>init()</code>, is called only once in the lifecycle of a servlet. The following is a typical user scenario of these methods. # Assume that a user requests to visit a [[uniform resource locator|URL]]. #* The browser then generates an HTTP request for this URL. #* This request is then sent to the appropriate server. # The HTTP request is received by the web server and forwarded to the servlet container. #* The container maps this request to a particular servlet. #* The servlet is dynamically retrieved and loaded into the address space of the container. # The container invokes the <code>init()</code> method of the servlet. #* This method is invoked only when the servlet is first loaded into memory. #* It is possible to pass initialization parameters to the servlet so that it may configure itself. # The container invokes the <code>service()</code> method of the servlet. #* This method is called to process the HTTP request. #* The servlet may read data that has been provided in the HTTP request. #* The servlet may also formulate an HTTP response for the client. # The servlet remains in the container's address space and is available to process any other HTTP requests received from clients. #* The <code>service()</code> method is called for each HTTP request. # The container may, at some point, decide to unload the servlet from its memory. #* The algorithms by which this decision is made are specific to each container. # The container calls the servlet's <code>destroy()</code> method to relinquish any resources such as file handles that are allocated for the servlet; important data may be saved to a persistent store. # The memory allocated for the servlet and its objects can then be garbage collected.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Jakarta Servlet
(section)
Add topic