• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about Life cycle of Servlets

 
Ranch Hand
Posts: 419
Mac jQuery Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


I have following Servlet.


When the very first request comes to this servlet. I got following results:

i am in constructor
init
doPost
service HTTP
service generic



I would like to know the reason behind last 2 outputs. I am confused how these two methods are getting executed.


 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawan,



executes the last 2 lines. Remember doPost() is called by HTTP service method.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this code is just for experiment then its fine, otherwise in general refrain from overriding the service method. Override the doPost and/or doGet methods instead...
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The service(HttpServletRequest, HttpServletResponse) is the HTTP-specific version of the Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) method. Thus, when you make an HTTP request to your server, you get the HTTP service method invoked before the generic service method.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet life cycle is as follows.

1. Creates an Instance of the servlet class (Constructor)
2. Initializes the servlet (init method) and wait for requests.
3. if any request comes in, service method is called and this determines which type of request it is and calls the appropriate method(like doGet, doPost etc...)
4. The destroy method is called before removing the servlet from the container

I think you can trace what is happening by putting as the first line of the method definition and at the end of the method definition.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If this code is just for experiment then its fine, otherwise in general refrain from overriding the service method. Override the doPost and/or doGet methods instead...


+1. The service method doesn't need to be overwritten unless you're writing a servlet to handle HTTP extensions (which would be an exceedingly rare occasion).
 
reply
    Bookmark Topic Watch Topic
  • New Topic