31. Explain Servlet Life Cycle.

  1. Servlets are normal Java classes which are created when needed and destroyed when not needed. Since Servlets run within a Servlet Container, creation and destruction of Servlets is the duty of Servlet Container and not yours.
  2. Implementing the init() and destory() methods of Servlet interface allows you to be told by the Servlet Container that when it has created an instance of your Servlet and when it has destroyed that instance.
  3. An important point to remember is that your Servlet is not created and destroyed for every request it receives, rather it is created and kept in memory where requests are forwarded to it and your Servlet then generates response.

  

Servlet Initialization (init())
So you have created your Servlet class by extending the HttpServlet class and have placed it in /WEB-INF/classes/ directory of your application.

Now when will Servlet Container create an instance of your Servlet? there are two situations :

  • When you have specifically told the Servlet Container to preload your Servlet when the Servlet Container starts by setting the load-on-startup tag to a non-zero value e.g.
    <web-app>
    
      <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>com.wordpress.sharat.servlets.TestServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
    </web-app>

    So when the Servlet Container starts it will preload your Servlet in the memory.

  • If your Servlet is not already loaded, it’s instance will be created as soon as a request is received for it by the Servlet Container.
  • During the loading of the Servlet into the memory, Servlet Container will call your Servlet’s init() method.
  • Since init() is going to be called only once you can put Servlet initialization code in it like getting hold of a database connection or whatever.

Responding to Requests(service())

  1. Once your Servlet is initialized and it’s init() method called, any request that the Servlet Container receives will be forwarded to your Servlet’s service() method.
  2. HttpServlet class breakes this service() method into more useful doGet(), doPost(), doDelete(), doOptions(), doPut() and doTrace() methods depending on the type of HTTP request it receives.
  3. So in order to generate respose you should override the doGet() or doPost() method as required.
  4. At this moment all the requests will be forwarded to the appropriate doGet() or doPost() or whatever method as required. No new instance will be created for your Servlet.

Servlet Destruction(destory())

When your application is stopped or Servlet Container shuts down, your Servlet’s destroy() method will be called. This allows you to free any resources you may have got hold of in your Servlet’s init() method.

Always Remember

  • init() and destroy() methods will be called only once during the life time of your Servlet
  •  while service() and it’s broken down methods ( doGet(), doPost() etc ) will be called as many times as requests are received for them by the Servlet Container.
Advertisements

9 Responses

  1. is get is used to get the data from the servelt and post is used to store the ddata to the servlet?
    —–is this is correct?
    i’m having a doubt in this
    can u clarify me?

  2. GET and POST are methods that are used on HTML form with as attribute to tag.

    Difference in GET and POST from Servlets point of view is not so much bothersome, since you use same getter methods to extact name value pair , except that in service method you either call doGet() or doPost().
    Yes under certain circustances it might make a difference like when a binary data is posted ( implying some client browser is uploading an image to server).
    GET and POST methods are more to do with HTTP protocol standards.
    GET – URL and name/value pairs are visible on the browser.
    There is a max limit for the length of data.
    POST – There is no limit for max length of data
    Client browser can send binary data
    On server side whether a CGI or a Servlet internally reads information from standard in.

    Header format for both GET and POST are a little bit different.

  3. well, above statement got truncated ..
    GET and POST are methods that are used on HTML form with …… as

    I intended to included FORM tags as an example.
    I wanted to indicate _LEFT_ANGLE_BRACKET_ FORM METHOD=POST
    Or _LEFT_ANGLE_BRACKET_ FORM METHOD=GET.

    Well, look at the HTML syntax for form. I am not able to see the text printed exactly as i type for TAGS.

  4. if a request comes through get method, and we have both service and doget who one first entertain the request

  5. I have written a tutorial on servlet life cycle here
    Servlet life cycle

  6. I read your blog for quite a long time and must tell that your articles are always valuable to readers.

  7. pls give me a structure jdbc connection

  8. nice job…keep up this good work

  9. Can you anyone explian me how do we declare in one servlet handles only 100 threads,for this do we need to configure any configuration file?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: