Life Cycle of Servlet in Detail

In this post, we will see the Servlet Life Cycle explained below in 5 steps.



1. Loading Servlet Class : 

• A Servlet class is loaded when the first request for the servlet is received by the Web Container.

2. Servlet instance creation : 
• After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle. 

3. Call to the init() method :
 • init() method is called by the Web Container on servlet instance to initialize the servlet. 
• Syntax: public void init(ServletConfig config) throws ServletException. 
• The init method is designed to be called only once. 
• It is called when the servlet is first created, and not called again for each user request. 

4. Call to the service() method : 
• The containers call the service() method each time the request for a servlet is received. 
• The service() method will then call the doGet, doPost, doPut, doDelete, etc. methods as appropriate based on the type of the HTTP request. 
• Syntax: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException 
• The service () method is called by the container and service method invokes doGet, doPost, doPut, doDelete, etc. methods as appropriate. So we override either doGet() or doPost() depending on what type of request we receive from the client. 

5. Call to destroy() method:
 • The Web Container calls the destroy() method before removing the servlet instance for cleanup activity. 
• Syntax: public void destroy() { // Finalization code... } 
• It is called only once at the end of the life cycle 
• Then it is marked for garbage collection


HttpServlet Methods: 

doGet method:

 • It is used to serve the request of user. 

• It will get called implicitly if no method is mentioned. 

• The service method calles this method according to user requirement. 

• Syntax: public void doGet(HttpServletRequest request, HttpServletResponse response)throws

ServletException, IOException { // Servlet code }

doPost method:

 • It is also used to serve the request of the user. 

• It has to be called explicitly whenever the user needs to pass secure content. 

• The service method calls this method according to user requirements. 

• Syntax: public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code } 

doPut method: 

• The doPut() method handles requests sent using the HTTP PUT method. 

• The PUT method allows a client to store information on the server. 

• For example, you can use it to post an image file to the server 

• Syntax: public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code } 

doDelete method: 

• It is called by the server (via the service method) to allow a servlet to handle a DELETE request. 

• The DELETE operation allows a client to remove a document or Web page from the server. 


 Difference between GET and POST methods:

Feature

GET

POST

Sending of data

Client data is appended to URL and sent

Client data is sent separately

Storing in the browser history

As data is appended, the client data is stored in the browser history

As data is sent separately, the client data is not stored in the browser history

Bookmark

The URL with the client data can be bookmarked. Thereby, later without filling the HTML form, the same data can be sent to the server

Not possible to bookmark

Limitation of data sent

Limited to 2048 characters (browser dependent)

Unlimited data

Hacking easiness

Easy to hack the data as data is stored in the browser history

Difficult to hack

Type of data sent

Only ASCII data can be sent

Any type of data can be sent including binary data

Data secrecy

Data is not as secret as other people can see the data in the browser history

Data is secret as not stored in history

When to be used

Prefer when data sent is not secret. Do not use for passwords etc.

Prefer for critical and sensitive data like passwords etc.

Cache

Can be caught

Cannot be caught

Default

If not mentioned, GET is assumed as default

Should be mentioned explicitly

Performance

Relatively faster as data is appended to URL

A separate message the body is to be created



Recommended Topics:

Web Server and Working of CGI

Introduction to Servlet




Previous
Next Post »