It's not a secret anymore!*
The moose likes Servlets and the fly likes Main methods in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Main methods in servlets" Watch "Main methods in servlets" New topic
Author

Main methods in servlets

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

Hey,

Why Do not we have main() method in servlets. In other words, without writing the main method we are able to run the servlet program. why is it possible throgh servlets ?

Regards,
rahul roy
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Because Servlets are instantiated by the servlet container (ie Tomcat, Weblogic, etc).


GenRocket - Experts at Building Test Data
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rauhl Roy:
...
In other words, without writing the main method we are able to run the servlet program. why is it possible throgh servlets ?


There is a main method but it's not in the servlets themselves.
It's in the code for the container. Once started, it runs continuously, waiting for HTTP requests. When it receives one, as Gregg said, it instanciates a servlet to handle it.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rambabu Gonela
Greenhorn

Joined: Jul 31, 2006
Posts: 24
servlet does't have main() method but it run under control of one application that is container, the container will take care about servlet execution.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
One part of the specification is implemented by vendor and using the other part of the specification you are suppose to write the pluggable component (servlets) that would fit into the first specifications implementation (that is the container).So there is a main method ,but that lies with the container code and at later part of the flow your code gets enxcuted.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main methods in servlets
 
Similar Threads
Main methods
Why Init() method
not main() in servlet
calling main
Servlet Constructor