wood burning stoves*
The moose likes Servlets and the fly likes servlet does not contain main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet does not contain main()" Watch "servlet does not contain main()" New topic
Author

servlet does not contain main()

nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
Foe any java class to run, we need main(). When we execute java class at command prompt using "java anyJvaClass" , main() is searched by the JVM and execution starts from there on.
How does a servlet execute without main() ? I believe its in the container and container passes the parameters to servlet. But I dont exactly understand the mechanism. Can someone please explain the mechanism in detail about how the information is passed to servlet from the request and how ?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61238
    
  66

The container is the Java program that starts via main, as you suspected. As for the remainder of your question, I'm not sure what you are asking. Just like any other Java program, the container can create instances of a class (servlets are just classes like any other) and call them.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
thanks.
I got one part cleared.
About other question, when a request is received by the server, there is no response. While the
doGet(HttpServletRequest request, HttpServletResponse response) requires request and response both parameters. So is request parameter is supplied by the browser. Is response parameter supplied by the container to the doGet() ? What is supplies as response parameter by the container ?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61238
    
  66

Both the request and response instances are created and passed to the servlet by the container. The browser has nothing to do with generating Java instances on the server.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688


Hi,

Yes, you are right ......servlet has no main method but still how it runs.................


That's why servlet need help to start or invoke the servlet.

So servlet need program that invoke the servlet. Then tomcat or say container comes into the picture.

container is a program that invoke the servlet by passing sevlet config object.
That sevlet config object pass to init() method of servlet and create or initialize servlet and class object acquired all the property of servlet.

That's way it makes servlet.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Shiraz Khan
Ranch Hand

Joined: Mar 20, 2006
Posts: 51
Please refer to servlet life cycle, then you will understand why main method is not present in Servlet.

Basically, servlet's service method is called only when there is a request coming to that servlet. This service method calls doGet or doPost, which your servlet class overrides.
Since everthing is managed by container, you have to follow the protocols : get parameters from request object, and do the business processing blah blah blah ... and write the output to response object. Container then generates html response from this reponse object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: servlet does not contain main()