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

1. Loading Servlet Class :
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:
ConversionConversion EmoticonEmoticon