File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet init()

 
vigneswar rao
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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 ...
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can�t define method within a method in Java.

Why do you want to declare inti() within service()?
 
Ravi Kotha
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur clarification Mani Ram n Rathi ji
 
Kj Reddy
Ranch Hand
Posts: 1704
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot my clarification
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic