This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

importance of main method in servlets

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know if main() is used in servlets or not? if yes, how is it being used?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic