Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Is it possible to write an main() inside servlet? If yes how we can call that method ?

user-image
Question ajoutée par Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies
Date de publication: 2016/10/12

Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.

Basically servlets are usually used to implement web applications 

soubhagya kumar bitt
par soubhagya kumar bitt , Technical Lead , IBM india

Yes. It is possible to keep method inside a servlet. but it wont get called because in servlet container only life cycle method gets invoked. 

Abuzar Siddiqui
par Abuzar Siddiqui , Software Developer , Vyom Labs

Yes you can write main method in servlet but it would not be called implicitly by the executor. Servlet has its own life cycle and methods which are referred during execution automatically. Therefore there is no need of defining a main method there in servlet when it can't be used properly.

Kais Hamdulay
par Kais Hamdulay , Technical Lead , Barclays

Well you can write a main method inside servlet.

But the whole intention of servlet is to create a web based application which will undermined by main method

Anitha N
par Anitha N , Classroom Teacher , Little Flower’s Nursery & Primary School

pass-by-reference;  parameter will be the same as the callers passed argument.

pass-by-value;  parameter will be copy of the caller passed argument.

youssouf zemam
par youssouf zemam , dev la protection civilagent , protection civil

you can to write in this bloc this bloc is principale ,and if you write each method  u can to call it

 

Mayank Rawat
par Mayank Rawat , Spark Developer , Iris software

Servlet's lifecycle is managed by Application Container or Servlet Container(Apache Tomcat). We can create main method inside servlet, but it will not called by JRE automatically, it will work as normal method. Servlet has different methods, we can use these methods according to our requirement.    1. As replacement of main method servlet has init method, which gets called at a time of servlet initialize. This method is useful, when we want to do any       thing before the initialization of servlet.    2. There is a listener ServletContextListener, if we want to do any thing at a time of initialization of Application Context.

Omar Boukhobza
par Omar Boukhobza , Full-Stack Developer Consultant , CGI

It's not possible, because the servlets are designed to run inside a servlet container (web server). So every time a servlet is invoked, its "service()" method is called and execute the appropriate "doXxx()" method based on request type. We may see "service" method like "main" one in web development.

shashank sen
par shashank sen , junior full stack developer , aegle meditech

 you can keep the main method in servlet but it won't be invoked because in servlet container you only lifecycle method of the servlet.main method is for standalone apps,not for servlets which are executed by lifecycle methods called by the servlet container.You can place the main method but it will not invoke by the servlet container as it is not a life cycle method. Hope this helps you a bit.

More Questions Like This