Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet does not contain main()

 
nirali shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirali shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Shiraz Khan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic