Why servlet does not have main method




















Then, it will bind your code to incoming HTTP calls, but it will not start new processes for that. It will rather start new threads. Web applications are not stand-alone applications. They run on some applications what we call a servletContainer in a Java context so there aren't any "main method or Java process OS " for any web application.

They are just deployed on those containers that have a main method and Java process in OS runtime. If you've created a basic program in Java then you must know that every Java program has a main method, which is the starting point of the program. So, how come servlets don't have a main? That is because servlets are served using Web containers. A Web container will perform all the underlying work on behalf of the servlet so the programmer can focus on the business logic. When a client requests a servlet, the server hands requests to a Web container where the servlet is deployed.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why don't I see any main method in this Java dynamic web project? Ask Question. Asked 7 years, 10 months ago.

Active 4 months ago. Viewed 17k times. Improve this question. Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. Kraken Kraken Add a comment. Active Oldest Votes. You can still see the main method being called by using the debugger like this: Put a breakpoint in some initialization method, such as the init method of some servlet Servlet. Servlet are supposed to be deployed under web container like tomcat and their life cycles are managed through them.

When you try to run it as standalone program, its like any other plain java program without main method and compiler won't be able to run without main method which is as starting point and complains. Servlet life cycle is maintained by container, which is responsible to execute the servlet, container requires some cycles to be performed by servlet, and it executes the life cycle methods only. In short, container don't have information about any main method, so there is no need.

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. 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 Container then generates html response from this reponse object.

Boost this thread! Html Input to oracle through servlet and JSP output. Why do we have init method in Servlets?

Thread mechanism for Request in web container.



0コメント

  • 1000 / 1000