• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet init()

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends!


can we declare init() method in service()method of the servlet.
Is there is any chance to declare that type.....please ...
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can we declare init() method in service()method of the servlet.
Is there is any chance to declare that type.....please ...


Sorry, but I don't see what you mean.

1. init()
2. service()
3. destroy()

are three life-cycle methods of servlet.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can�t define method within a method in Java.

Why do you want to declare inti() within service()?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reply from Chetan....

"You can�t define method within a method in Java."

But we write method System.out.println() with in other methods in Java. Need some clarification about this....
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravi kotha:
But we write method System.out.println() with in other methods in Java. Need some clarification about this....



Writing System.out.println() inside a method doesn't mean that we are defining that method inside another method. We are just calling that method, which is defined somewhere else.





So, if your question is
a) Can we declare init() method inside service() method, the answer is NO. It is illegal, and the code won't even compile.
b) Can we call init() method from service() method, the answer is yes, you can.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravi kotha:
Reply from Chetan....

"You can�t define method within a method in Java."

But we write method System.out.println() with in other methods in Java. Need some clarification about this....



We don't (rather can't) define a method in another method, we just call it.

If you mean, calling init() method inside service() method, then it's a valid question.

And my answer is, servlet is a java class, so you can do in servlet whatever you do in java class. So yes, you can call.



 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for ur clarification Mani Ram n Rathi ji
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But its better to avoid calling life cycle methods by your own. Some times you may end up with mess up.
 
vigneswar rao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot my clarification
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic