wood burning stoves 2.0*
The moose likes Servlets and the fly likes Confused about Life cycle of Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Confused about Life cycle of Servlets" Watch "Confused about Life cycle of Servlets" New topic
Author

Confused about Life cycle of Servlets

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

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.



Pawan Chopra
SCJP - DuMmIeS mInD
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Pawan,



executes the last 2 lines. Remember doPost() is called by HTTP service method.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
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.
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
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.


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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).


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about Life cycle of Servlets
 
Similar Threads
Problems with EJB Local and using servlets - null pointers
Can not run servlet in Eclipse Europa
multiple get requests - one thread serving them all?
overriding JDBCRealm
Best Practice for Listing Directory Names