Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 16 February 2013

Jsp and Servlet Tutorials - Servlet life cycle methods - doGet() and doPost() (Part 3 of 8)

Welcome to third part of jsp-servlet tutorials series. So far we have discussed Request-Response model of Servlet and Archetecture of Servlet. We have also discussed, what a servlet is ? why to use a servlet and we have also discussed how the communication takes place between client and the server. In this particular blog we will dive into lifecycle of a servlet and will come to know about servlet lifecycle methods doGet() & doPost() and their implementation.

Lets first starts with servlet lifecycle states, when it comes to servlet lifecycle we can say that sevlet has a single state so called initialization state. During its whole lifecycle servlet went through three main methods and all these methods are basically called by web container or servlet engine.


1 -init() : init() method of a servlet is called only once in its lifecycle, when a very first request comes to servlet it makes servlet state from does not exists to initialized state and during all other requests servlet remains already initialized.
The container calls the init() method before the servlet can serve any client request. This method can be overridden in our servlet classes to write some initialization code in it. Initialization means something like initializing your data source or other things that we want ready before servlet starts serving the request.

As we all knows that in java constructors are used for initialization stuff, now the question arises why do we use a init() method and not a constructor of servlet class ? The answer of this question is that init() method have access to two useful objects so called servletContext and servletConfig and we needs to use these two many times during our application. A constructor does not have access to servletConfig and servletContext thats why we use init() instead of class constructor to initialize a servlet.

What exactly the servletContext and servletConfig is, we will see this in a moment.

2 -service() : Service methods are the methods where actual working of servlet is defined, a service method is called every time a request comes for a servlet. It determines which http method should be called based on client request. There are 7 http methods but almost in 99% of web application you will be dealing with either a doGet() or a doPost() method.

If user request is a get request than doGet() method of servlet is called and if the request ia a post than doPost() method is called and all the business logic goes into either a doGet() or a doPost() method of servlet. 

3 -destroy() : This method is called by the container when the servlet is no longer required.

Servlet Hierarchy


Servlet Interface : At the top of the hierarchy we have an interface with name Servlet.

GenericServlet Abstract Class : This is an abstract class that implements Servlet interface and overrides its init() method. If init() method is not overridden in your servlet class than init() method of GenericServlet is implemented by default.

HttpServlet AbstractClass : This is the abstract class that is being extended by all servlets created by us, it has all service methods in it so called doGet() or doPost(). If our servlet does not override any service method than the one deleared in HttpServlet class in being implemented by default.

User defined servlet (the one we created in our application) : This is the servlet that we code in our application, http methods so called doGet() and doPost() are written here and all business logic and implementation goes inside these methods.

Difference between doGet() and doPost()
So far we have discussed a lot about doGet() and doPost() methods, now the question arises that when to use a doGet and when to use a doPost(). The one thing to be noted down here is that a get request is sent to request some resource from the server whereas the post request is sent to process data.

The get request contains only the request line and HTTP header, whereas a post request also contains HTTP body along with request line and header.

In get the request parameters are passed to the server by appending at the end of the url, whereas in a post request form elements or parameters are passed as a part of HTTP body and does not append at the end of URL. So whenever we need to send some sensitive information to server, a post request is sent.


The parameters data to be sent over the web is limited to a get request and it depends on container, whereas in a post request we can send a huge amount of data to the server.

In this particular blog from jsp-servlet tutorial series we come across servlet lifecysle and difference between doGet() and doPost() methods. In upcoming tutorial we will cover Response part of Servlet and Difference between RequestDispatcher and SendRedirect.











Thanks for reading !
Being Java Guys Team



3 comments:

  1. very informative tutorial...keep it up buddy !

    ReplyDelete
  2. Thanks a lot for the information.

    ReplyDelete
  3. good post (y)

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft