Why main() is not written in servlets programs?

Questions by m.pavankumar

Showing Answers 1 - 1 of 1 Answers

askvenki

  • Aug 13th, 2007
 

In normal Java Application we have to invoke Java Interpter, it will start through main.?
Servlets have live cycle
Client Request->Container->Invokes the HTTP method automatically

  Was this answer useful?  Yes

ramesh.sukka

  • Nov 17th, 2007
 

As per the specs of Java, the only standalone program should implement/have the methods main() so that interpreter will understand the starting point of execution. If you consider Servlets it is not standalone program so id doesn’t require main.

  Was this answer useful?  Yes

As per the java specs , the only standalone program should implement/have the methods main() so that interpreter will understand the starting point of execution. If you consider Servlets it is not standalone program so id doesn’t require main.

  Was this answer useful?  Yes

Servlets are server side programmes everything will be done by the webcontainer.

i agree with them as they said standalone java applications will have main method

but servlets are not standalone java applications .

  Was this answer useful?  Yes

Servlet will execute in webcontainer only. The life cycle of servlet is

init(ServletConfig cg)
service(ServletRequest,ServletResponse)
destroy()

So our webcontainer will call these methods only to execute our servlet. So even you defined main() method in your servlet there is no wrong but there is no use why because it never execute throughout the servlet life cycle.

Rgds,
Mullangi

vinayroz

  • Aug 6th, 2008
 

Simple java programs are stand alone application and in this case we have to specify the starting point of execution for interpreter but servlets are server side tech and based on client request. Whenever client send a request, webcontainer calls servlet with its own life cycle method. So there is no need of having main() method in servlets.

  Was this answer useful?  Yes

Abinash

  • May 25th, 2015
 

Actually any web apps is managed by Servlet container. Servlet container is nothing but java program. So our servlet classes are managed by Servlet container which is already present in tomcat server and program starts execution from there itself. Thus Servlet container class has main method from where program starts execution and later call init(), service() method which will internally call doGet() or doPost(). This is the control flow.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions