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 ?
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.
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 ?
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.
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.
Don't MAKE me come back there with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop