servlet assignment
servlet assignment
ID:3201/14
SECTION B
What is Servlet?
Servlet Lifecycle
A servlet life cycle can be defined as the entire process from its
creation till the destruction. The following are the paths followed by
a servlet
• The servlet is initialized by calling the init () method.
• The servlet calls service() method to process a client's request.
• The servlet is terminated by calling the destroy() method.
• Finally, servlet is garbage collected by the garbage collector of
the JVM.
The init method is called only once. It is called only when the
servlet is created, and not
called for any user requests afterwards. So, it is used for one-time
initializations, just as
servlet, but you can also specify that the servlet be loaded when the
server is first
started.
appropriate. The init() method simply creates or loads some data that
will be used
// Initialization code...
}
The service() method is the main method to perform the actual task.
The servlet
the client( browsers) and to write the formatted response back to the
client.
Each time the server receives a request for a servlet, the server
spawns a new thread and calls service. The service() method checks the
HTTP request type (GET, POST, PUT,
DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods
as appropriate.
Here is the signature of this method:
ServletResponse response)
The doGet() and doPost() are most frequently used methods with in each
service
A GET request results from a normal request for a URL or from an HTML
form that has
HttpServletResponse response)
// Servlet code
}
A POST request results from an HTML form that specifically lists POST
as the METHOD
HttpServletResponse response)
//Servlet code
The destroy() method is called only once at the end of the life cycle
of a servlet. This
threads, write cookie lists or hit counts to disk, and perform other
such cleanup
activities.
After the destroy() method is called, the servlet object is marked for
garbage collection.
Servlets are Java classes which service HTTP requests and implement
the javax.serviet.Servlet interface. Web application developers
typically write servlets thatextend javax.servlet. http.HttpServlet,
an abstract class that implements the Servlet
SUBMITTED TO : Mr.ABATE