aspose file tools*
The moose likes Servlets and the fly likes importance of main method in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "importance of main method in servlets" Watch "importance of main method in servlets" New topic
Author

importance of main method in servlets

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

may i know if main() is used in servlets or not? if yes, how is it being used?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Typically, it is not.

Servlets are instanciated by the servlet container which also calls their methods. If you have user defined methods in your servlet class that you want to be able to test from the command line, you might want to add a main method. This is not typical as it is considered a best practice to factor any non web handling functionality out of servlet classes and into beans or plain old Java objects.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

did you mean, we can use the main method if we want , but it is not considered as a best practice.

please correct me if im wrong.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Yes,
Nothing prevents you from having a main method in servlet. Yes its not a best practise. Why would any one need a main method in servlet?


[Servlet tutorial] [Servlet 3.0 Cook Book]
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

if we can use main method ... how the execution happens.. i mean.. both main, service metods will have same priority..

please correct me
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

applications get 'started' via the main method
servlets get 'started' via the service method
applets get started differently again
EJB's differently again

The point is that if you write a 'main' method and call it from the command line, the class will be an application and not a servlet. If you put it in a container and the container calls the service method, it acts like a servlet.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

And if you put it in a servlet container, nothing will call main(); it will just sit there, unused, just as would any other random methods named foo() or bar() or kjhskjhdkjhd(). I think that's what you're after, right?


[Jess in Action][AskingGoodQuestions]
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

thanks alot all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: importance of main method in servlets