• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

servlet does not contain main()

 
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
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • 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.

 
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.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic