This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Confused about Life cycle of Servlets

 
Ranch Hand
Posts: 419
Mac jQuery Objective C
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

Pawan,



executes the last 2 lines. Remember doPost() is called by HTTP service method.
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 43016
76
  • Mark post as helpful
  • send pies
  • 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).
 
    Bookmark Topic Watch Topic
  • New Topic